20

Using redis-cli I connected to specific server:

redis-cli -h 10.1.xx.xx

And select 1

Then just to get list of one key features:

KEYS data_column*

THis will print list of that column values on command line. However, there are like quite many values, I want to save query output to file.

In general, using > file_name after the command works. But in this case, it does not work, as its on redis server, though from command line. How to save such query result?

xavi
  • 327
  • 1
  • 5
  • 13

6 Answers6

30

Simply use:

./redis-cli -h 10.1.xx.xx -n 1 keys 'data_column*' >file.txt
Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • In this way, "Access denied" is response. The way i did (connecting to ip, then select 1, after having OK, typing KEYS column*) actually print on screen, but I just was not able to save. – xavi Nov 02 '15 at 16:14
  • Make sure you create the file at a place you actually have the right to write. For instance, use > /tmp/file.txt – Didier Spezia Nov 02 '15 at 19:32
  • Made sure. Actually it return empty list. If I do as I explained in question, it returns list of values. Btw, address is in this way `10.1.xx.xx:xxxx` (select 1 was moving it appear in `10.1.xx.xx:xxxx[1]` ) – xavi Nov 03 '15 at 13:19
  • 1
    IMO, this is your problem. You should never use "-h 10.1.xx.xx:xxxx" but rather "-h 10.1.xx.xx -p xxxx -n 1". In the redis-cli command line, address, port and database are all managed by different options. – Didier Spezia Nov 03 '15 at 14:18
  • Getting empty list even though using separate port name, host name (elastic cache name) - ./redis-cli -h assigned.name.cache.amazonaws.com -p 6379 -n 1 keys 'keyName_*'. – Sandeepan Nath Aug 26 '19 at 10:52
  • Why do we need to specify database number? `-n 1`? – franklin Sep 12 '19 at 21:17
  • Because the KEYS command only applies in the context of a database. You cannot run KEYS globally for all databases at the same time. – Didier Spezia Oct 29 '19 at 14:58
6
echo "keys data_column*" | redis-cli -h 10.1.xx.xx -p xx > file.txt
upe
  • 1,862
  • 1
  • 19
  • 33
hjiam2
  • 61
  • 1
  • 3
5

Following what hjiam2 said above but I cannot comment on their post. I misunderstood what they meant by "keys data_column*" and eventually achieved what I wanted with:

echo 'GET key_name' | redis-cli -h localhost -p 6379 > key_value.txt

I had a long value in a key that I wanted to view so needed to place it into a file where I can then do whatever I want with it. Using the above command achieved this.

Obviously make sure the key_name is what you're looking for and make sure the host and port are correct too.

Arkin
  • 51
  • 1
  • 1
3

Use below command line to export all keys to file.

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

XX -> hostname

YY -> password

2

This is what worked for me (redis-cli version 5.0.7):

redis-cli -a [password] -h [hostname] -p [port] [operation name] [key] >> /tmp/myfile.txt 

So for example:

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

I was searching for this but I didn't find a suitable solution, So I wrote a piece of JS code to do this in my way, I hope it can be useful.

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!');
      });
    })
  }
})
Arsham Arya
  • 1,461
  • 3
  • 17
  • 25