You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2023.04.23 17:53:07首次发布时间:2022.08.08 17:46:46

本文介绍 Redis Python SDK 的代码示例供您参考。

前提条件

  1. 下载安装 SDK
  2. 初始化客户端

请求超时时间设置

使用 Python SDK 调用 Redis 接口时,默认无超时时间限制。
您可以在发起接口调用请求时,在代码的最后加上 _request_timeout 参数来指定请求的超时时间(单位:秒),若未在该时间内完成,请求即会终止并报超时错误。

例如,您可以在调用 DescribeDBInstanceDetail 接口时,通过如下代码将超时时间设置为 10 秒。

resp = api_instance.describe_db_instance_detail(volcenginesdkredis.DescribeDBInstanceDetailRequest(
    instance_id="redis-cn04giei8c91*****",
), _request_timeout=10)

创建 Redis 实例

说明

关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance

  • 示例 1
    使用如下示例代码创建 1 个启用分片集群的 Redis 实例,配置如下:
    • 实例所在地域和可用区:华北2(北京)-可用区A
    • 数据库版本号: 5.0
    • 可用内存:8GiB
    • 分片数:2
    • 每个分片中的节点数:2
import volcenginesdkredis
import volcenginesdkcore
from volcenginesdkcore.rest import ApiException
from pprint import pprint

if __name__ == "__main__":
    configuration = volcenginesdkcore.Configuration()

    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"

    try:
        api_instance = volcenginesdkredis.REDISApi(volcenginesdkcore.ApiClient(configuration))
        resp = api_instance.create_db_instance(volcenginesdkredis.CreateDBInstanceRequest(
            region_id="cn-beijing",
            zone_ids=["cn-beijing-a"],
            instance_name="python_sdk_shardedcluster_test",
            sharded_cluster=1,
            password="Pwd@12****",
            node_number=2,
            shard_number=2,
            shard_capacity=8192,
            engine_version="5.0",
            vpc_id="vpc-1g0ckv3hu4s1s8ibuxwqe****",
            subnet_id="subnet-1g0ckvr67hfr48ibuxxgr****",
        ))
        pprint(resp)
    except ApiException as e:
        print("Exception when calling REDISApi->create_db_instance: %s\n" % e)
  • 示例 2
    使用如下示例代码创建 1 个未启用分片集群的 Redis 实例,配置如下:
    • 实例所在地域和可用区:华北2(北京)-可用区A
    • 数据库版本号: 5.0
    • 可用内存:8GiB
    • 节点数:2
    import volcenginesdkredis
    import volcenginesdkcore
    from volcenginesdkcore.rest import ApiException
    from pprint import pprint
    
    if __name__ == "__main__":
        configuration = volcenginesdkcore.Configuration()
    
        configuration.ak = "Your AK"
        configuration.sk = "Your SK"
        configuration.region = "Your Region"
    
        try:
            api_instance = volcenginesdkredis.REDISApi(volcenginesdkcore.ApiClient(configuration))
            resp = api_instance.create_db_instance(volcenginesdkredis.CreateDBInstanceRequest(
                region_id="cn-beijing",
                zone_ids=["cn-beijing-a"],
                instance_name="python_sdk_test",
                sharded_cluster=0,
                password="Pwd@12****",
                node_number=2,
                shard_capacity=8192,
                engine_version="5.0",
                vpc_id="vpc-1g0ckv3hu4s1s8ibuxwqe****",
                subnet_id="subnet-1g0ckvr67hfr48ibuxxgr****",
            ))
            pprint(resp)
        except ApiException as e:
            print("Exception when calling REDISApi->create_db_instance: %s\n" % e)
    

查询 Redis 实例信息

说明

关于下述 SDK 请求中所使用参数的定义、取值范围等信息,请参见 DescribeDBInstanceDetail

import volcenginesdkredis
import volcenginesdkcore
from volcenginesdkcore.rest import ApiException
from pprint import pprint

if __name__ == "__main__":
    configuration = volcenginesdkcore.Configuration()

    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"

    try:
        api_instance = volcenginesdkredis.REDISApi(volcenginesdkcore.ApiClient(configuration))
        # 查询指定 Redis 实例信息
        resp = api_instance.describe_db_instance_detail(volcenginesdkredis.DescribeDBInstanceDetailRequest(
            instance_id="redis-cn04giei8c91*****",
        ))
        pprint(resp)
    except ApiException as e:
        print("Exception when calling REDISApi->describe_db_instance_detail: %s\n" % e)