欢迎使用阿里云开发者工具套件(Alibaba Cloud SDK for Java),此为OpenApi的SDK文档说明。Alibaba Cloud SDK for Java让您不用复杂编程即可对云数据库Cassandra实例进行管理,包括创建、升级、删除实例、白名单、标签管理等,详情查看《API参考》,注:其有别于访问Cassandra实例的sdk。本教程介绍如何安装并开始使用Alibaba Cloud SDK for Java。

1、创建AccessKey

在线调试和生成SDK示例

OpenAPI Explorer提供在线调用云产品API、动态生成 SDK示例代码和快速检索接口等功能,能显著降低使用API的难度,推荐您使用,进入页面后,选择Cassandra产品,提供了接口列表,且包含了各个语言的调试demo,供大家参考。

1、安装下载Alibaba Cloud SDK for Java

注意:通过maven引用sdk包时,请下载最新的版本。

2、示例demo

1) 调用DescribeClusterRequest获取Cassandra集群信息

① 设置环境参数

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
		"LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
// 以下非必选项,可默认
DefaultProfile.addEndpoint(
			"cn-hangzhou",    // 地域ID
			"cassandra",    // 产品编码
			"cassandra.aliyuncs.com"    // 接入地址,即域名
);

② 初始化客户端

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();
}

参考示例

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
		"cn-hangzhou",    // 地域ID
		"cassandra",    // 产品编码
		"cassandra.aliyuncs.com"    // 接入地址,即域名
);
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();
}

响应示例

{
  "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) 调用CreateCluster创建Cassandra集群

① 设置环境参数

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
		"LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
// 以下非必选项,可默认
DefaultProfile.addEndpoint(
			"cn-hangzhou",    // 地域ID
			"cassandra",    // 产品编码
			"cassandra.aliyuncs.com"    // 接入地址,即域名
);

② 初始化客户端

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");//若付费类型为Subscription则该参数必须传入。
request.setPeriod("12");//若付费类型为Subscription则该参数必须传入。
request.setAutoRenew("false");//若付费类型为Subscription则该参数必须传入。
request.setAutoRenewPeriod("3");//若付费类型为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();
}

参考示例

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
		"cn-hangzhou",    // 地域ID
		"cassandra",    // 产品编码
		"cassandra.aliyuncs.com"    // 接入地址,即域名
);
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");//若付费类型为Subscription则该参数必须传入。
request.setPeriod("12");//若付费类型为Subscription则该参数必须传入。
request.setAutoRenew("false");//若付费类型为Subscription则该参数必须传入。
request.setAutoRenewPeriod("3");//若付费类型为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();
}

响应示例

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

3) 调用ResizeNodeCount扩容数据中心的节点

① 设置环境参数

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
		"LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
// 以下非必选项,可默认
DefaultProfile.addEndpoint(
			"cn-hangzhou",    // 地域ID
			"cassandra",    // 产品编码
			"cassandra.aliyuncs.com"    // 接入地址,即域名
);

② 初始化客户端

IAcsClient client = new DefaultAcsClient(profile);

③ 构造请求对象

ResizeNodeCountRequest request = new ResizeNodeCountRequest();
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();
}

参考示例

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
		"cn-hangzhou",    // 地域ID
		"cassandra",    // 产品编码
		"cassandra.aliyuncs.com"    // 接入地址,即域名
);

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();
}

响应示例

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

4) 调用DeleteCluster删除Cassandra集群

① 设置环境参数

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
		"LTAI4FnvzUftE**********", "PxPzgelK1XCkWBXW***********");
// 以下非必选项,可默认
DefaultProfile.addEndpoint(
			"cn-hangzhou",    // 地域ID
			"cassandra",    // 产品编码
			"cassandra.aliyuncs.com"    // 接入地址,即域名
);

② 初始化客户端

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();
}

参考示例

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "ak******************", "akSecret********************");
DefaultProfile.addEndpoint(
		"cn-hangzhou",    // 地域ID
		"cassandra",    // 产品编码
		"cassandra.aliyuncs.com"    // 接入地址,即域名
);

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();
}

响应示例

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