All Products
Search
Document Center

:Use ApsaraDB for MongoDB SDK for Java

Last Updated:Jul 18, 2023

This topic describes how to use ApsaraDB for MongoDB SDK for Java and provides examples for your reference.

Prerequisites

  • An AccessKey pair is created. For more information, see Create an AccessKey pair.

    Warning

    We recommend that you create a Resource Access Management (RAM) user, grant the RAM user the permissions to access ApsaraDB for MongoDB, and then use the AccessKey pair of the RAM user to call ApsaraDB for MongoDB SDK for Java. This way, you can protect the AccessKey pair of your Alibaba Cloud account and prevent security risks. For more information, see Control access to resources by using RAM users.

  • The ApsaraDB for MongoDB SDK software package for Java is downloaded. For more information, see Download SDKs.

Install the SDK

For more information, see Install Alibaba Cloud SDK for Java.

Call the SDK

  1. Specify the region and the AccessKey pair.

    IClientProfile profile = DefaultProfile.getProfile("<RegionId>","<accessKeyId>","<accessSecret>");
    Note
    • <RegionId>: the ID of the region

    • <accessKeyId>: the AccessKey ID of the RAM user

    • <accessSecret>: the AccessKey secret of the RAM user

  2. Optional:Set the endpoint.

    An endpoint is the API server address of an Alibaba Cloud service. Each service may have different endpoints in different regions. Each Alibaba Cloud SDK has a built-in endpoint addressing module. When you call an Alibaba Cloud SDK to request access to a service from a client, the SDK automatically identifies the endpoint based on the region ID that you specified when you created the client and based on the service ID. This step is optional. For more information about the endpoints of different regions, see Endpoints.

    DefaultProfile.addEndpoint("<endpointName>","<RegionId>", "dds", "<domain>");
    Note
    • <endpointName>: the name of the endpoint.

    • <RegionId>: the ID of the region. For more information, see Regions and zones.

    • <domain>: the domain name. For more information, see Endpoints.

  3. Initialize the client.

    DefaultAcsClient client = new DefaultAcsClient(profile);
  4. Create an API request and configure the required parameters.

    The following code snippet is an example that shows how to create and configure a request to call the DescribeAccounts operation which is used to view the information of the test account:

    DescribeAccountsRequest request = new DescribeAccountsRequest();
          request.setDBInstanceId("dds-bp19f409d7512****");
          request.setAccountName("test");
  5. View the response.

    DescribeAccountsResponse response = client.getAcsResponse(request);

Sample requests

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.dds.model.v20151201.DescribeAccountsRequest;
import com.aliyuncs.dds.model.v20151201.DescribeAccountsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

 public class ApiDescribeAccountsTest {
     public static void main(String[] args_){
         IClientProfile profile = DefaultProfile.getProfile("cn-qingdao", "********", "**********");
         //Initialize the client.
         DefaultAcsClient client = new DefaultAcsClient(profile);
         DescribeAccountsRequest request = new DescribeAccountsRequest();
         request.setDBInstanceId("dds-bp19f409d7512****");
         request.setAccountName("test");
         try {
             DescribeAccountsResponse response = client.getAcsResponse(request);
             String s = JSON.toJSONString(response);
             System.out.println(s);
         }
         catch (Exception e) {
             e.printStackTrace();
         }
     }
}

Sample success response

{"accounts":[{"accountName":"test","accountStatus":"Available","dBInstanceId":"dds-bp19f409d7512****"}],"requestId":"4D********-9640ED88F3C4"}