How get data with redis using this hash - database

In the documentation is said that you can use hash to store objects or any kind of data, and later you can extract them. I want to save, for instance, ids from a few users by using hash, and later extract all the ids, or all the elements with ids (something, I didnt accomplish nothing similar), can you help me?
redis 127.0.0.1:6379> set user:id:1234 "content of my first user"
OK
redis 127.0.0.1:6379> set user:id:1235 "content of my second user"
OK
redis 127.0.0.1:6379> set user:id:1236 "content of my third user"
OK
redis 127.0.0.1:6379> get user:id
(nil) ####I hoped to see all my id's users listed, I want to make
something like that,in the documentation I saw an example, but
is not completed####

Those are plain top-level key sets. To make a hash, you should use HSET
hset user:id 1234 "content of my first user"
hset user:id 1235 "content of my second user"
hgetall user:id

Related

Hashing a hash in redis

I have been reading around that I cannot have nested data structures in redis, and the only way to include it is to create a reference to it.
If I understand correct, a way to create a generic struct in redis would be:
hmset ARandomStringAsAKey name kostas address milky_way
I tried to store the hash in that way:
hmset ReferenceTest name kostas ref ARandomStringAsAKey
and then tried to get it back via:
hget ReferenceTest ref
but the only thing I got was a string saying ARandomKeyAsAString .
How could I possibly do that?
Thanks in advance!
You can't get what you want via hget ReferenceTest ref.
You shoud:
get the key via hget ReferenceTest ref
get the real data by the value it returns(ARandomKeyAsAString )
127.0.0.1:6379> hmget ReferenceTest ref
1) "ARandomStringAsAKey"
127.0.0.1:6379> hgetall ARandomStringAsAKey
1) "name"
2) "kostas"
3) "address"
4) "milky_way"
BTW: We don't store data like that, we transfer name kostas address milky_way into a json string, and store it.
127.0.0.1:6379> hmset ReferenceTest name kostas data "{\"name\":\"kostas\",\"address\": \"milky_way\"}"
OK
127.0.0.1:6379> hget ReferenceTest data
"{\"name\":\"kostas\",\"address\": \"milky_way\"}"
127.0.0.1:6379>

Command in command in Redis

I just started Redis. I need to create a database for online store or whatever. Main idea to show functionality. I never worked in Redis and terminal, so a bit confusing. Firstly I want to create a database of users with id users counter:
SET user:id 1000
INCR user:id
(integer) 1001
Can I use somehow command in command like:
HMSET incr user:id username "Lacresha Renner" gender "female" email "renner#gmail.com"
(error) ERR wrong number of arguments for HMSET
in case that my database automatically count new users in database. Or it's not possible in Redis? Should I do it by hands, like user:1, user:2, user:n?
I am working in terminal (MacOS).
HMSET receives a key name, and pairs of elements names and values.
Your first argument (incr) is invalid, and the id part of the second should be explicit id.
e.g.:
HMSET user:1000 username "Lacresha Renner" gender "female" email "renner#gmail.com"
Regarding your first SET, you should have one key that its whole purpose is a running uid, you should use the reply of INCR as the new UID for the new user HASH key name (1000 in the above example).
If you never delete users, the value of the running UID will be the number of users in your system. If you do delete users, you should also insert the UID into a SET and remove the UID once you delete the user. In that case, an SCARD should give you the number of users in your system, and SMEMBERS (or SSCAN) will give you all of their UIDs.

how to store this data, by using hash, in redis

I want to store an structure similiar to this.
The idea is make possible extract all the descriptions with the same id, I didnt see many examples in reddis, the documentation is very basic I think. Someone can tell me?
user:id:12:description:"nice"
user:id:12:description:"bad"
user:id:15:description:"nothing_especial"
...
I know that this is possible with hset, I tried with something like:
hset user id 12
hset user description "nice"
...
but the problem is that, when I try to insert the same key different times, HSET override the content value, and finally I have the same but with a different value
In the documentation I saw an example but was with Set, was something like this (i dont understand why they user a simple set....):
$ redis-cli set news:1:title "Redis is simple"
OK
$ redis-cli set news:1:url "http://code.google.com/p/redis"
OK
$ redis-cli lpush submitted.news 1
I made this, At first I thought about this, but its limited because I cannot have more thatn one description for every user:
hset user id:1:deal "yeaaaaaaaah"
hset user id:1:description "sometifjjapodsifj"
hget user id:1:deal
>yeaaaaaaaah
hget user id:1:description
>sometifjjapodsifj
Finally the solution was this:
hset user1 description1 "asdoifjasdpofjaspdo"
hset user1 description2 "aosdifj"
hset user2 description1 "asdoijfaodsifj"
hset user2 description2 "asdoifjaposdifjaposdifjasdf"
hgetall user2
>description1=asdoijfaodsifj
>description2=asdoifjaposdifjaposdifjasdf

Redis : How to set one key equal to the value of another key?

Is there any quick command in REDIS which allows me to do the following
I want to set the Value of key Y equal to the value of Key X .
How do I go about doing this from the Redis Client .
I use the standard Redis-cli client .
Basically I am looking for some equivalent of the following -
Y.Val() = X.Val()
You can do this with a Lua script:
redis.call('SET', KEYS[2], redis.call('GET', KEYS[1])); return 1;
KEYS1 is the source key
KEYS2 is the target key
The example below uses SCRIPT LOAD to create the script and invokes it using EVALSHA passing the following arguments:
The SHA1 returned from the script load
a 2 for the number of keys that will be passed
The source key
The target key.
Output:
redis 127.0.0.1:6379> set src.key XXX
OK
redis 127.0.0.1:6379> get src.key
"XXX"
redis 127.0.0.1:6379> SCRIPT LOAD "redis.call('SET', KEYS[2], redis.call('GET', KEYS[1])); return 1;"
"1119c244463dce1ac3a19cdd4fda744e15e02cab"
redis 127.0.0.1:6379> EVALSHA 1119c244463dce1ac3a19cdd4fda744e15e02cab 2 src.key target.key
(integer) 1
redis 127.0.0.1:6379> get target.key
"XXX"
It does appear to be a lot of stuff compared to simply doing a GET and then s SET, but once you've loaded the script (and memorized the SHA1) then you can reuse it repeatedly.
If you dont want script loading then below will work as a single command.
127.0.0.1:6379> eval "return redis.call('SET', KEYS[2], redis.call('GET', KEYS[1]))" 2 key1 key2
OK
Note that key1 value should be already set else you will get the below error
Lua redis() command arguments must be strings or integers
So check like below and set
127.0.0.1:6379> GET key1
(nil)
127.0.0.1:6379> SET key1 hello
OK
Now it will work.
If you want copy map to another new map key
eval "return redis.call('HMSET', KEYS[2], unpack(redis.call('HGETALL', KEYS[1])))" 2 existingMapKey newMapKey
One more way is while inserting time itself you can insert the value to two keys using MSET.
redis> MSET key1 "Hello" key2 "Hello"
"OK"
redis> GET key1
"Hello"
redis> GET key2
"Hello"
Ofcource this will not solve the issue of copying when the key is already created.
Also note that there is no way in redis more than one key is referring the same value object. All these workaround will create duplicate value objects. So if one of the value is updated will not reflect in another value object.
Since 6.2.0 you have a COPY command :
https://redis.io/commands/copy
No, there is no quick command to do this. You have to GET the value of the source key, and then SET the value of the new key.
Source: http://redis.io/commands#string

Simple search by value?

I would like to store some information as follows (note, I'm not wedded to this data structure at all, but this shows you the underlying information I want to store):
{ user_id: 12345, page_id: 2, country: 'DE' }
In these records, user_id is a unique field, but the page_id is not.
I would like to translate this into a Redis data structure, and I would like to be able to run efficient searches as follows:
For user_id 12345, find the related country.
For page_id 2, find all related user_ids and their countries.
Is it actually possible to do this in Redis? If so, what data structures should I use, and how should I avoid the possibility of duplicating records when I insert them?
It sounds like you need two key types: a HASH key to store your user's data, and a LIST for each page that contains a list of related users. Below is an example of how this could work.
Load Data:
> RPUSH page:2:users 12345
> HMSET user:12345 country DE key2 value2
Pull Data:
# All users for page 2
> LRANGE page:2:users 0 -1
# All users for page 2 and their countries
> SORT page:2:users By nosort GET # GET user:*->country GET user:*->key2
Remove User From Page:
> LREM page:2:users 0 12345
Repeat GETs in the SORT to retrieve additional values for the user.
I hope this helps, let me know if there's anything you'd like clarified or if you need further assistance. I also recommend reading the commands list and documentation available at the redis web site, especially concerning the SORT operation.
Since user_id is unique and so does country, keep them in a simple key-value pair. Quering for a user is O(1) in such a case... Then, keep some Redis sets, with key the page_id and members all the user_ids..

Resources