Home > database >  How to check null value from REDIS MGET via Jedis client in java
How to check null value from REDIS MGET via Jedis client in java

Time:12-10

I have some keys in redis. I can do a MGET and fetch the values. But when the key is not present I get a (nil) from redis. But how to check that in java ? I am checking for null but it is not working.

127.0.0.1:6379> MGET key1 key2 key3 key4
1) "Hello"
2) "World"
3) "Hi"
4) "Hi-World"
127.0.0.1:6379> MGET key1 key2 key3 key5 key4
1) "Hello"
2) "World"
3) "Hi"
4) (nil)
5) "Hi-World"
127.0.0.1:6379>

In my java class actually I am mapping the key-list and value-list from MGET in a hash map. And on the value list I am doing some operations. There I need to put a null check. But simple java null is not working.

CodePudding user response:

If you have object as a map then you can access it like this

System.out.println(redissonClient.getMap("object").get("key")==null);

CodePudding user response:

You can always call exists to check if a key exists in Redis. To check with java, first, create a client and call the appropriate method.

Jedis jedis = new Jedis(host, port);
List<String> values = jedis.mget(keys); //get all values for the given keys
values.removeAll(Collections.singletonList(null)); //remove all null values
if(jedis.exists(key)) { //check if a particular key exists or not
     System.out.println(key   " exists");
}
  • Related