本文介紹如何使用阿里雲Python語言SDK開發包,調用雲資料庫ClickHouse的DescribeDBClusters介面(查詢執行個體列表及其基本資料)。
查看OpenAPI文檔
在調用OpenAPI前,建議您先閱讀對應介面文檔,瞭解、學習調用該介面所需要的參數及許可權等,更多資訊請參見API概覽。
建立RAM使用者並完成授權
如您已建立RAM使用者且已完成授權,可跳過此步驟。
建立RAM使用者:
訪問RAM使用者列表,單擊建立使用者。
設定登入名稱稱為clickhouse-openapi-operator,選擇訪問方式為OpenAPI 呼叫訪問。
單擊確定,建立RAM使用者並儲存AccessKey ID與AccessKey Secret資訊。
完成授權:
調用OpenAPI
本文介紹阿里雲Python語言SDK方式調用OpenAPI,其他語言SDK的用法也類似,更多資訊請參見雲資料庫ClickHouse版SDK。
準備Python環境
下載並安裝Python 3。如您已安裝Python,查看Python版本的命令樣本:python --version
。
配置環境變數
配置環境變數ALIBABA_CLOUD_ACCESS_KEY_ID
和ALIBABA_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_ID
和ALIBABA_CLOUD_ACCESS_KEY_SECRET
,並寫入已準備好的AccessKey ID和AccessKey Secret。然後重啟Windows系統。
安裝依賴
在終端裝置中,逐條運行如下命令,安裝環境依賴。
pip install alibabacloud_clickhouse20191111
下載範例程式碼
訪問DescribeDBClusters,進入該介面在OpenAPI門戶的調試地址。
在左側的參數配置頁簽,填寫需要的參數資訊,本樣本需要在RegionId參數中輸入cn-hangzhou。
在右側的SDK樣本頁簽,選擇Python語言,單擊下載完整工程按鈕,下載範例程式碼包。
本地解壓並進入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****
}
]
}
}
}