This topic describes how to install and use RDS SDK for Java. Alibaba Cloud SDK for Java allows you to call ApsaraDB for Redis API operations without complex coding.

Perform online debugging and generate SDK demos

Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to search for API operations, call API operations, and dynamically generate SDK sample code.

Prerequisites

  • An AccessKey pair is created. For more information, see Create an AccessKey pair.
    Note To protect the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user, grant the RAM user permissions to access DTS, and then use the AccessKey pair of the RAM user to call DTS SDK for Java. For more information, see Implement access control by using RAM.
  • A Java environment is installed. Alibaba Cloud SDK for Java supports only Java Development Kit (JDK) 1.6 or later.

Install Alibaba Cloud SDK for Java

You can install Alibaba Cloud SDK for Java by adding dependencies through Maven or by importing JAR files. For more information, see Install Alibaba Cloud SDK for Java.
Note If you use Maven to add dependencies, enter the latest version number of the SDK.

Use Alibaba Cloud SDK for Java to call the ApsaraDB for Redis API operations

  1. Create a DefaultAcsClient instance and initialize the instance.
    DefaultProfile profile = DefaultProfile.getProfile(
        "<regionId>",         //The region ID.
        "<accessKeyId>",      //The AccessKey ID of the RAM user.
        "<accessKeySecret>"); //The AccessKey secret of the RAM user.
    IAcsClient client = new DefaultAcsClient(profile);
  2. Optional. Specify the region and endpoint.
    An endpoint is the API server address of an Alibaba Cloud service. Each service may have different endpoints in different regions. An endpoint addressing module is built in the Alibaba Cloud SDK. When you call the SDK to initiate a request to a service, the SDK automatically identifies the endpoint based on the region ID that you specified when you create the SDK client and the service ID. Therefore, this step is optional. For more information about the endpoints of different regions, see Endpoints.
    DefaultProfile.addEndpoint(
        "<EndpointName>",  //The name of the endpoint. You can use a custom name.
        "<regionId>",      //The region ID.
        "r-kvstore",       //The service code. The service code of ApsaraDB for Redis is r-kvstore.
        "<endpoint>");     //The endpoint of the API server.
  3. Create an API request and set the request parameters.
    The following is an example on how to call the DescribeAccounts operation.
    DescribeAccountsRequest request = new DescribeAccountsRequest();
        request.setInstanceId("r-bp1xxxxxxxxxxxx");
  4. Send the request and handle the response or exception.
    try {
        DescribeAccountsResponse response = client.getAcsResponse(request);
        System.out.println(new Gson().toJson(response));
    } catch (ServerException e) {
        e.printStackTrace();
    } catch (ClientException e) {
        System.out.println("ErrCode:" + e.getErrCode());
        System.out.println("ErrMsg:" + e.getErrMsg());
        System.out.println("RequestId:" + e.getRequestId());
    }

Complete sample code

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.r_kvstore.model.v20150101.*;

public class DescribeAccounts {

    public static void main(String[] args) {
        //Create a default ACS client and initialize it.
        DefaultProfile profile = DefaultProfile.getProfile(
            "cn-hangzhou",        //The region ID.
            "<accessKeyId>",      //The AccessKey ID of the RAM user.
            "<accessKeySecret>"); //The AccessKey secret of the RAM user.
        IAcsClient client = new DefaultAcsClient(profile);
        //Optional. Specify the region and endpoint.
        DefaultProfile.addEndpoint(
            "cn-hangzhou", //The name of the endpoint. You can use a custom name as needed.
            "cn-hangzhou", //The region ID.
            "r-kvstore",   //The service code. The service code of ApsaraDB for Redis is r-kvstore.
            "r-kvstore.aliyuncs.com"); //The endpoint of the API server.
        //Create an API request and set the parameters.
        DescribeAccountsRequest request = new DescribeAccountsRequest();
        request.setInstanceId("r-bp1xxxxxxxxxxxx");
        //Send the request and handle the response or exception.
        try {
            DescribeAccountsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}