ohadinho
ohadinho

Reputation: 7144

Get all hashes exists in redis

I'm have hashes in redis cache like:

Hash         Key    Value
hashme:1    Hello   World
hashme:2    Here    Iam
myhash:1    Next    One

My goal is to get the Hashes as output in the CLI like:

hashme
myhash

If there's no such option, this is ok too:

 hashme:1
 hashme:2
 myhash:1

I didn't find any relevant command for it in Redis API.

Any suggestions ?

Upvotes: 13

Views: 35375

Answers (5)

Roshan Chauhan
Roshan Chauhan

Reputation: 84

for redis in python, you can use below command to retrieve keys from redis db

def standardize_list(bytelist):
    return [x.decode('utf-8') for x in bytelist]

>>> standardize_list(r.keys())
['hat:1236154736', 'hat:56854717', 'hat:1326692461']

here r variable is redis connection object

Upvotes: 0

for_stack
for_stack

Reputation: 22886

You can use the SCAN command to get all keys from Redis. Then for each key, use the TYPE command to check if it's a hash.

UPDATE:

With Redis 6.0, the SCAN command supports TYPE subcommand, and you can use this subcommand to scan all keys of a specified type:

SCAN 0 TYPE hash

Also never use KEYS command in production environment!!! It's a dangerous command which might block Redis for a long time.

Upvotes: 14

Hui Tan
Hui Tan

Reputation: 103

keys * 

is work for me. you Can try it.

Upvotes: 4

Gyanender Singhle
Gyanender Singhle

Reputation: 19

connection.keys('*') this will bring all the keys irrespective of the data type as everything in redis is stored as key value format

Upvotes: 1

Pascal Le Merrer
Pascal Le Merrer

Reputation: 5981

The idea of redis (and others K/v stores) is for you to build an index. The database won't do it for you. It's a major difference with relational databases, which conributes to better performances.

So when your app creates a hash, put its key into a SET. When your app deletes a hash, remove its key from the SET. And then to get the list of hash IDs, just use SMEMBERS to get the content of the SET.

Upvotes: 3

Related Questions