Get Redis keys and values at command prompt

前端 未结 3 1551
面向向阳花
面向向阳花 2020-12-12 15:23

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 * 

3条回答
  •  醉酒成梦
    2020-12-12 16:08

    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 
    

    and depending on the response perform:

    • for "string": get
    • for "hash": hgetall
    • for "list": lrange 0 -1
    • for "set": smembers
    • for "zset": zrange 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

提交回复
热议问题