1

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.

1
  • Which Jedis version are you using? Please add shortest reproducible Java code that you think is not working. Commented Dec 10, 2021 at 18:59

2 Answers 2

2

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"); } 
Sign up to request clarification or add additional context in comments.

6 Comments

This is to check a particular key. Again in that case I need to loop through the entire List. I am using mget and getting the list and returning the list. In somewhere else I am looping through the list and checking if its null. How to do that.
I've updated my answer. You can remove all null values before returning the values
This will remove if the value is exactly null. But it is returning (nil) and and not a null . There is my problem. How do I do a null check on that. Please see the output from REDIS in my question.
Actually I am mapping the key-list and value-list in a hash map. And on the value list I am doing some operations. There I need to put a null check.
I've just run some codes again on my PC and it returns null in java instead of nil if I try to get some values that don't exist in Redis via mget. It shows nil in the console though.
|
0

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

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

1 Comment

I am not using redisson

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.