Python SDK example

更新时间:
复制 MD 格式

This guide shows you how to call the DescribeInstances operation using ApsaraDB for HBase SDK for Python to list your ApsaraDB for HBase clusters and retrieve their basic information.

Prerequisites

Before you begin, ensure that you have:

  • Python 3 installed. To check your version, run python --version.

  • An Alibaba Cloud account with access to the RAM console.

Review the API documentation

Before calling any operation, read the API overview to understand the required parameters and permissions.

Set up a RAM user

Skip this section if you already have a RAM user with the appropriate permissions.

Create a RAM user

  1. Log on to the RAM console. In the left-side navigation pane, choose Identities > Users.

  2. Click Create User.

  3. Set Logon Name to hbase-openapi-operator and Access Mode to Using permanent AccessKey to access.

  4. Click OK. The RAM user is created, and the system generates an AccessKey ID and an AccessKey secret. Save both values immediately — the AccessKey secret is only shown once.

Grant permissions to the RAM user

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

  2. In the Add Permissions panel, search for AliyunHBase and select the AliyunHBaseReadOnlyAccess policy.

    AliyunHBaseReadOnlyAccess grants read-only access to ApsaraDB for HBase clusters. To also modify clusters, select AliyunHBaseFullAccess instead, or customize a RAM policy.
  3. Click OK.

Call the DescribeInstances operation

The following steps show how to use ApsaraDB for HBase SDK for Python to call DescribeInstances. For SDKs in other languages, see ApsaraDB for HBase SDKs.

Set environment variables

Store your credentials as environment variables rather than hardcoding them in source code.

Linux or macOS

export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>

Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user.

Windows

Create a system environment variable file, add ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET, and set their values to your AccessKey ID and AccessKey secret. Restart Windows for the changes to take effect.

Install dependencies

Run the following commands to install the required packages:

pip install alibabacloud_credentials    # Credential management
pip install alibabacloud_hbase20190101  # ApsaraDB for HBase SDK (API version 2019-01-01)
pip install alibabacloud_tea_console    # Console output utilities for SDK responses

Download and run the sample code

  1. Open the DescribeInstances page on the OpenAPI Portal.

  2. On the Parameters tab, enter cn-hangzhou for RegionId.

  3. Click the SDK Sample Code tab, select Python for the language, and click Download Project.

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

  5. Run the sample:

    python sample.py

The sample code reads your credentials from environment variables, creates an SDK client, and calls DescribeInstances. A successful response looks similar to the following:

[
    LOG
] {
    "headers": {
        "date": "Mon, 24 Jul 2023 09:56:30 GMT",
        "content-type": "application/json;charset=utf-8",
        "content-length": "943",
        "connection": "keep-alive",
        "keep-alive": "timeout=25",
        "access-control-allow-origin": "*",
        "access-control-expose-headers": "*",
        "x-acs-request-id": "C20BAA8E-C543-58F1-A611-9C812336****",
        "x-acs-trace-id": "63ca81587ddddeb0d47443462b3d****",
        "etag": "919xrdSHGE5LzH4To3ra****"
    },
    "body": {
        "Instances": {
            "Instance": [
                {
                    "BackupStatus": "open",
                    "ClusterId": "ld-bp1st3v9780tk****",
                    "ClusterName": "Test",
                    "ClusterType": "cluster",
                    "ColdStorageStatus": "open",
                    "CoreDiskCount": 4,
                    "CoreDiskSize": 100,
                    "CoreDiskType": "cloud_ssd",
                    "CoreInstanceType": "hbase.sn2.large",
                    "CoreNodeCount": 4,
                    "CreatedTime": "2023-07-24T17:44:54",
                    "CreatedTimeUTC": "2023-07-24T09:44:54Z",
                    "Engine": "hbaseue",
                    "InstanceId": "ld-bp1st3v9780tk****",
                    "InstanceName": "Test",
                    "IsDeletionProtection": false,
                    "IsHa": true,
                    "MajorVersion": "2.0",
                    "MasterDiskSize": 0,
                    "MasterDiskType": "",
                    "MasterInstanceType": "hbase.sn1.large",
                    "MasterNodeCount": 2,
                    "ModuleId": 0,
                    "NetworkType": "VPC",
                    "PayType": "Postpaid",
                    "RegionId": "cn-hangzhou",
                    "ResourceGroupId": "rg-acfmz7u4zzr****",
                    "Status": "ACTIVATION",
                    "Tags": {
                        "Tag": []
                    },
                    "VpcId": "vpc-bp16crlc5o2dmwi8p****",
                    "VswitchId": "vsw-bp1sueafuqh4ly8ae****",
                    "ZoneId": "cn-hangzhou-f"
                }
            ]
        },
        "PageNumber": 1,
        "PageSize": 10,
        "RequestId": "C20BAA8E-C543-58F1-A611-9C812336****",
        "TotalCount": 1
    }
}

What's next