All Products
Search
Document Center

ApsaraDB for ClickHouse:Use the SDK for Python

Last Updated:Jul 02, 2024

This topic describes how to use Alibaba Cloud SDK for Python to call the DescribeDBClusters operation of ApsaraDB for ClickHouse to query the ApsaraDB for ClickHouse clusters and cluster information.

View the API documentation

Before you call an API operation, we recommend that you read the documentation about the API operation to learn about the parameters and permissions that are required to call the API operation. For more information, see List of operations by function.

Create a RAM user and grant permissions to the RAM user

If you have created a RAM user and granted permissions to the user, you can skip this step.

  1. Create a RAM user.

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

    2. Set Logon Name to clickhouse-openapi-operator and Access Mode to OpenAPI Access.

    3. Click OK. After the RAM user is created, save the AccessKey ID and AccessKey secret of the user.

  2. Grant permissions to the RAM user.

    1. Go to the Users page and click Add Permissions in the Actions column of the RAM user that you created.

    2. In the Policy section, enter AliyunClickHouse in the text box and select the AliyunClickHouseReadOnlyAccess policy.

      Note

      The AliyunClickHouseReadOnlyAccess policy can grant the RAM user permissions to query ApsaraDB for ClickHouse clusters. You can also select the AliyunClickHouseFullAccess policy that can grant the RAM user permissions to query and modify ApsaraDB for ClickHouse clusters or customize a policy. For more information, see Create custom policies.

    3. Click OK.

Call an API operation

This section describes how to use Alibaba Cloud SDK for Python to call an API operation. You can use SDKs for other programming languages in a similar way. For more information, see ApsaraDB for ClickHouse SDK.

Prepare the Python environment

Download and install Python 3. If you have installed Python, you can run the python --version command to view the Python version.

Configure environment variables

This section describes how to set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your operating system.

  • If you use a Linux or macOS operating system, replace <access_key_id> and <access_key_secret> in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands in sequence on your device.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • If you use a Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables and set the variables to your AccessKey ID and AccessKey secret. Then, restart the Windows operating system.

Install dependencies

Run the following commands in sequence on your device to install dependencies:

pip install alibabacloud_clickhouse20191111

Download the sample code

  1. Go to the DescribeDBClusters page in OpenAPI Explorer where you can debug APIs.

  2. On the Parameters tab, configure the required parameters. In this example, RegionId is set to cn-hangzhou.

  3. On the right-side SDK Sample Code tab, select Python for Languages and click Download Project to download the sample code package.

  4. Decompress the package on your device and go to the alibabacloud_sample directory.

Run the sample code

Run the following command:

python sample.py

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****
                }
            ]
        }
    }
}