Redis-cli --csv option (exporting to csv)

后端 未结 5 2015
轻奢々
轻奢々 2021-02-02 12:34

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

相关标签:
5条回答
  • 2021-02-02 12:49

    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"
    ...
    
    0 讨论(0)
  • 2021-02-02 13:05

    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
    
    0 讨论(0)
  • 2021-02-02 13:05

    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.

    0 讨论(0)
  • 2021-02-02 13:06

    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
    
    0 讨论(0)
  • 2021-02-02 13:11

    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
    
    0 讨论(0)
提交回复
热议问题