Redis hmget与通配符字段 [英] redis hmget with wildcard fields
问题描述
我在redis中有一个哈希集,如下所示.
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"
"..." : "...",
"..." : "...",
"..." : "...",
"..." : "..."
}
如果我叫hgetall abcd,它将为我提供哈希中的所有字段.我的目标是仅获取以"rec.number"开头的哈希集的那些字段.当我打电话给
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*",
它给我的结果是
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 不支持通配符字段名称.您可以为此使用 HSCAN :
HMGET do not supports wildcard in field name. You can use HSCAN for that:
HSCAN abcd 0 MATCH rec.number*
有关官方文档.
此脚本通过LUA脚本实现:
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
可以在 Redis用户指南.
这篇关于Redis hmget与通配符字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!