在命令提示符下获取 Redis 键和值 [英] Get Redis keys and values at command prompt

查看:79
本文介绍了在命令提示符下获取 Redis 键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Redis 中保存了一个非常小的数据,以下内容按预期工作,这将允许我下载所有密钥.

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 * 

有什么办法可以得到keys+values *?

推荐答案

没有针对此的命令,但您可以编写脚本来执行此操作.

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>

并根据响应执行:

  • 对于字符串":get
  • 对于哈希":hgetall
  • 对于列表":lrange 0 -1
  • 对于设置":smembers
  • 对于zset":zrange 0 -1 得分

请记住,对于散列和排序集,您将获得键/分数和值.

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

一个可能的 sh 实现:

#!/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

但请注意:

警告:将 KEYS 视为只应在生产环境中极其小心地使用的命令.当它针对大型数据库执行时,它可能会破坏性能.此命令用于调试和特殊操作,例如更改键空间布局.

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

这篇关于在命令提示符下获取 Redis 键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆