13

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 ?

ohadinho
  • 6,894
  • 16
  • 71
  • 124

5 Answers5

13

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.

for_stack
  • 21,012
  • 4
  • 35
  • 48
  • 2
    Can you further explain what is the set of commands I need to execute ? – ohadinho Dec 14 '16 at 09:43
  • 1
    The [SCAN](https://redis.io/commands/scan) command is used for incrementally iterating keys in Redis. With this command, you can get all keys in Redis. The [TYPE](https://redis.io/commands/type) command is used for getting the type of a key. Please see the doc for more details. – for_stack Dec 14 '16 at 13:13
  • This command only returns the first 10 found. Not the equivalent of keys *. – FredTheWebGuy Mar 04 '23 at 03:23
  • @FredTheWebGuy You can scan in a loop with the cursor it returns. Check its documentation for detail. However, never use `KEYS` in production, since it blocks Redis for a long time especially when there're lots of keys in the database. – for_stack Mar 04 '23 at 04:19
4
keys * 

is work for me. you Can try it.

Hui Tan
  • 95
  • 1
  • 5
3

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.

Pascal Le Merrer
  • 5,883
  • 20
  • 35
  • That is a nice workaround, but I don't want to write code in my app just for this scenario. – ohadinho Dec 14 '16 at 09:44
  • 2
    Its not a workaround its how you have to do it. You are responsible to construct and manage your own indexes typically using a SET - so if you need to be able to retrieve all HASH of some type relevant to your app (eg users) you need to keep a SET called users into which you add/remove your HASH keys as required – Mike Miller Dec 14 '16 at 12:05
  • Both answers may be valid, depending on the context. We do not have enough context info in the question to know which one is better (by example: how any keys are they in the DB? Is it in production? etc.) – Pascal Le Merrer Dec 14 '16 at 17:17
1

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

  • 1
    Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets. – Valeriy Solovyov May 26 '19 at 07:58
0

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