Get Redis keys and values at command prompt
Asked Answered
P

4

151

I have a very small data saved in Redis and the following is working as expected that will allow me to download all keys.

redis-cli keys * 

Is there any way to get the keys+values *?

Powel answered 10/11, 2011 at 10:22 Comment(0)
S
308

There's no command for that, but you can write a script to do so.

You will need to perform for each key a "type" command:

> type <key>

and depending on the response perform:

  • for "string": get <key>
  • for "hash": hgetall <key>
  • for "list": lrange <key> 0 -1
  • for "set": smembers <key>
  • for "zset": zrange <key> 0 -1 withscores

Keep in mind that for hashes and sorted sets you will be getting the keys/scores and values.

A possible sh implementation:

#!/bin/sh -eu
keys=`redis-cli keys '*'`
if [ "$keys" ]; then
    echo "$keys" | while IFS= read -r key; do
        type=`echo | redis-cli type "$key"`
        case "$type" in
            string) value=`echo | redis-cli get "$key"`;;
            hash) value=`echo | redis-cli hgetall "$key"`;;
            set) value=`echo | redis-cli smembers "$key"`;;
            list) value=`echo | redis-cli lrange "$key" 0 -1`;;
            zset) value=`echo | redis-cli zrange "$key" 0 -1 withscores`;;
        esac
        echo "> $key ($type):"
        echo "$value" | sed -E 's/^/    /'
    done
fi

But do note:

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.

https://redis.io/commands/keys

Suppletion answered 10/11, 2011 at 11:59 Comment(5)
To add to it, the values mean and fetch different things for different "type"Thamos
Can you guide how it can be written? If I get the data out of redis, I can use commands like grep and sed. As of now I am able to export only keys and not values as mentioned above.Powel
Depends on what you want to do with that. Do you want to create a series of commands to reproduce the data you have? or what do you want to do? Those commands will give you the data, you just have to write the output you want.Suppletion
Is there any way to adapt your script to support password authentication, with the AUTH command? I tried adding "AUTH my_password' to your redis-cli commands and it doesn't work.Amandie
I also added stream) value=`echo | redis-cli -h localhost -p 6379 xinfo stream "$key"`;; to the script.Cuvette
Q
41

WARNING: Do not run this in a production database. You can cause major problems.

Short answer:

for i in $(redis-cli KEYS '*'); do echo $i; redis-cli GET $i; done

Long answer:

To get all keys:

redis-cli KEYS '*' 

to get the value for a key:

redis-cli GET <your-key>

and if you want all values:

for i in $(redis-cli KEYS '*'); do redis-cli GET $i; done

and finally all keys and values:

for i in $(redis-cli KEYS '*'); do echo $i; redis-cli GET $i; done
Quinn answered 20/11, 2020 at 15:30 Comment(2)
I know the question is about "command prompt", but this Redis GUI might still be useful for people ending up here: redislabs.com/redis-enterprise/redis-insightQuinn
This will only GET string values. You will get a (error) WRONGTYPE Operation against a key holding the wrong kind of value for other types. The list of types and how to retrieve them are in the post above.Koblenz
L
5

With redis >= 5.x, a new datatype stream was introduced. So, the

> type <key>

should give you stream. To get its values:

> XRANGE <key> - +
Lewandowski answered 25/6, 2020 at 11:13 Comment(0)
F
0

for windows for /l %%x in (1,1,100000) do (redis-cli get %%x)

Feeling answered 28/10, 2023 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.