This topic describes how to install Alibaba Cloud SDK for Java and call the ApsaraDB for Cassandra API. By using Alibaba Cloud SDK for Java, you can manage ApsaraDB for Cassandra instances without the need of coding. For example, you can create, upgrade, and delete instances, configure whitelists, and manage tags. For more information, see API reference. Note that Alibaba Cloud SDK for Java is different from the SDKs used to access Cassandra instances.

1. Create an AccessKey

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. 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: Download the latest versions of SDKs through Maven.

2. Sample code

1)You can call DescribeClusterRequest to query the details of a Cassandra instance.

① 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.
			"hbase.aliyuncs.com"    // The endpoint, which is the domain name.
);

② Initialize the client.

IAcsClient client = new DefaultAcsClient(profile);

③ Construct the request object.

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

④ Initiate a call.

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

Sample requests

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
		"cn-hangzhou",    // The region ID.
		"cassandra", // The product code.
		"hbase.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 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"
        }
      ]
    }
  }
}

2)You can call CreateCluster to create a Cassandra instance.

① 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.
			"hbase.aliyuncs.com"    // The endpoint, which is the domain name.
);

② Initialize the client.

IAcsClient client = new DefaultAcsClient(profile);

③ Construct the request object.

CreateClusterRequest request = new CreateClusterRequest();
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");

request.setPeriodUnit("Year");            //You must specify this parameter if the billing method is Subscription.
request.setPeriod("12");                      //You must specify this parameter if the billing method is Subscription.
request.setAutoRenew("false");          //You must specify this parameter if the billing method is Subscription.
request.setAutoRenewPeriod("3");     //You must specify this parameter if the billing method is Subscription.
request.setClientToken("ETnLKlblzczshOTUbOCzxxxxxxxxxx");
request.setClusterName("cassandra_01");
request.setDataCenterName("cassandra_dc_01");
request.setDiskType("cloud_ssd");
request.setDiskSize("160");
request.setPassword("Cassandra1@!");

④ Initiate a call.

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

Sample requests

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
			"cn-hangzhou",    // The region ID.
			"cassandra", // The product code.
			"hbase.aliyuncs.com"    // The endpoint, which is the domain name.
);

IAcsClient client = new DefaultAcsClient(profile);

CreateClusterRequest request = new CreateClusterRequest();
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");

request.setPeriodUnit("Year");             //You must specify this parameter if the billing method is Subscription.
request.setPeriod("12");                      //You must specify this parameter if the billing method is Subscription.
request.setAutoRenew("false");          //You must specify this parameter if the billing method is Subscription.
request.setAutoRenewPeriod("3");     //You must specify this parameter if the billing method is Subscription.
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 responses

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

3)You can call ResizeNodeCount to resize disks.

① 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.
			"hbase.aliyuncs.com"    // The endpoint, which is the domain name.
);

② Initialize the client.

IAcsClient client = new DefaultAcsClient(profile);

③ Construct the request object.

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

④ Initiate a call.

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

Sample requests

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
		"cn-hangzhou",    // The region ID.
		"cassandra", // The product code.
		"hbase.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 responses

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

4)You can call DeleteCluster to delete a cluster with a specified ID.

① 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.
			"hbase.aliyuncs.com"    // The endpoint, which is the domain name.
);

② Initialize the client.

IAcsClient client = new DefaultAcsClient(profile);

③ Construct the request object.

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

④ Initiate a call.

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

Sample requests

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
		"cn-hangzhou",    // The region ID.
		"cassandra", // The product code.
		"hbase.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 responses

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