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?
Asked
Active
Viewed 3.6k times
5 Answers
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
-
how to import the exported csv? – Mithril Mar 11 '16 at 01:59
-
@Boom Shakalaka - Please guide us here: https://stackoverflow.com/questions/53321019/load-csv-file-in-redis-using-redis-cli-of-windows-version – PAA Nov 15 '18 at 13:56
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