Python调用SDK示例

本文介绍如何使用阿里云Python语言SDK开发包,调用云数据库ClickHouse的DescribeDBClusters接口(查询实例列表及其基本信息)。

查看OpenAPI文档

在调用OpenAPI前,建议您先阅读对应接口文档,了解、学习调用该接口所需要的参数及权限等,更多信息请参见API概览

创建RAM用户并完成授权

如您已创建RAM用户且已完成授权,可跳过此步骤。

  1. 创建RAM用户:

    1. 访问RAM用户列表,单击创建用户

    2. 设置登录名称clickhouse-openapi-operator,选择访问方式OpenAPI 调用访问

    3. 单击确定,创建RAM用户并保存AccessKey ID与AccessKey Secret信息。

  2. 完成授权:

    1. 访问RAM用户列表,单击目标RAM用户操作列的添加权限

    2. 在文本框中搜索AliyunClickHouse,选择AliyunClickHouseReadOnlyAccess策略。

      说明

      AliyunClickHouseReadOnlyAccess策略拥有查询ClickHouse实例的操作权限,您也可以按需选择AliyunClickHouseFullAccess权限(具有查询、修改ClickHouse实例的完全控制权限)或进行自定义策略,更多信息请参见创建自定义权限策略

    3. 单击确定,完成授权操作。

调用OpenAPI

本文介绍阿里云Python语言SDK方式调用OpenAPI,其他语言SDK的用法也类似,更多信息请参见云数据库ClickHouse版SDK

准备Python环境

下载并安装Python 3。如您已安装Python,查看Python版本的命令示例:python --version

配置环境变量

配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET

  • Linux和macOS系统:将以下命令中的<access_key_id><access_key_secret>替换为您自己的AccessKey ID和AccessKey Secret,然后在终端设备中逐条运行。

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows系统:新建环境变量文件,添加环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET,并写入已准备好的AccessKey ID和AccessKey Secret。然后重启Windows系统。

安装依赖

在终端设备中,逐条运行如下命令,安装环境依赖。

pip install alibabacloud_clickhouse20191111

下载示例代码

  1. 访问DescribeDBClusters,进入该接口在OpenAPI门户的调试地址。

  2. 在左侧的参数配置页签,填写需要的参数信息,本示例需要在RegionId参数中输入cn-hangzhou

  3. 在右侧的SDK示例页签,选择Python语言,单击下载完整工程按钮,下载示例代码包。

  4. 本地解压并进入alibabacloud_sample目录。

运行代码

执行以下命令。

python sample.py

返回示例如下。

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