全部产品
Search
文档中心

云数据库 Cassandra - 文档停止维护:Java SDK for Cassandra OpenAPI使用参考

更新时间:May 30, 2022

欢迎使用阿里云开发者工具套件(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集群信息

  1. 设置环境参数

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

    IAcsClient client = new DefaultAcsClient(profile);
  3. 构造请求对象

    DescribeClusterRequest request = new DescribeClusterRequest();
    request.setClusterId("cds-wz9bui92****697r");
  4. 请求调用

    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集群

  1. 设置环境参数

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

    IAcsClient client = new DefaultAcsClient(profile);
  3. 构造请求对象

    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@!");
  4. 请求调用

    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扩容数据中心的节点

  1. 设置环境参数

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

    IAcsClient client = new DefaultAcsClient(profile);
  3. 构造请求对象

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

    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集群

  1. 设置环境参数

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

    IAcsClient client = new DefaultAcsClient(profile);
  3. 构造请求对象

    DeleteClusterRequest request = new DeleteClusterRequest();
    request.setClusterId("cds-bp1hy2ipk****46k");
  4. 请求调用

    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"
}