You need to enable JavaScript to run this app.
文档中心
缓存数据库 Redis 版

缓存数据库 Redis 版

复制全文
下载 pdf
Java SDK
代码示例
复制全文
下载 pdf
代码示例
本文提供 Redis Java SDK 的代码示例供您参考。​
前提条件​
请求超时时间设置​
使用 Java SDK 调用 Redis 接口时,默认无超时时间设置。​
您可以在初始化客户端时,通过 setConnectTimeout(${超时时间})(默认单位:毫秒) 来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。​
例如,您可以通过如下代码将客户端的请求超时时间设置为 5000 毫秒。​
​
public static ApiClient apiClient = new ApiClient()​
.setCredentials(Credentials.getCredentials(ak,sk))​
.setRegion(region).setConnectTimeout(5000);​
​
创建 Redis 实例​
说明​
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance。​
​
示例 1
示例 2
​
使用如下示例代码创建 1 个启用分片集群的 Redis 实例,配置如下:​
  • 数据库版本号: 5.0。​
  • 可用内存:8 GiB(即 8192 MiB)。​
  • 分片数:2。​
  • 每个分片中的节点数:3。​
  • 在华北 2(北京)地域下使用多可用区部署,其中主节点部署在可用区 A,2 个从节点均在可用区 B。​
  • 绑定至 acl-cq014ioqkm9zt5j**** 和 acl-cq015pp15v8buvl**** 两个白名单。​
  • 绑定 department:game 和 owner:Alice 两个标签。​
  • 计费类型:包年包月,预计使用 3 个月,且不开启自动续费。​
​
import com.volcengine.ApiClient;​
import com.volcengine.ApiException;​
import com.volcengine.sign.Credentials;​
​
import com.volcengine.redis.RedisApi;​
import com.volcengine.redis.model.*;​
​
import java.util.Arrays;​
​
public class TestRedis {​
public static void main(String[] args) throws Exception {​
// Set AKSK authentication information and Region​
// Note the safety of the example code. Code leakage may lead to AKSK leakage, which poses a great security risk.​
String ak = "Your AK";​
String sk = "Your SK";​
String region = "Your Region";​
​
ApiClient apiClient = new ApiClient()​
.setCredentials(Credentials.getCredentials(ak, sk))​
.setRegion(region);​
RedisApi redisApi = new RedisApi(apiClient);​
​
// Create an interface input parameter​
CreateDBInstanceRequest request = new CreateDBInstanceRequest();​
// Set the interface input parameter according to the parameter description of the CreateDBInstance interface​
request.regionId("cn-beijing")​
.multiAZ("enabled")​
.configureNodes(Arrays.asList(new ConfigureNodeForCreateDBInstanceInput().AZ("cn-beijing-a"), new ConfigureNodeForCreateDBInstanceInput().AZ("cn-beijing-b"), new ConfigureNodeForCreateDBInstanceInput().AZ("cn-beijing-c")))​
.instanceName("java_sdk_shardedcluster_test")​
.shardedCluster(1)​
.password("Pwd@12****")​
.nodeNumber(3)​
.shardNumber(2)​
.shardCapacity(8192L)​
.engineVersion("5.0")​
.vpcId("vpc-30uf6q5ku7vgg7r2qr12n****")​
.subnetId("subnet-2gcljx616l5hc50ztyyzz****")​
.allowListIds(Arrays.asList("acl-cq0118bj1dah84v****", "acl-cq011jlt9rkap8n****"))​
.tags(Arrays.asList(new TagForCreateDBInstanceInput().key("department").value("game"), new TagForCreateDBInstanceInput().key("owner").value("Alice")))​
.chargeType("PrePaid")​
.purchaseMonths(3)​
.autoRenew(false);​
​
// Call the interface to create a Redis instance​
try {​
// Copy the code to run the example. Please print the API return value by yourself.​
CreateDBInstanceResponse response = redisApi.createDBInstance(request);​
System.out.println(response);​
} catch (ApiException e) {​
// Copy the code to run the example. Please print the API error information by yourself.​
System.out.println(e.getResponseBody());​
}​
}​
}​
​
​
​
查询 Redis 实例信息​
说明​
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 DescribeDBInstanceDetail。​
​
import com.volcengine.ApiClient;​
import com.volcengine.ApiException;​
import com.volcengine.sign.Credentials;​
​
import com.volcengine.redis.RedisApi;​
import com.volcengine.redis.model.*;​
​
import java.util.Arrays;​
​
public class TestRedis {​
public static void main(String[] args) throws Exception {​
// Set AKSK authentication information and Region​
// Note the safety of the example code. Code leakage may lead to AKSK leakage, which poses a great security risk.​
String ak = "Your AK";​
String sk = "Your SK";​
String region = "Your Region";​
​
ApiClient apiClient = new ApiClient()​
.setCredentials(Credentials.getCredentials(ak, sk))​
.setRegion(region);​
RedisApi redisApi = new RedisApi(apiClient);​
​
// Create an interface input parameter​
DescribeDBInstanceDetailRequest request = new DescribeDBInstanceDetailRequest(); ​
​
// Set the interface input parameter according to the parameter description of the DescribeDBInstanceDetail interface​
request.instanceId("redis-cn0212gzmh2xs****")​
​
// Call the interface to query the detailed information of the specified Redis instance​
try {​
// Copy the code to run the example. Please print the API error information by yourself.​
DescribeDBInstanceDetailResponse response = redisApi.describeDBInstanceDetail(request);​
System.out.println(response);​
} catch (ApiException e) {​
// Copy the code to run the example. Please print the API error information by yourself.​
System.out.println(e.getResponseBody());​
}​
}​
}​
​
​
最近更新时间:2026.02.27 17:24:35
这个页面对您有帮助吗?
有用
有用
无用
无用