25

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?

5 Answers 5

38

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
Sign up to request clarification or add additional context in comments.

2 Comments

how to import the exported csv?
@Boom Shakalaka - Please guide us here: stackoverflow.com/questions/53321019/…
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"
...

Comments

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

1 Comment

This is the best answer IMO
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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.