当前位置:   article > 正文

java通过scan进行redis key遍历_jediscluster only supports scan commands with matc

jediscluster only supports scan commands with match patterns containing hash

遍历所有redis key,删除满足条件的field

  1. import xxx.utils.RedisClusterUtil;
  2. import org.checkerframework.checker.units.qual.A;
  3. import redis.clients.jedis.JedisCluster;
  4. import java.util.*;
  5. //add by xq
  6. import java.util.List;
  7. import redis.clients.jedis.Jedis;
  8. import redis.clients.jedis.ScanParams;
  9. import redis.clients.jedis.ScanResult;
  10. public class ttt {
  11. private static JedisCluster jedis = RedisClusterUtil.getJedis();
  12. public static void main(String[] args) {
  13. // Map<String, String> map = jedis.hgetAll("xxx");
  14. // if(map.containsKey("None")){
  15. // jedis.hdel("xxx", "None");
  16. // }
  17. // System.out.println(map);
  18. String keyPrefix = "test*";
  19. ScanParams params = new ScanParams()
  20. .match(keyPrefix)
  21. .count(1000);
  22. jedis.getClusterNodes().values().stream().forEach(pool -> {
  23. boolean done = false;
  24. String cur = "0";
  25. try (Jedis jedisNode = pool.getResource()) {
  26. while (!done) {
  27. ScanResult<String> scanResult = jedisNode.scan(cur, params);
  28. List<String> list = scanResult.getResult();
  29. for(String str:list){
  30. Map<String, String> map = jedis.hgetAll(str);
  31. if(map.containsKey("None")){
  32. jedis.hdel(str, "None");
  33. }
  34. }
  35. cur = scanResult.getCursor();
  36. if (cur.equals("0")) {
  37. System.out.println("ok");
  38. done = true;
  39. }
  40. try {
  41. Thread.sleep(1000L);
  42. System.out.println("sleep");
  43. } catch (InterruptedException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. });
  49. // String cursor = "0";
  50. // String key = "myhonor_post_rec_*";
  51. // ScanParams scanParams = new ScanParams();
  52. // scanParams.match(key);// 匹配以* 为前缀的 key
  53. // scanParams.count(1);
  54. // List<String> keylist = new ArrayList<>();
  55. //
  56. // while (true) {
  57. // // 使用scan命令获取500条数据,使用cursor游标记录位置,下次循环使用
  58. // ScanResult scanResult = jedis.scan(cursor, scanParams);
  59. // List<String> list = scanResult.getResult();
  60. // if(list != null && (!list.isEmpty())){
  61. // keylist.addAll(list);
  62. // }
  63. // cursor = scanResult.getCursor();// 返回0 说明遍历完成
  64. // if ("0".equals(cursor)) {
  65. // break;
  66. // }
  67. // try {
  68. // Thread.sleep(1000L);
  69. // } catch (InterruptedException e) {
  70. // e.printStackTrace();
  71. // }
  72. // }
  73. // System.out.println("ok");
  74. // System.out.println("keylist:"+keylist);
  75. // Set s = jedis.keys("*");
  76. //
  77. // for (Object o : s) {
  78. // String key = (String) o;
  79. // String value = jedis.get(key);
  80. // System.out.println(key + value);
  81. // }
  82. }
  83. }

几个小问题:

(1)不能用.keys

用keys 命令可以返回redis中所有的以test开头的key。但是会引起问题就是卡顿,因为redis是单线程执行,而keys是要拿所有的key来做比对。于是有了scan来代替keys解决卡顿问题。

(2)JedisCluster only supports SCAN commands with MATCH patterns containing hash-tags

问题解决:

 

redis - Why am I getting a missing hash-tags error when I try to run JedisCluster.scan() using a match pattern? - Stack Overflow

(3)如果是单节点的话,则用代码块中注释掉的代码即可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/240386
推荐阅读
相关标签
  

闽ICP备14008679号