JedisCluster:扫描密钥不起作用 [英] JedisCluster : Scan For Key does not work

查看:98
本文介绍了JedisCluster:扫描密钥不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扫描存储在 JedisCluster.

String product = "MMATest";

String redisServer = "mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com:6379,mycachecluster.eaogs8.0002.usw2.cache.amazonaws.com:6379";
    Set<HostAndPort> jedisClusterNode = new HashSet<>();
    String[] serversArray = redisServer.split(";");
    for (String aServersArray : serversArray) {
        jedisClusterNode.add(new HostAndPort(aServersArray.split(":")[0],
                Integer.valueOf(aServersArray.split(":")[1])));
    }
    JedisCluster jedisCluster = new JedisCluster(jedisClusterNode,
            buildPoolConfig());

ScanParams params = new ScanParams();
StringJoiner joiner = new StringJoiner("");
joiner.add("{");
joiner.add("Image-"+product);
joiner.add("}");
params.match(joiner.toString()).count(100);
System.out.println(joiner.toString());
ScanResult<String> scanResult = null;
String scanMarker = "0";
 do {
     scanResult = jedisCluster.scan(ScanParams.SCAN_POINTER_START, params);
      System.out.println(scanResult.getResult());
             System.out.println(!(scanResult.getResult() == null || scanResult.getResult().isEmpty()));

         } while (!scanMarker.equals("0"));

        ScanResult<Map.Entry<String,String>> scan = jedisCluster.hscan(joiner.toString(), ScanParams.SCAN_POINTER_START);
        System.out.println(scan.getResult());

这里我得到的是空值.但是有一个值存储在集群节点中.

Here I was getting the null value. But there is a value stored in the cluster node.

但是如果我尝试扫描每个 Jedis 池,我会得到结果.

But if I try to scan the each Jedis pool, I will get the result.

  Map<String, JedisPool> jedisPools = jedisCluster.getClusterNodes();
         Set<String>jedisPoolList = jedisPools.keySet();
         System.out.println(jedisPools.keySet());
         System.out.println(jedisPools.values());
         System.out.println(jedisPools.size());
         for (String hostAndPort : jedisPoolList) {
             String[] parts = hostAndPort.split(":");
             String host = parts[0];
             int port = Integer.valueOf(parts[1]);
             try (Jedis jedis = new Jedis(host, port)) {
                 ScanParams params = new ScanParams().match("Image-"+product).count(100);
                 String scanMarker = "0";
                 ScanResult<String> results = null;

                 do {
                     results = jedis.scan(scanMarker, params);
                     System.out.println("XXXX"+results.getResult());
                     System.out.println("XXXX"+!(results.getResult() == null || results.getResult().isEmpty()));

                 } while (!scanMarker.equals("0"));
             }
         }

为什么 JedisCluster 扫描方法没有给出正确的结果?我该如何解决这个问题?

Why the JedisCluster scan method does not give the proper result? How do I solve this issue?

注意:我可以使用 jedisCluster.exists(key) 来检查密钥是否存在.但是我需要使用扫描,因为我可以对 Jedis 和 JedisCluster 使用相同的界面.

Note: I can use jedisCluster.exists(key) to check the key's existence. But I need to use scan as I can use the same interface to both Jedis and JedisCluster.

推荐答案

第 1 部分:

在您使用 Jedis 的实现中,您正在匹配 Image-MMATest.当您获取数据时,它证明数据是由该密钥存储的.

In your implementation with Jedis, you are matching Image-MMATest. As you are getting data, it proves that data is stored by that key.

但是,在 JedisCluster 实现中,您将 "Image-"+product 用花括号括起来.这意味着您实际上正在匹配 {Image-MMATest}.您没有获得任何数据,因为该密钥未存储任何数据.

However, with JedisCluster implementation, you have enclosed "Image-"+product with curly braces. Which means you are actually matching {Image-MMATest}. Your are not getting any data, because no data is stored by that key.

第 2 部分:

在 JedisCluster 中,scan 支持仅限于 Redis 哈希标签模式.Image-MMATest 不是 Redis 哈希标签兼容模式.因此,您将无法使用 JedisCluster 的 scan 获取 Image-MMATest.

In JedisCluster, scan support is limited to Redis hash tag pattern. Image-MMATest is not Redis hash tag compliant pattern. Because of that you won't be able to get Image-MMATest using scan of JedisCluster.

要通过 JedisCluster 的 scan 获取数据,您必须根据符合 Redis 哈希标签模式的键存储数据,例如{Image-MMATest}.

To get data by scan of JedisCluster, you'd have to store data against a key which is Redis hash tag compliant pattern, e.g. {Image-MMATest}.

有关 Redis 哈希标签的更多详细信息,请参阅本文档.

More details about Redis hash tag can be found in this documentation.

这篇关于JedisCluster:扫描密钥不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆