This topic describes how to install and use Alibaba Cloud SDK for Java to call the ApsaraDB for Redis API. Alibaba Cloud SDK for Java allows you to call the ApsaraDB for Redis API without writing complex code.

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

  • A Resource Access Management (RAM) user account and its AccessKey are prepared. You can go to the AccessKey management page of the Alibaba Cloud console to create or view the AccessKey. Alternatively, you can contact your system administrator to obtain the AccessKey.
  • 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 choose the method of adding dependencies through Maven, 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 account.
        "<accessKeySecret>"); // The AccessKey secret of the RAM user account.
    IAcsClient client = new DefaultAcsClient(profile);
  2. Optional. Specify the region and endpoint.
    An endpoint is the address of the API server of an Alibaba Cloud service. An Alibaba Cloud service may have different endpoints for 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 region ID that you specified when creating the SDK client and the service ID. Therefore, this step is optional. For more information about the endpoints for different regions, see Endpoints.
    DefaultProfile.addEndpoint(
        "<endpointName>",  // The name of the endpoint. You can customize the 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 request parameters.
    The following code takes the DescribeAccounts operation as an example.
    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 account.
             "<accessKeySecret>"); // The AccessKey secret of the RAM user account.
        IAcsClient client = new DefaultAcsClient(profile);
        // Optional. Specify the region and endpoint.
        DefaultProfile.addEndpoint(
            "cn-hangzhou", // The name of the endpoint. You can customize the 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 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());
        }

    }
}