How to get keys from the value in redis - redis

I have checked following, but didn't work.
https://redis.io/commands/keys
KEYS Room:*
1) "Room:120"
2) "Room:121"
3) "Room:122"
Following is the redis key/values (HMSET)
Room:120 [SocketId:mOQDJusPjDTBN5L-AAAC,TimeStamp:10-10-2017 12:10:00 AM]
Room:121 ....
Room:122 ....
...
Need to search as Room:* SocketId:mOQDJusPjDTBN5L-AAAC
How can I search for SocketId in the collection ?
Need to search with:
mOQDJusPjDTBN5L-AAAC

The question is not so clear
as u mentioned hmset i am assuming that you are using hashes to store your data.
As per your data,
'room120' should be the key, 'socketId' should be the field and 'mOQDJusPjDTBN5L-AAAC' should be the value.
so in order to search for socketId you can use hscan,where hscan iterates through the field of a particular key.https://redis.io/commands/scan
in case if you are just using key/value storage i.e
'socketId' being the key ,'mOQDJusPjDTBN5L-AAAC' being the value.
here u can just use the command Keys *socket*to search for the key socketId

Related

What is optimal way to search Redis keys by prefix?

For example, I have stored a value under some keys in redis with prefix doc:
doc:office:room_1
doc:office:room_2
doc:reception
Let's say I want to find key prefix doc:office:
How can I fastly search keys by prefix beside KEYS, SCAN command?
I have checked Redis Search, but it works only on fields not on the keys itself.
Can I add index keys somehow?
You could manage an index yourself. For example, you could have a Set called doc:office. Whenever you add or remove a key that starts with doc:office, you could add or remove that string from the Set. Then, if you need all the keys with that prefix, just get the members of the Set.
Like this:
> SET doc:office:room_1 data
> SADD doc:office doc:office:room_1
> SET doc:office:room_2 moardata
> SADD doc:office doc:office:room_2
> SMEMBERS doc:office
1) doc:office:room_1
2) doc:office:room_2

I found a redis key is empty ,it is a list and has 102360 items

I want to count redis big key,so I did this redis-cli --bigkeys, but I found a key is empty ,it is a list but there are 102360 items
enter image description here
The empty string is a valid name for a key in Redis. You can access it like so:
redis-cli> TYPE ""
list

Redis: How to increment hash key when adding data?

I'm iterating through data and dumping some to a Redis DB. Here's an example:
hmset id:1 username "bsmith1" department "accounting"
How can I increment the unique ID on the fly and then use that during the next hmset command? This seems like an obvious ask but I can't quite find the answer.
Use another key, a String, for storing the last ID. Before calling HMSET, call INCR on that key to obtain the next ID. Wrap the two commands in a MULTI/EXEC block or a Lua script to ensure the atomicity of the transaction.
Like Itamar mentions you can store your index/counter in a separate key. In this example I've chosen the name index for that key.
Python 3
KEY_INDEX = 'index'
r = redis.from_url(host)
def store_user(user):
r.incr(KEY_INDEX, 1) # If key doesn't exist it will get created
index = r.get(KEY_INDEX).decode('utf-8') # Decode from byte to string
int_index = int(index) # Convert from string to int
result = r.set('user::%d' % int_index, user)
...
Note that user::<index> is an arbitrary key chosen by me. You can use whatever you want.
If you have multiple machines writing to the same DB you probably want to use pipelines.

Get redis hash data via cli

I've been experimenting with Redis today. I've managed to store cached values from Drupal, but I'm looking to investigate a bit further and view the value stored in cache_my_custom_cache..
127.0.0.1:6379> keys *
1) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:lookup_cache"
2) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:module_implements"
3) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:bootstrap_modules"
4) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:system_list"
5) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:variables"
6) "ff3169bd93659dc31322abc32835ef3e:path:a:und"
7) "myhash"
8) "ff3169bd93659dc31322abc32835ef3e:path:s:und"
9) "ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup"
10) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:hook_info"
127.0.0.1:6379> type ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup
hash
this reveals it's of type hash.. But when I run:
127.0.0.1:6379> HGET ff3169bd93659dc31322abc32835ef3e:cache_qbe:qbe_markup
(error) ERR wrong number of arguments for 'hget' command
It doesn't like it! Completely new to this, can someone offer a solution?
Besides the key you will need to specify the field in the HGET command.
To get a list of all fields in the hash you can run this:
hkeys ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup
Then you can do:
hget ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup FIELD
You can also get all the values in the hash like this:
hvals ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup
HGET expects an additional parameter after the key name that enumerates which field of your hash you would like returned. Something like this:
HGET my_hash_key my_hash_field
If you're trying to retrieve all fields of your hash at once, you should use HGETALL:
HGETALL my_hash_key
Documentation for HGET: here
Documentation for HGETALL: here
Discussion of Redis types (including hashes): here

How to retrieve all hash values from a list in Redis?

In Redis, to store an array of objects we should use hash for the object and add its key to a list:
HMSET concept:unique_id name "concept"
...
LPUSH concepts concept:unique_id
...
I want to retrieve all hash values (or objects) in the list, but the list contains only hash keys so a two step command is necessary, right? This is how I'm doing in python:
def get_concepts():
list = r.lrange("concepts", 0, -1)
pipe = r.pipeline()
for key in list:
pipe.hgetall(key)
pipe.execute()
Is it necessary to iterate and fetch each individual item? Can it be more optimized?
You can use the SORT command to do this:
SORT concepts BY nosort GET concept:*->name GET concept:*->some_key
Where * will expand to each item in the list.
Add LIMIT offset count for pagination.
Note that you have to enumerate each field in the hash (each field you want to fetch).
Another option is to use the new (in redis 2.6) EVAL command to execute a Lua script in the redis server, which could do what you are suggesting, but server side.