You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2024.10.28 17:41:13首次发布时间: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 实例,配置如下:
    • 数据库版本号: 5.0
    • 可用内存:8GiB(即 8192MiB)
    • 分片数:2
    • 每个分片中的节点数:3
    • 在华北2(北京)地域下使用多可用区部署,其中主节点部署在可用区 A,2 个从节点均在可用区 B
    • 绑定至 acl-cq014ioqkm9zt5j**** 和 acl-cq015pp15v8buvl**** 两个白名单
    • 绑定 department:game 和 owner:Alice 两个标签
    • 计费类型:包年包月,预计使用 3 个月,且不开启自动续费
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",
            multi_az="enabled",
            configure_nodes=[
	            volcenginesdkredis.ConfigureNodeForCreateDBInstanceInput(az="cn-beijing-a"),
	            volcenginesdkredis.ConfigureNodeForCreateDBInstanceInput(az="cn-beijing-b"),
	            volcenginesdkredis.ConfigureNodeForCreateDBInstanceInput(az="cn-beijing-c"),
	          ], 
            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 实例,配置如下:
    • 数据库版本号: 5.0
    • 可用内存:8GiB(即 8192MiB)
    • 节点数:2
    • 在华北2(北京)地域下使用单可用区部署,且所有节点部署在可用区 A
    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",
                multi_az="disabled",
                configure_nodes=[
                    volcenginesdkredis.ConfigureNodeForCreateDBInstanceInput(az="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)