带有通配符字段的 redis hmget
redis hmget with wildcard fields
我在 redis 中有一个哈希集,如下所示。
"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,它会给我散列中的所有字段。我的 objective 是只获取 hashset 中以 "rec.number" 开头的那些字段。当我打电话时
redis-cli hmget "abcd" "rec.number*",
它给了我
这样的结果
1)
有没有办法只检索那些以我预期的模式开头的键的数据?我只想检索那些键,因为我的数据集包含许多其他不相关的字段。
HMGET do not supports wildcard in field name. You can use HSCAN 为:
HSCAN abcd 0 MATCH rec.number*
更多关于 SCAN function in official docs。
LUA 方式
此脚本在 LUA 脚本中执行:
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-cli
和 LUA 的精彩介绍可以在 A Guide for Redis Users 中找到。
我在 redis 中有一个哈希集,如下所示。
"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,它会给我散列中的所有字段。我的 objective 是只获取 hashset 中以 "rec.number" 开头的那些字段。当我打电话时
redis-cli hmget "abcd" "rec.number*",
它给了我
这样的结果1)
有没有办法只检索那些以我预期的模式开头的键的数据?我只想检索那些键,因为我的数据集包含许多其他不相关的字段。
HMGET do not supports wildcard in field name. You can use HSCAN 为:
HSCAN abcd 0 MATCH rec.number*
更多关于 SCAN function in official docs。
LUA 方式
此脚本在 LUA 脚本中执行:
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-cli
和 LUA 的精彩介绍可以在 A Guide for Redis Users 中找到。