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 ho
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"
...
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
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.
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
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