Alibaba Cloud SDK for Java allows you to access ApsaraDB for Redis without the need to write complex code. This topic describes how to install and use Alibaba Cloud SDK for Java.

Debugging

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 ApsaraDB for Redis, and then use the AccessKey pair of the RAM user to call Alibaba Cloud SDK for Java. For more information, see Implement access control by using RAM.
  • The Java Development Kit (JDK) 1.6 or a later version is installed.

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

  1. Create a DefaultAcsClient instance and initialize it.
    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. Alibaba Cloud SDK for Java has a built-in module to search for endpoints. When you use the SDK to send an API request to a service, the SDK automatically obtains the endpoint based on the service ID and the region ID that you specified when you create the SDK client. 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 as needed.
        "<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 code 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 DefaultAcsClient instance 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 request 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());
        }

    }
}