All Products
Search
Document Center

Use Alibaba Cloud SDK for Java to call the ApsaraDB for Cassandra API

Last Updated: Dec 10, 2021

This topic describes how to install Alibaba Cloud SDK for Java and then use the SDK to call the ApsaraDB for Cassandra API. Alibaba Cloud SDK for Java frees you from complex coding. You can use it to manage ApsaraDB for Cassandra instances. For example, you can create, upgrade, and delete instances, configure whitelists, and manage tags. Alibaba Cloud SDK for Java is different from the SDKs used to access ApsaraDB for Cassandra instances. This topic describes how to install and use Alibaba Cloud SDK for Java.

Prerequisites

An AccessKey pair is created. For more information, see Obtain an AccessKey pair.

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. After you navigate to the OpenAPI Explorer page, choose the ApsaraDB for Cassandra service. The system provides various API operations related to the ApsaraDB for Cassandra service. You can debug each API operation with the sample code written in different programming languages.

Download and 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 to use Maven, download the Alibaba Cloud SDK for Java package of the latest version.

Demo

Call the DescribeClusterRequest operation to query information about an ApsaraDB for Cassandra cluster

  1. Specify environment parameters.

    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                    "LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
    // The following parameters are optional. You can use the default settings.
    DefaultProfile.addEndpoint(
                            "cn-hangzhou",    // The region ID.
                            "cassandra", // The product code.
                            "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
    );
  2. Initialize the client.

    IAcsClient client = new DefaultAcsClient(profile);
  3. Create the request object.

    DescribeClusterRequest request = new DescribeClusterRequest();
    request.setClusterId("cds-wz9bui92****697r");
  4. Call the API operation.

    DescribeClusterResponse response;
    try {
            response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
    } catch (ClientException e) {
            e.printStackTrace();
    }

Example

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
                "cn-hangzhou",    // The region ID.
                "cassandra", // The product code.
                "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
);
IAcsClient client = new DefaultAcsClient(profile);
DescribeClusterRequest request = new DescribeClusterRequest();
request.setClusterId("cds-wz9bui92****697r");
DescribeClusterResponse response;
try {
        response = client.getAcsResponse(request);
        System.out.println(new Gson().toJson(response));
} catch (ClientException e) {
        e.printStackTrace();
}

Sample success responses

{
  "RequestId": "3B235CE2-2339-42A8-A11D-5F7548519AD2",
  "Cluster": {
    "Status": "Running",
    "MajorVersion": "3.11",
    "CreatedTime": "2020-04-21T08:50:04Z",
    "ClusterId": "cds-wz9bui92****6973",
    "MinorVersion": "3.11.9",
    "PayType": "PayAsYouGo",
    "LockMode": "Expired",
    "IsLatestVersion": true,
    "MaintainEndTime": "22:00Z",
    "DataCenterCount": 1,
    "ClusterName": "auto_test20200421170214",
    "MaintainStartTime": "18:00Z",
    "Tags": {
      "Tag": [
        {
          "Value": "1",
          "Key": "test"
        },
        {
          "Value": "2",
          "Key": "test2"
        }
      ]
    }
  }
}

Call the CreateCluster operation to create an ApsaraDB for Cassandra cluster

  1. Specify environment parameters.

    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                    "LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
    // The following parameters are optional. You can use the default settings.
    DefaultProfile.addEndpoint(
                            "cn-hangzhou",    // The region ID.
                            "cassandra", // The product code.
                            "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
    );
  2. Initialize the client.

    IAcsClient client = new DefaultAcsClient(profile);
  3. Create the request object.

    CreateClusterRequest request = new CreateClusterRequest();
    // The following parameters are required.
    request.setPayType("PayAsYouGo");
    request.setRegionId("cn-hangzhou");
    request.setZoneId("cn-hangzhou-e");
    request.setMajorVersion("3.11");
    request.setInstanceType("cassandra.c.large");
    request.setNodeCount("2");
    request.setVpcId("vpc-wz9s6dl6nttp1****ydur");
    request.setVswitchId("vsw-wz9c90poaus9q****1dxl");
    // The following parameters are optional.
    request.setPeriodUnit("Year");// If the billing method is subscription, this parameter is required. 
    request.setPeriod("12");// If the billing method is subscription, this parameter is required. 
    request.setAutoRenew("false");// If the billing method is subscription, this parameter is required. 
    request.setAutoRenewPeriod("3");// If the billing method is subscription, this parameter is required. 
    request.setClientToken("ETnLKlblzczshOTUbOCzxxxxxxxxxx");
    request.setClusterName("cassandra_01");
    request.setDataCenterName("cassandra_dc_01");
    request.setDiskType("cloud_ssd");
    request.setDiskSize("160");
    request.setPassword("Cassandra1@!");
  4. Call the API operation.

    CreateClusterResponse response;
    try {
            response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
    } catch (ClientException e) {
            e.printStackTrace();
    }

Example

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
                "cn-hangzhou",    // The region ID.
                "cassandra", // The product code.
                "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
);
IAcsClient client = new DefaultAcsClient(profile);
CreateClusterRequest request = new CreateClusterRequest();
// The following parameters are required.
request.setPayType("PayAsYouGo");
request.setRegionId("cn-hangzhou");
request.setZoneId("cn-hangzhou-e");
request.setMajorVersion("3.11");
request.setInstanceType("cassandra.c.large");
request.setNodeCount("2");
request.setVpcId("vpc-wz9s6dl6nttp1****ydur");
request.setVswitchId("vsw-wz9c90poaus9q****1dxl");
// The following parameters are optional.
request.setPeriodUnit("Year");// If the billing method is subscription, this parameter is required. 
request.setPeriod("12");// If the billing method is subscription, this parameter is required. 
request.setAutoRenew("false");// If the billing method is subscription, this parameter is required. 
request.setAutoRenewPeriod("3");// If the billing method is subscription, this parameter is required. 
request.setClientToken("ETnLKlblzczshOTUbOCzxxxxxxxxxx");
request.setClusterName("cassandra_01");
request.setDataCenterName("cassandra_dc_01");
request.setDiskType("cloud_ssd");
request.setDiskSize("160");
request.setPassword("Cassandra1@!");
CreateClusterResponse response;
try {
        response = client.getAcsResponse(request);
        System.out.println(new Gson().toJson(response));
} catch (ClientException e) {
        e.printStackTrace();
}

Sample success responses

{
    "ClusterId":"hb-bp16o0pd5****582s",
    "RequestId":"728C7EAF-4844-4D42-9BBE-DFFFBB77CF33"
}

Call the ResizeNodeCount operation to scale out nodes in a data center.

  1. Specify environment parameters.

    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                    "LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
    // The following parameters are optional. You can use the default settings.
    DefaultProfile.addEndpoint(
                            "cn-hangzhou",    // The region ID.
                            "cassandra", // The product code.
                            "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
    );
  2. Initialize the client.

    IAcsClient client = new DefaultAcsClient(profile);
  3. Create the request object.

    ResizeNodeCountRequest request = new ResizeNodeCountRequest();
    request.setClusterId("cds-bp1b136j****5d59");
    request.setDataCenterId("cn-hangzhou-g");
    request.setNodeCount("3");
  4. Call the API operation.

    ResizeNodeCountResponse response;
    try {
            response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
    } catch (ClientException e) {
            e.printStackTrace();
    }

Example

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
                "cn-hangzhou",    // The region ID.
                "cassandra", // The product code.
                "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
);
IAcsClient client = new DefaultAcsClient(profile);
ResizeNodeCountRequest request = new ResizeNodeCounttaRequest();
request.setClusterId("cds-bp1b136j****5d59");
request.setDataCenterId("cn-hangzhou-g");
request.setNodeCount("3");
ResizeNodeCountResponse response;
try {
        response = client.getAcsResponse(request);
        System.out.println(new Gson().toJson(response));
} catch (ClientException e) {
        e.printStackTrace();
}

Sample success responses

{
    "RequestId": "D7D3088F-AA4F-49C3-BB46-B04F3E35041A"
}

Call the DeleteCluster operation to delete an ApsaraDB for Cassandra cluster

  1. Specify environment parameters.

    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                    "LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
    // The following parameters are optional. You can use the default settings.
    DefaultProfile.addEndpoint(
                            "cn-hangzhou",    // The region ID.
                            "cassandra", // The product code.
                            "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
    );
  2. Initialize the client.

    IAcsClient client = new DefaultAcsClient(profile);
  3. Create the request object.

    DeleteClusterRequest request = new DeleteClusterRequest();
    request.setClusterId("cds-bp1hy2ipk****46k");
  4. Call the API operation.

    DeleteClusterResponse response;
    try {
            response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
    } catch (ClientException e) {
            e.printStackTrace();
    }

Example

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
                "cn-hangzhou",    // The region ID.
                "cassandra", // The product code.
                "cassandra.aliyuncs.com"    // The endpoint, which is the domain name.
);
IAcsClient client = new DefaultAcsClient(profile);
DeleteClusterRequest request = new DeleteClusterRequest();
request.setClusterId("cds-bp1hy2ipk****46k");
DeleteClusterResponse response;
try {
        response = client.getAcsResponse(request);
        System.out.println(new Gson().toJson(response));
} catch (ClientException e) {
        e.printStackTrace();
}

Sample success responses

{
    "RequestId":"728C7EAF-4844-4D42-9BBE-DFFFBB77CF33"
}