JedisCluster : Scan For Key does not work

天涯浪子 提交于 2019-12-11 16:24:13

问题


I was trying to scan for particular key stored in 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.

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"));
             }
         }

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

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:


Part 1:

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

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.

Part 2:

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.

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}.

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




回答2:


Scan works on only a single redis, and does not scan in all nodes in the redis cluster. While scanning, you should always use Jedis instance and not JedisCluster.

So, ideally for scanning all keys of a particular pattern in a redis cluster, you should do the following.

for <Ip:port> in List<Ip:port> {
      Connect to Redis using Jedis and get Jedis object.
      scan using Jedis.
      do stuff with result.
}


来源:https://stackoverflow.com/questions/49732535/jediscluster-scan-for-key-does-not-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!