7

I use spring's RedisTemplate.

I have a hash in redis.

I want to query redis, using the template, in order to get all entries whose key in a certain set of keys.

I noticed the method:

Map<Object, Object> entries = redisTemplate.opsForHash().multiGet(HASH_NAME, keySet);

But, unfortunately this method returns only the values, without the matching keys.

I could also do it like this:

Map<Object, Object> entries = redisTemplate.opsForHash().entries(HASH_NAME);
for(Object key : keySet){
 System.out.println(entries.get(key));
}

But this means I will get all entries from redis, to my local machine, and iterate them.

Don't I have a better way of doing this?

asked Oct 21, 2015 at 11:47

4 Answers 4

8

If you're using RedisTemplate you could use opsForHash().entries(H key) to get all the hash, (keys and values).

For example...

Map messageGroupMetadata = redisTemplate.opsForHash().entries(H key);

Besides it's necesary to set RedisTemplate serializers to complete this operation.

redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
Madbreaks
19.6k7 gold badges62 silver badges75 bronze badges
answered Feb 5, 2016 at 10:33
Sign up to request clarification or add additional context in comments.

Comments

6

From the documentation of HMGET:

Array reply: list of values associated with the given fields, in the same order as they are requested.

And from the documentation for Spring Redis Hash multiGet, you can see it returns a list (not a map), which should be in the same order as the keys you sent.

Example code:

List<Object> values = redisTemplate.opsForHash().multiGet(HASH_NAME, keySet);
int i = 0;
for (String k: keySet) {
 // k is the key
 // and here is the value corresponding to the key k
 Object value = values.get(i++);
}
answered Oct 21, 2015 at 16:24

Comments

0

I also had same issue. Even if the method description says multiget should return the data in the same sequence as input, it does not. It returns it in the way a Map would return. I wanted to open a bug for this but I don't see any option to do so on spring.io.

answered Aug 22, 2018 at 18:40

Comments

-1

Not sure about spring, but Redis has a native solution for this need. You can use SORT command:

SORT index_set BY nosort get # get *->hash_field1 get *->hash_field2 ...

The first get # will return the set element, which in your case is the hash key name.

answered Oct 22, 2015 at 7:07

1 Comment

This is interesting but doesn't address op's question.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.