Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Redis查询输出保存到文件_Redis_Redis Cli - Fatal编程技术网

将Redis查询输出保存到文件

将Redis查询输出保存到文件,redis,redis-cli,Redis,Redis Cli,使用连接到特定服务器的redis cli I: rediscli-h10.1.xx.xx 然后选择1 然后,只需获取一个关键功能的列表: KEYS data\u列* 这将在命令行上打印该列值的列表。不过,有很多值,我想将查询输出保存到文件中 通常,在命令生效后使用>文件名。但在这种情况下,它不能工作,因为它在redis服务器上,尽管是从命令行。如何保存这样的查询结果?只需使用: ./redis-cli -h 10.1.xx.xx -n 1 keys 'data_column*' >file

使用连接到特定服务器的redis cli I:

rediscli-h10.1.xx.xx

然后
选择1

然后,只需获取一个关键功能的列表:

KEYS data\u列*

这将在命令行上打印该列值的列表。不过,有很多值,我想将查询输出保存到文件中

通常,在命令生效后使用
>文件名
。但在这种情况下,它不能工作,因为它在redis服务器上,尽管是从命令行。如何保存这样的查询结果?

只需使用:

./redis-cli -h 10.1.xx.xx -n 1 keys 'data_column*' >file.txt

echo“keys data_column*”|redis cli-h 10.1.xx.xx-p xx>file.txt使用下面的命令行将所有密钥导出到文件

./redis cli-h XX-p YY KEYS“keyname*”>>filedata.txt

XX->hostname


YY->password

遵循hjiam2上面所说的内容,但我无法对他们的帖子发表评论。我误解了他们所说的“关键数据列*”的含义,最终实现了我的目标:

echo 'GET key_name' | redis-cli -h localhost -p 6379 > key_value.txt
我想查看的密钥中有一个很长的值,所以需要将其放入一个文件中,然后我可以对其执行任何操作。使用上述命令实现了这一点


显然,请确保键\u名称是您要查找的,并确保主机端口也正确无误。

这正是我所需要的(redis cli 5.0.7版):

例如:

redis-cli -a password -h 127.0.0.1 -p 6379 GET myrediskey >> /tmp/myfile.txt 

我在寻找这个,但没有找到合适的解决方案, 所以我用自己的方式写了一段JS代码,希望它能有用

const redis = require("redis");
const client = redis.createClient(6379);
const fs = require("fs")

client.KEYS("*", (e, res) => {
  for (i=0; i < res.length; i++){
    client.get(res[i], (e, item) => {
      fs.appendFile('output.txt', "Item: " + item + "\n", function (err) {
        if (err) throw err;
        console.log('Item Saved!');
      });
    })
  }
})
const redis=require(“redis”);
const client=redis.createClient(6379);
常数fs=要求(“fs”)
客户端密钥(“*”,(e,res)=>{
对于(i=0;i{
fs.appendFile('output.txt',“Item:+Item+”\n',函数(err){
如果(错误)抛出错误;
console.log('itemsaved!');
});
})
}
})

这样,“拒绝访问”就是响应。我的方式(连接到ip,然后选择1,在确定后,键入KEYS column*)在屏幕上实际打印,但我无法保存。请确保您在实际有权写入的位置创建文件。例如,请确保使用>/tmp/file.txt。实际上它返回空列表。如果我按照我在问题中解释的那样做,它将返回值列表。顺便说一句,地址是这样的
10.1.xx.xx:xxxx
(选择1正在移动它出现在
10.1.xx.xx:xxxx[1]
)IMO,这是您的问题。您不应该使用“-H10.1.xx.xx:xxxx”,而应该使用“-H10.1.xx.xx-pxxxx-n1”。在redis cli命令行中,地址、端口和数据库都由不同的选项管理。即使使用单独的端口名、主机名(弹性缓存名)-/redis cli-h assigned.name.cache.amazonaws.com-p 6379-n 1键“keyName”*,也会获取空列表。
const redis = require("redis");
const client = redis.createClient(6379);
const fs = require("fs")

client.KEYS("*", (e, res) => {
  for (i=0; i < res.length; i++){
    client.get(res[i], (e, item) => {
      fs.appendFile('output.txt', "Item: " + item + "\n", function (err) {
        if (err) throw err;
        console.log('Item Saved!');
      });
    })
  }
})