歡迎使用阿里雲開發人員工具套件(Alibaba Cloud SDK for Java),此部分為OpenAPI的SDK文檔說明。Alibaba Cloud SDK for Java讓您不用複雜編程即可對雲資料庫Cassandra執行個體進行管理,包括建立、升級、刪除執行個體、白名單、標籤管理等,有別於訪問Cassandra執行個體的SDK。本教程介紹如何安裝並開始使用Alibaba Cloud SDK for Java。
前提條件
已建立AccessKey,具體請參見擷取AccessKey。
線上調試和產生SDK樣本
OpenAPI Explorer提供線上調用雲產品API、動態產生SDK範例程式碼和快速檢索介面等功能,能顯著降低使用API的難度,推薦您使用。進入頁面後,選擇Cassandra產品,系統提供了介面列表,並且每個介面提供了各個語言的調試demo。
下載並安裝Alibaba Cloud SDK for Java
您可以通過添加Maven依賴或者匯入Jar檔案來安裝Alibaba Cloud SDK for Java,詳細說明請參見安裝Alibaba Cloud SDK for Java。
說明
通過Maven下載SDK包時,請下載最新版本。
樣本demo
調用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"
}
]
}
}
}
調用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"
}
調用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"
}
調用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"
}