23

I would like to export a subset of my Redis data on the slave to a csv file. I notice a new csv output option was added to redis-cli but I am unable to find documentation of how it works. Enabling the option prints the command outputs to screen in csv format. What is the best way to get this into a csv file?

Boom Shakalaka
  • 895
  • 2
  • 10
  • 18

5 Answers5

36

Cutting edge!

I've just looked at the source code & all it does is output the commands as comma separated values to stdout. Which is no big surprise.

So you could just redirect it to a file, in the standard way, as long as you're on Linux?

e.g./

redis-cli --csv your-command > stdout.csv 2> stderr.txt
Community
  • 1
  • 1
gef
  • 7,025
  • 4
  • 41
  • 48
8

The command:

redis-cli --csv hgetall mykey > stdout.csv

created a CSV file like:

"id","value","id","value", ...

To make life easier, I used:

sed -i 's/\([^\",]*",[^,]*\),/\1\n/g' stdout.csv

which converted the output into:

"id", "value"
"id", "value"
"id", "value"
...
temp3l
  • 81
  • 1
  • 2
1

If you don't require wrapping the values in quotes as --csv does, then the raw output is sufficient, and you just need to join every 2 lines with a comma to get a CSV:

redis-cli <your redis command> | paste -d ","  - - > out.csv
Max
  • 1,313
  • 14
  • 11
0

Go to src redis directory and run the below command

./redis-cli $command > $file_name

Exemple: ./redis-cli SMEMBERS "$KEY" > $file_name(RANDOM NAME)

this worked for me.

goto
  • 7,908
  • 10
  • 48
  • 58
0

In case of socket and/or multiple redis servers, you'd need to do:

redis-cli -s /path/to/socket --csv your-command > stdout.csv 2> stderr.txt

E.g.

redis-cli -s /var/run/redis/redis4.sock --csv lrange my_list 0 -1 > stdout.csv 2> stderr.txt
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205