0

I have redis data with the format below:

HMSET name:1 key "value"
HMSET name:2 key "value2"
HMSET name:3 key "value3"
HMSET name:a key "valuea"

How to get all hashmap with prefix is "name:" the same result below:

name:1 key "value"
name:2 key "value2"
name:3 key "value3"
asked Sep 3, 2020 at 13:58
1

1 Answer 1

3

You want to use SCAN and not KEYS, as KEYS may block your server for too long.

You want to filter by hashmap type. See more in How can I get all of the sets in redis?

Then, you want to append the field-values to the response. For that you need Lua Scripting.

This script should get you started:

local result = redis.call('SCAN', ARGV[1], 'MATCH', ARGV[2])
local filtered = {}
for _,key in ipairs(result[2]) do
 if redis.call('TYPE', key).ok == ARGV[3] then
 local keyWithHash = {}
 keyWithHash[1] = key
 keyWithHash[2] = redis.call('HGETALL', key)
 table.insert(filtered, keyWithHash)
 end
end
result[2] = filtered
return result

If you are using Redis 6 you can optimize it by making the SCAN use the TYPE option instead of filtering one by one by calling TYPE command.

Use as:

EVAL "local result = redis.call('SCAN', ARGV[1], 'MATCH', ARGV[2]) local filtered = {} for _,key in ipairs(result[2]) do if redis.call('TYPE', key).ok == ARGV[3] then local keyWithHash = {} keyWithHash[1] = key keyWithHash[2] = redis.call('HGETALL', key) table.insert(filtered, keyWithHash) end end result[2] = filtered return result" 0 0 "name:*" hash

Pre-test:

HMSET name:1 key "value"
HMSET name:2 key "value2"
HMSET name:3 key "value3"
HMSET name:a key "valuea"
SET name:notAhash "asdasd"

Test:

> EVAL "local result ... return result" 0 0 "name:*" hash
1) "0"
2) 1) 1) "name:a"
 2) 1) "key"
 2) "valuea"
 2) 1) "name:3"
 2) 1) "key"
 2) "value3"
 3) 1) "name:2"
 2) 1) "key"
 2) "value2"
 4) 1) "name:1"
 2) 1) "key"
 2) "value"
answered Sep 3, 2020 at 19:40
Sign up to request clarification or add additional context in comments.

Comments

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.