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