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 Implement access control by using RAM.
  • The ApsaraDB for MongoDB SDK software package for Java is downloaded. For more information, see Download SDKs.

Install ApsaraDB for MongoDB SDK for Java

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

Call ApsaraDB for MongoDB SDK for Java

  1. Specify the region and the AccessKey pair.
    IClientProfile profile = DefaultProfile.getProfile("<RegonId>","<accessKeyId>","<accessSecret>");
    Note
    • <RegonId>: the ID of the region
    • <accessKeyId>: the AccessKey ID of the RAM user
    • <accessSecret>: the AccessKey secret of the RAM user
  2. Optional:Specify 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 ApsaraDB for MongoDB in different regions, see Endpoints.
    DefaultProfile.addEndpoint("<endpointName>","<RegonId>", "dds", "<domain>");
    Note
    • <endpointName>: the endpoint of ApsaraDB for MongoDB in the region.
    • <RegonId>: the ID of the region. For more information, see Regions and zones.
    • <domain>: the domain name of the region. For more information, see Endpoints.
  3. Initialize the client.
    DefaultAcsClient client = new DefaultAcsClient(profile);
  4. Create an API request and configure the request parameters.

    The following code snippet is an example that shows how to create and configure a request to call the DescribeAccounts operation:

    DescribeAccountsRequest request = new DescribeAccountsRequest();
          request.setDBInstanceId("dds-********");
          request.setAccountName("root");
  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-********");
         request.setAccountName("root");
         try {
             DescribeAccountsResponse response = client.getAcsResponse(request);
             String s = JSON.toJSONString(response);
             System.out.println(s);
         }
         catch (Exception e) {
             e.printStackTrace();
         }
     }
}

Sample success responses

null

References

For more information, see Java SDK Developer Guide.