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.
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.
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-cli
and LUA in Redis may be found in A Guide for Redis Users.