Python SDK example

更新时间:
复制 MD 格式

This topic shows how to use the Alibaba Cloud SDK for Python to call the DescribeDBClusters API to query ApsaraDB for ClickHouse instances and their basic information.

Review the OpenAPI documentation

Before calling an API, read the documentation to understand its parameters and required permissions. For more information, see the API overview.

Create a RAM user and grant permissions

If you have already created a RAM user with the required permissions, you can skip this section.

  1. Create a RAM user:

    1. Go to the Users page and click Add User.

    2. Set the Username to clickhouse-openapi-operator, and for Access Method, select Programmatic Access.

    3. Click OK to create the RAM user. Then, save the AccessKey ID and AccessKey Secret.

  2. Grant permissions:

    1. On the Users page, find the target RAM user and click Add Permissions in the Actions column.

    2. In the search box, enter AliyunClickHouse and select the AliyunClickHouseReadOnlyAccess policy.

      Note

      The AliyunClickHouseReadOnlyAccess policy grants read-only permissions for ApsaraDB for ClickHouse instances. You can also select the AliyunClickHouseFullAccess policy, which grants full permissions to query and modify instances, or create a custom policy. For more information, see Create a custom policy.

    3. Click OK to grant the permission.

Call the API

This topic demonstrates how to call the API using the Alibaba Cloud SDK for Python. The process is similar for SDKs in other languages. For more information, see SDK for ApsaraDB for ClickHouse.

Set up the Python environment

Download and install Python 3. If you already have Python installed, you can check your version by running python --version.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • For Linux and macOS, replace <access_key_id> and <access_key_secret> in the following commands with your AccessKey ID and AccessKey Secret. Then, run the commands in your terminal.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • For Windows, create the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables and set their values to your AccessKey ID and AccessKey Secret. You must then restart your computer for the changes to take effect.

Install dependencies

In your terminal, run the following command to install the required dependency.

pip install alibabacloud_clickhouse20191111

Download the sample code

  1. Go to the DescribeDBClusters API page in the OpenAPI Portal.

  2. On the Parameter Configuration tab on the left, set the required parameters. For this example, set the RegionId parameter to cn-hangzhou.

  3. On the SDK Sample tab on the right, select Python and click Download Project.

  4. Extract the downloaded package and navigate to the alibabacloud_sample directory.

Run the code

Run the following command:

python sample.py

The following output shows a sample response:

[
    LOG
]{
    "headers": {
        "date": "Thu, 27 Jul 2023 10:03:39 GMT",
        "content-type": "application/json;charset=utf-8",
        "content-length": "1665",
        "connection": "keep-alive",
        "keep-alive": "timeout=25",
        "vary": "Accept-Encoding",
        "access-control-allow-origin": "*",
        "access-control-expose-headers": "*",
        "x-acs-request-id": "F2F4B6CB-3C03-51A0-9300-F78BC94E****",
        "x-acs-trace-id": "4d9ceb1f6cb98086cd8333f0bb30****",
        "etag": "1YlaXvHZ3H5CjZic1Oeo****"
    },
    "statusCode": 200,
    "body": {
        "DBClusters": {
            "DBCluster": [
                {
                    "Category": "Basic",
                    "ResourceGroupId": "rg-acfmz7u4zzr****",
                    "ControlVersion": "v2",
                    "Port": "8123",
                    "DBClusterId": "cc-bp1084nnobip9****",
                    "DBClusterNetworkType": "vpc",
                    "DBNodeCount": 1,
                    "CommodityCode": "clickhouse_go_public_cn",
                    "Bid": "26842",
                    "LockReason": "",
                    "Tags": {
                        "Tag": [
                            {
                                "Value": "01",
                                "Key": "test"
                            }
                        ]
                    },
                    "DBNodeStorage": 100,
                    "IsExpired": false,
                    "StorageType": "CloudESSD",
                    "ZoneId": "cn-hangzhou-h",
                    "DBClusterStatus": "Running",
                    "CreateTime": "2023-07-27T07:47:19Z",
                    "VSwitchId": "vsw-bp11dxnc500olkyyf****",
                    "DBClusterDescription": "cc-bp1084nnobip9****",
                    "PayType": "Postpaid",
                    "LockMode": "Unlock",
                    "DBNodeClass": "S8",
                    "ScaleOutStatus": {
                        
                    },
                    "VpcId": "vpc-bp1pf7eciga85yo53****",
                    "VpcCloudInstanceId": "cc-bp1084nnobip9****",
                    "RegionId": "cn-hangzhou",
                    "ConnectionString": "cc-bp1084nnobip9****.clickhouse.ads.aliyuncs.com",
                    "ExpireTime": "",
                    "AliUid": 164882191396****
                },
                {
                    "Category": "Basic",
                    "ResourceGroupId": "rg-acfmz7u4zzr****",
                    "ControlVersion": "v2",
                    "Port": "8123",
                    "DBClusterId": "cc-bp166xfnay4rw****",
                    "DBClusterNetworkType": "vpc",
                    "DBNodeCount": 1,
                    "CommodityCode": "clickhouse_go_public_cn",
                    "Bid": "26842",
                    "LockReason": "",
                    "Tags": {
                        "Tag": [
                            {
                                "Value": "01",
                                "Key": "test"
                            }
                        ]
                    },
                    "DBNodeStorage": 100,
                    "IsExpired": false,
                    "StorageType": "CloudESSD",
                    "ZoneId": "cn-hangzhou-h",
                    "DBClusterStatus": "Running",
                    "CreateTime": "2023-07-27T06:53:54Z",
                    "VSwitchId": "vsw-bp1mbnyrjtf3ihga1****",
                    "DBClusterDescription": "cc-bp166xfnay4rw****",
                    "PayType": "Postpaid",
                    "LockMode": "Unlock",
                    "DBNodeClass": "S8",
                    "ScaleOutStatus": {
                        
                    },
                    "VpcId": "vpc-bp1ov7as4yvz4kxei****",
                    "VpcCloudInstanceId": "cc-bp166xfnay4rw****",
                    "RegionId": "cn-hangzhou",
                    "ConnectionString": "cc-bp166xfnay4rw****.clickhouse.ads.aliyuncs.com",
                    "ExpireTime": "",
                    "AliUid": 164882191396****
                }
            ]
        }
    }
}