This topic describes how to install Alibaba Cloud SDK for Java and 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. For more information, see API Reference. Take note that Alibaba Cloud SDK for Java is different from the SDKs used to access Cassandra instances. This topic describes how to install and use Alibaba Cloud SDK for Java.

1. Create an AccessKey pair

Perform online debugging and generate SDK demos

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. On the OpenAPI Explorer web interface, select ApsaraDB for Cassandra. A list of operations is displayed, and sample code in various languages is provided for your reference.

1. Download and install Alibaba Cloud SDK for Java

Note: When you use Maven to download the SDK package, make sure that the latest SDK version is downloaded.

2. Sample code

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

1. Set 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 a client.

IAcsClient client = new DefaultAcsClient(profile);

3. Construct the request object.

DescribeClusterRequest request = new DescribeClusterRequest();
request.setClusterId("cds-wz9bui92****697r");

4. Initiate the request.

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

Sample request

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 response

{
  "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. Set 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 a client.

IAcsClient client = new DefaultAcsClient(profile);

3. Construct 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. Initiate the request.

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

Sample request

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 response

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

Call the ResizeNodeCount operation to add nodes to a data center

1. Set 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 a client.

IAcsClient client = new DefaultAcsClient(profile);

3. Construct the request object.

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

4. Initiate the request.

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

Sample request

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 response

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

Call the DeleteCluster operation to delete an ApsaraDB for Cassandra cluster

1. Set 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 a client.

IAcsClient client = new DefaultAcsClient(profile);

3. Construct the request object.

DeleteClusterRequest request = new DeleteClusterRequest();
request.setClusterId("cds-bp1hy2ipk****46k");

4. Initiate the request.

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

Sample request

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 response

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