How do I iterate over objects in Redis and get all their values? Something like this:
scan object* hgetall
This should return
age 22 password 1234 username Sarah
age 20 password 5678 username Mike
When Redis contains this data:
hmset objectSarah age 22 password 1234 username Sarah
hmset objectMike age 20 password 5678 username Mike
Thanks
You can't do that with just one command, you'll need to scan and then process the results (i.e. HGETALL them) one by one.
You could, however, wrap this into a Lua script (see EVAL) that accepts the current cursor and fetches the data before returning everything to the client. I.e.:
local cur, pat = tonumber(ARGV[1]), ARGV[2]
local scan = redis.call('SCAN', cur, 'MATCH', pat)
for i, v in ipairs(scan[2]) do
local data = redis.call('HGETALL', v)
scan[2][i] = { v, data }
end
return scan
Runtime example:
127.0.0.1:6379> hmset objectSarah age 22 password 1234 username Sarah
OK
127.0.0.1:6379> hmset objectMike age 20 password 5678 username Mike
OK
127.0.0.1:6379> script load "..."
"d81ace3345591b4fee7f3bf4cf28e7b260b59554"
127.0.0.1:6379> evalsha d81ace3345591b4fee7f3bf4cf28e7b260b59554 0 0 object*
1) "0"
2) 1) 1) "objectSarah"
2) 1) "age"
2) "22"
3) "password"
4) "1234"
5) "username"
6) "Sarah"
2) 1) "objectMike"
2) 1) "age"
2) "20"
3) "password"
4) "5678"
5) "username"
6) "Mike"
Notes:
This is actually cluster-safe and the use of programmatically-generated key names here should be ok.
Be careful of HGETALL w/ big hashes.
Scanning is not recommended for big datasets and/or for providing fast responses
Related
I am using Redis to store JSON data with a model that has Id and Name fields. My goal is to implement cursor pagination over this data using the Name as the cursor. I realize this isn't unique and I'll need to do some additional work to resolve the issues presented by that using that particular field as the cursor. However I am struggling to find a way to even begin to implement a basic cursor pagination with Redis commands. Redis has options for various string operations using the "Search" module but these are limited to search within a given string, nothing with comparisons. Am I barking up the wrong tree here and cursor pagination is not possible? Or is there something fundamental I am missing in my design?
Redis has options for various string operations using the "Search" module but these are limited to search within a given string, nothing with comparisons.
Can you please give an example for a required comparison not within a given string?
The Search query can do some comparisons.
For Cursor, FT.CURSOR can be used, for example:
127.0.0.1:6379> JSON.SET key1 $ '{"Id": 100, "Name": "foo", "data": "val1"}'
OK
127.0.0.1:6379> JSON.SET key2 $ '{"Id": 200, "Name": "bar", "data": "val2"}'
OK
127.0.0.1:6379> JSON.SET key3 $ '{"Id": 300, "Name": "foo", "data": "val3"}'
OK
127.0.0.1:6379> FT.CREATE idx ON JSON SCHEMA '$.Id' as id NUMERIC '$.Name' as name TAG
OK
127.0.0.1:6379> FT.AGGREGATE idx '#name:{foo}' LOAD 2 #id #name WITHCURSOR COUNT 1
1) 1) (integer) 1
2) 1) "id"
2) "300"
3) "name"
4) "foo"
2) (integer) 17284697
127.0.0.1:6379> FT.CURSOR READ idx 17284697 COUNT 1
1) 1) (integer) 1
2) 1) "id"
2) "100"
3) "name"
4) "foo"
2) (integer) 17284697
127.0.0.1:6379> FT.CURSOR READ idx 17284697 COUNT 1
1) 1) (integer) 0
2) (integer) 0
127.0.0.1:6379>
I'm trying to strategies how to calculate position/rank based on score and date
I have a two pieces of data with the same score shown in the image below.
If i run the command
$ZADD usersScores 11 userId6
It will add it above userId5 which is not something I want
Is it possible to add it below userId1 maybe based on date?
This is my goal essentially where i would like to show the position based on the amount and the oldest would be ontop via date
Thanks!
Sorted Set ordering
Sorted sets are ordered by their scores first, and then lexigraphically by their members if the scores are equivalent. So in your case if you added the following keys:
127.0.0.1:6379> ZADD myset 11 user1
(integer) 1
127.0.0.1:6379> ZADD myset 11 user5
(integer) 1
127.0.0.1:6379> ZADD myset 24 user3
(integer) 1
127.0.0.1:6379> zadd myset 54 user2
(integer) 1
127.0.0.1:6379> zadd myset 11 user6
(integer) 1
and then did a ZRANGE, you'd get them in ascending order with the members ordered in alphabetical order:
127.0.0.1:6379> zrange myset 0 -1 WITHSCORES
1) "user1"
2) "11"
3) "user5"
4) "11"
5) "user6"
6) "11"
7) "user3"
8) "24"
9) "user2"
10) "54"
What it looks like, is your GUI is using, or are just passing in the rev argument to ZRANGE under the hood, which orders everything in reverse (scores descending, reverse alphabetical)
127.0.0.1:6379> ZRANGE myset 0 -1 WITHSCORES REV
1) "user2"
2) "54"
3) "user3"
4) "24"
5) "user6"
6) "11"
7) "user5"
8) "11"
9) "user1"
10) "11"
Second sort-by
There are a couple of ways you could handle the second sort by with a sorted set, let's assume you wanted to do this with a 32 bit unix timestamp
You could prepend the second sortby field to the member name so if user6's score came in at 1659579650, you'd just make the member name be 1659579650:user1, then when it orders it, it would do so lexigraphically
You could combine the score with the second, since 11 is the score, we could shift 11 32 bits to the left and add/or the timestamp to it, which comes out to 48,904,219,906, those will then order correctly, and then when you want to find the score, all you need to do is right-shift the score by 32 bits.
I have a redis hash store that looks like Item:<id>, with attribute name. I want to filter the hash store by a prefix for name attribute.
What I'm trying to do is store the name (lowercased) in a separate Z-set called Item:::name while setting the score to 0. By doing this, I'm successfully able to get the desired result using ZRANGEBYLEX however I'm unable to map the results back to the original Items. How should I go about implementing something like this?
I've seen multiple autocomplete examples for Redis which require the same functionality but without linking the words back to an actual Item (hash in this case)
In sorted sets the member can't be duplicated, it has to be unique. So different users with the same name will cause problem.
My suggestion requires application layer coding to parse response array and executing hash commands (it will be like secondary indexes);
127.0.0.1:6379> HSET user:1 name jack
(integer) 1
127.0.0.1:6379> HSET user:2 name john
(integer) 1
127.0.0.1:6379> HSET user:3 name keanu
(integer) 1
127.0.0.1:6379> HSET user:4 name jack
(integer) 1
127.0.0.1:6379> ZADD item:names 0 jack::user:1 0 john::user:2 0 keanu::user:3 0 jack::user:4
(integer) 4
127.0.0.1:6379> ZRANGE item:names 0 -1 WITHSCORES
1) "jack::user:1"
2) "0"
3) "jack::user:4"
4) "0"
5) "john::user:2"
6) "0"
7) "keanu::user:3"
8) "0"
127.0.0.1:6379> ZRANGEBYLEX item:names [jack [jo
1) "jack::user:1"
2) "jack::user:4"
At the end you will have name::hash-key formatted array elements. At application layer if you separate each element to two substrings by using ::(any other string such as !!! or || etc) you will have user:1 and user:4.
127.0.0.1:6379> HGETALL user:1
1) "name"
2) "jack"
127.0.0.1:6379> HGETALL user:4
1) "name"
2) "jack"
127.0.0.1:6379>
I am storing some values in redis like for key: 1 the value will be
{"counter":1,"counter1":2}
Now I need to reset value of counter while the counter1 should be remaining same.
To increase counter I am use the command SETEX mykey 60 redis .
But it will also reset the value of counter1. So is there any way I can reset one value for a single key.
Let me know if I need to add some more info.
Instead of string you may use hash, then it will be easy. you can increment by some other value, delete counter etc etc. Each key in your json will be hash field.
127.0.0.1:6379> hset mykey counter 1 counter1 2
(integer) 2
127.0.0.1:6379> hgetall mykey
1) "counter"
2) "1"
3) "counter1"
4) "2"
127.0.0.1:6379> hset mykey counter 25
(integer) 0
127.0.0.1:6379> hgetall mykey
1) "counter"
2) "25"
3) "counter1"
4) "2"
127.0.0.1:6379> HINCRBY mykey counter 15
(integer) 40
127.0.0.1:6379> hgetall mykey
1) "counter"
2) "40"
3) "counter1"
4) "2"
127.0.0.1:6379>
Are these 2 Redis commands different, except for the second having optional LIMIT argument?
http://redis.io/commands/zrange
http://redis.io/commands/zrangebyscore
They are different:
ZRANGE key start stop ...: start and stop are zero-based indexes (i.e they correspond to a position of an element within the sorted set),
ZRANGEBYSCORE key min max ...: min and max refer to scores (i.e they are used to specify a score range).
So, the first one operates by indexes while the second one (as its name implies) operates by scores. Thus they are used for different purposes.
say, the sorted set is:
value score
tom 0
bob 1
alice 100
lucy 102
when you use zrangebyscores, and the max score is 2, the min score is 0, then, you will get tom and bob;
when you use zrange, and the start is 0, the stop is 2, then you will get tom, bob and alice.
127.0.0.1:6379> zadd example 0 tom 1 bob 100 alice 102 lucy
127.0.0.1:6379> zrange example 0 2 WITHSCORES
1) "tom"
2) "0"
3) "bob"
4) "1"
5) "alice"
6) "100"
127.0.0.1:6379> zrangebyscore example 0 2 WITHSCORES
1) "tom"
2) "0"
3) "bob"
4) "1"
this is the key difference.