3

I have a hashset in redis like below.

"abcd" : {
 "rec.number.984567": "value1",
 "rec.number.973956": "value2",
 "rec.number.990024": "value3",
 "rec.number.910842": "value4",
 "rec.number.910856": "...",
 "other.abcd.efgh": "some value",
 "other.xyza.blah": "some other value"
 "..." : "...",
 "..." : "...",
 "..." : "...",
 "..." : "..."
}

if I call hgetall abcd, it will give me all fields in the hash. My objective is to get only those fields of the hashset that begin with "rec.number". When I call like

 redis-cli hmget "abcd" "rec.number*", 

it gives me a result like

1)

Is there a way to retrieve data for only those keys which start with my expected pattern? I want to retrieve only those keys because my dataset contains many other irrelevant fields.

Nick Bondarenko
6,4014 gold badges39 silver badges58 bronze badges
asked Jan 19, 2016 at 14:11
1

1 Answer 1

7

HMGET do not supports wildcard in field name. You can use HSCAN for that:

HSCAN abcd 0 MATCH rec.number*

More about SCAN function in official docs.

LUA way

This script does it in LUA scripting:

local rawData = redis.call('HGETALL', KEYS[1]);
local ret = {};
for idx = 1, #rawData, 2 do
 if string.match(rawData[idx], ARGV[1]) then
 hashData[rawData[idx]] = rawData[idx + 1];
 end 
end

Nice intro about using redis-cliand LUA in Redis may be found in A Guide for Redis Users.

answered Jan 19, 2016 at 14:28
Sign up to request clarification or add additional context in comments.

1 Comment

In the Lua script, you should actually use HSCAN as well. If the Hash is big enough/RAM is tight, this will reduce the Lua engine's heap.

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.