Any plans to include a command to get multiple keys/values from hash?

1,554 views
Skip to first unread message

chen foo

unread,
Aug 28, 2014, 4:20:05 AM8/28/14
to redi...@googlegroups.com
So far it seems that to get multiple keys/values from hash, one can either do multi/exec or do sort/get. Is there any plans to include a command?

Jan-Erik Rediger

unread,
Aug 28, 2014, 5:13:54 AM8/28/14
to redi...@googlegroups.com
There's HMGET. http://redis.io/commands/hmget

On Thu, Aug 28, 2014 at 01:20:04AM -0700, chen foo wrote:
> So far it seems that to get multiple keys/values from hash, one can either
> do multi/exec or do sort/get. Is there any plans to include a command?
>
> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
> To post to this group, send email to redi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/redis-db.
> For more options, visit https://groups.google.com/d/optout.

chen foo

unread,
Aug 28, 2014, 10:19:36 AM8/28/14
to redi...@googlegroups.com, jan...@fnordig.de
hmget can only get multiple values of a key, it is not able to get multiple keys

Itamar Haber

unread,
Aug 28, 2014, 10:24:40 AM8/28/14
to redi...@googlegroups.com, jan...@fnordig.de

So it appears that you're looking for the equivalent of mget for hashes. This isn't a part of Redis yet and as to whether it will be added... What's the use case?

In any case, another way to go about this is of course Lua.

Marc Gravell

unread,
Aug 28, 2014, 10:25:06 AM8/28/14
to redi...@googlegroups.com
well, you could use eval, or... just issue a whole load of hmget/hgetall. If you don't absolutely positively need them all to be from a single point in time, multi/exec is probably overkill - many client libraries allow you to pipeline commands so you aren't paying latency per key.

Marc
--
Regards,

Marc

Jan-Erik Rediger

unread,
Aug 28, 2014, 10:33:15 AM8/28/14
to chen foo, redi...@googlegroups.com
Would have been easier to help you if you made this more clear.

So what you want is the following, right?

MULTI
HMGET key1 field1 field2
HMGET key2 field1 field2
EXEC

Combined with pipelining this should be pretty effective already.

(sorry chen, I sent the first not to the mailing list)

On Thu, Aug 28, 2014 at 07:19:35AM -0700, chen foo wrote:
> hmget can only get multiple values of a key, it is not able to get multiple
> keys
>
> On Thursday, August 28, 2014 5:13:54 PM UTC+8, janerik wrote:
> >
> > There's HMGET. http://redis.io/commands/hmget
> >
> > On Thu, Aug 28, 2014 at 01:20:04AM -0700, chen foo wrote:
> > > So far it seems that to get multiple keys/values from hash, one can
> > either
> > > do multi/exec or do sort/get. Is there any plans to include a command?
> > >
> > > --
> > > You received this message because you are subscribed to the Google
> > Groups "Redis DB" group.
> > > To unsubscribe from this group and stop receiving emails from it, send
> > an email to redis-db+u...@googlegroups.com <javascript:>.
> > > To post to this group, send email to redi...@googlegroups.com
> > <javascript:>.

chen foo

unread,
Aug 28, 2014, 10:55:12 AM8/28/14
to redi...@googlegroups.com, chenf...@gmail.com, jan...@fnordig.de
Itarmar gets the idea, a command equivalent to mget for hashes. Maybe the topic should be "a command to get multiple hashes". Sorry for any confusion. It would be good that a command can do that instead of using multi/exec. Save many lines of code. Just wondering if there is any plans for that.

Josiah Carlson

unread,
Aug 28, 2014, 6:53:53 PM8/28/14
to redi...@googlegroups.com
No plans.

Generally, if what you want to do can be performed with MULTI/EXEC and/or a Lua script, it's not going to be added. There are exceptions to this case, but usually only when the utility (performance, significantly increases the utility of Redis, etc.) exceeds the cost of implementation and maintenance.

If using MULTI/EXEC is that big of a problem, here is a Lua script that does the same thing.

-- KEYS is your list of keys
-- ARGV is your list of hash keys, corresponding to KEYS hashes
local out = {}
for i, key in ipairs(KEYS) do
    table.insert(out, key, redis.call('HGET', key, ARGV[i]))
end
return out

 - Josiah


To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.

Itamar Haber

unread,
Sep 1, 2014, 1:14:01 PM9/1/14
to redi...@googlegroups.com

What everyone said and.

It's funny how fuzzy language is and how each of us interprets things slightly differently. Josiah's script is a better version of what I had in mind (and had I not been so lazy I would have written it too). The point I do want to make is slightly subtler though. The reason I asked Chen for the use case (besides the aging PM in me) is because I can see how this is a  less-frequent use pattern. If you think of/use hashes as the equivalent of a relational DBMS' rows then Chen's inquiry could be understood as asking for the equivalent of SELECTing specific columns from several (perhaps a range of) such "rows" (now it's the DBA in me talking), e.g. for getting a the 'uid' field from some set of session hashes.

I may be projecting here a little too much, so I apologize if I had presumed on your behalf Chen. In any case, that hypothetical use case isn't Redis core stuff because of at least two reasons: it is too specific and it is easily and effectively doable via existing API calls and Lua.

This approach makes you work to get the exact behavior that you want out of Redis and makes you consider the different trade offs for yourself. There are developers here that can add any (reasonable) feature to Redis in less than 5 minutes including testing - that doesn't mean that adding all features to Redis core is a good idea. Build on the core and extend it to do your bidding - all the basic pieces are there. I hope this makes sense to you and I thank you for asking a question that made me think :)

Cheers,

Itamar

Reply all
Reply to author
Forward
0 new messages