This topic describes how to use Alibaba Cloud SDK for Java to call the ModifyInstanceAttribute operation of ApsaraDB for Redis to modify the attributes of an ApsaraDB for Redis instance, including its name and password.

Prerequisites

  • You must have an Alibaba Cloud account and an AccessKey pair (AccessKey ID and AccessKey secret) to use Alibaba Cloud SDK for Java. You can create and view your AccessKey pair on the AccessKey Management page in the Alibaba Cloud Management Console.
  • You have installed Alibaba Cloud SDK for Java. For more information about SDK versions, see Alibaba Cloud SDK.
    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>java.demo</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.4.3</version>
            </dependency>
            <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-r-kvstore -->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-r-kvstore</artifactId>
                <version>2.1.2</version>
            </dependency>
        </dependencies>
    </project>

Sample code

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.r_kvstore.model.v20150101.ModifyInstanceAttributeRequest;
import com.aliyuncs.r_kvstore.model.v20150101.ModifyInstanceAttributeResponse;
import com.google.gson.Gson;

/**
 * Call the ModifyInstanceAttribute operation to modify the attributes of an ApsaraDB for Redis instance, including its name and password.
 */
public class ModifyInstanceAttribute {
    public static void main(String[] args) {
        // Initialize request parameters.
        DefaultProfile profile = DefaultProfile.getProfile(
                 "<your-region-id>", // The ID of the region.
                 "<your-access-key-id>", // The AccessKey ID.
                 "<your-access-key-secret>"); // The AccessKey secret.
        IAcsClient client = new DefaultAcsClient(profile);
        // Configure request parameters.
        ModifyInstanceAttributeRequest request = new ModifyInstanceAttributeRequest();
        // The ID of the instance that you want to modify.
        request.setInstanceId("<your-InstanceId>");
        // The new instance name.
        request.setInstanceName("test123");
        // The new password of the instance.
        request.setNewPassword("Test123456");
        try {
            // Send a request to get a response.
            ModifyInstanceAttributeResponse response = client.getAcsResponse(request);
            // Process the response.
            System.out.println(new Gson().toJson(response));
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}