```private static void deleteAllKeys() {JedisCluster jedisCluster = new JedisCluster(new HostAndPort(127.0.0.1, 6379));try {// 获取集群中所有的节点CollectionJedisPool jedisPools = jedisCluster.get...
```
private static void deleteAllKeys() {
JedisCluster jedisCluster = new JedisCluster(new HostAndPort("127.0.0.1", 6379));
try {
// 获取集群中所有的节点
Collection<JedisPool> jedisPools = jedisCluster.getClusterNodes().values();
boolean isSuccess = false;
// 遍历所有的节点,获取每个节点上对应数据匹配的结果,并删除
for (JedisPool pool : jedisPools) {
Jedis jedis = null;
try {
jedis = pool.getResource();
Set<String> keys = jedis.keys("*");
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
jedis.del(key);
}
pool.returnResource(jedis);
isSuccess = true;
} catch (JedisMovedDataException ignore) {
isSuccess = false;
pool.returnBrokenResource(jedis);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
isSuccess = false;
System.out.println("Error message: " + e.getMessage());
}
}
if (isSuccess) {
System.out.println("Complete!");
} else {
System.out.println("Error!");
}
jedisCluster.close();
} catch (Exception e) {
try {
jedisCluster.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} }
```
沃梦达教程
本文标题为:redis集群删除所有数据
基础教程推荐
猜你喜欢
- Python常见库matplotlib学习笔记之多个子图绘图 2023-07-27
- 如何将excel表格数据导入postgresql数据库 2023-07-20
- SQLServer 清理日志的实现 2023-07-29
- 关于MySQL中explain工具的使用 2023-07-27
- Mysql主从三种复制模式(异步复制,半同步复制,组复 2022-09-01
- 【Redis】数据持久化 2023-09-12
- python中pandas库的iloc函数用法解析 2023-07-28
- Mysql查询所有表和字段信息的方法 2023-07-26
- Sql Server Management Studio连接Mysql的实现步骤 2023-07-29
- Redis如何实现延迟队列 2023-07-13