This topic describes how to use Alibaba Cloud SDK for Java to query the detailed attributes of an Apsara PolarDB cluster (an ApsaraDB PolarDB for MySQL cluster).

Prerequisites

Before you begin, ensure that the following requirements are met:
  • 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.
    <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-polardb -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-polardb</artifactId>
            <version>1.6.3</version>
        </dependency>
    </dependencies>

Sample code

This section shows the sample code:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClusterAttributeRequest;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClusterAttributeResponse;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClustersRequest;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClustersResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.List;


/**
 * DescribeDBClusterAttribute     Queries the detailed attributes of a specified Apsara PolarDB cluster.
 * DescribeDBClusters    Queries the list of Apsara PolarDB clusters or the list of clusters authorized by the RAM user.
 */
public class DescribeDBClusterAttribute {

    public static void main(String[] args) {

        // Initialize common request parameters.
        IAcsClient client = Initialization();
        // Query the list of running MySQL clusters.
        List<DescribeDBClustersResponse.DBCluster> dbClusters = DescribeDBClusters(client);
        // Query the ID of the target cluster.
        for (DescribeDBClustersResponse.DBCluster dbCluster : dbClusters){
            // The ID of the cluster.
            String dbClusterId = dbCluster.getDBClusterId();
            // Query the detailed attributes of a MySQL cluster.
            DescribeDBClusterAttributeResponse describeDBClusterAttributeResponse = DescribeDBClusterAttribute(client, dbClusterId);
            // Process the response and business logic.
            System.out.println(new Gson().toJson(describeDBClusterAttributeResponse));
        }
    }

    /**
     * DescribeDBClusterAttribute    Queries the detailed attributes of a specified Apsara PolarDB cluster.
     */
    private static DescribeDBClusterAttributeResponse DescribeDBClusterAttribute(IAcsClient client, String dbClusterId) {
        DescribeDBClusterAttributeRequest request = new DescribeDBClusterAttributeRequest();
        // Cluster ID
        request.setDBClusterId(dbClusterId);
        try {
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * DescribeDBClusters     Queries the list of running ApsaraDB PolarDB for MySQL clusters.
     */
    private static List<DescribeDBClustersResponse.DBCluster> DescribeDBClusters(IAcsClient client) {
        DescribeDBClustersRequest request = new DescribeDBClustersRequest();
        // Database type. Valid values:
        // MySQL
        // PostgreSQL
        // Oracle
        request.setDBType("MySQL");
        // Cluster status
        // Creating: The cluster is being created.
        // Running: The cluster is running.
        // Deleting: The cluster is being deleted.
        // Rebooting: The cluster is being restarted.
        // DBNodeCreating: The node is being added.
        // DBNodeDeleting: The node is being deleted
        // ClassChanging: The node specification is being updated.
        // NetAddressCreating: The network connection is being created.
        // NetAddressDeleting: The network connection is being deleted.
        // NetAddressModifying: The network connection is being modified.
        request.setDBClusterStatus("Running");
        try {
            DescribeDBClustersResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            return response.getItems();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * Initialization    Initializes common request parameters
     */
    private static IAcsClient Initialization() {
        // 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.               
        return new DefaultAcsClient(profile);
    }
}

Output

This section shows the sample success response:
---------------------The list of running ApsaraDB PolarDB for MySQL clusters was queried.-------------------
{
    "requestId": "30745EE3-82B0-4011-AEE3-9E1C892B97CE", 
    "pageNumber": 1, 
    "totalRecordCount": 0, 
    "pageRecordCount": 0, 
    "items": [...]
}
---------------------The details of an ApsaraDB PolarDB for MySQL cluster was queried.-------------------
{
    "DBVersion": "5.6",
    "LockMode": "Unlock",
    "DBClusterDescription": "test",
    "DBClusterNetworkType": "VPC",
    "DBClusterId": "pc-xxxxxxxxxxxxxx",
    "VSwitchId": "vsw-xxxxxxxxxxxxxx",
    "Engine": "POLARDB",
    "DBClusterStatus": "Running",
    "CreationTime": "2019-04-26T06:01:28Z",
    "MaintainTime": "18:00Z-19:00Z",
    "Tags": [],
    "VPCId": "vpc-xxxxxxxxxxxxxx",
    "ExpireTime": "",
    "Expired": false,
    "RequestId": "4E148395-950A-46F8-BFF8-274A64CD793B",
    "RegionId": "cn-qingdao",
    "DBType": "MySQL",
    "DBNodes": [
        {
            "CreationTime": "2019-04-26T22:01:28Z",
            "MaxIOPS": 8000,
            "DBNodeRole": "Writer",
            "MaxConnections": 1200,
            "DBNodeClass": "polar.mysql.x2.medium",
            "DBNodeStatus": "Running",
            "ZoneId": "cn-qingdao-c",
            "DBNodeId": "pi-xxxxxxxxxxxxxx"
        },
        {
            "CreationTime": "2019-04-26T22:01:28Z",
            "MaxIOPS": 8000,
            "DBNodeRole": "Reader",
            "MaxConnections": 1200,
            "DBNodeClass": "polar.mysql.x2.medium",
            "DBNodeStatus": "Running",
            "ZoneId": "cn-qingdao-c",
            "DBNodeId": "pi-xxxxxxxxxxxxxx"
        }
    ],
    "SQLSize": 0,
    "PayType": "Postpaid"
}