How to Export Redis Keys as CSV Using CLI

Often, you want to export data from redis as a csv. The redis-cli has a --csv flag, but it only works if you want to export a single key. But with a little bit of command line magic, you can export any number of keys as a CSV format.

This script will export the fields id, display name, reputation and location for all hashes starting with users:*

Using Scan to Select Keys

The first step is to use redis-cli with the --scan and --pattern flags to select the keys of interest. So, to select keys matching users:*, you will run the command redis-cli --scan --pattern users:*.

Note that redis only allows glob style patterns. So users:* will match users:1234 and also match users:1234:favourites. If you want more control, you can follow it up with a grep and a regex.

redis-cli --scan --pattern users:* | grep -e '^users:[^:]*$'

Using AWK to Run Redis Commands

For each key we selected, we need to run a redis command to fetch the data for that key. In our case, we want to fetch fields from a hash, so we use the hmget command. The $0 in the awk script refers to the key we selected previously using scan.

The output of the awk script is piped to redis-cli. Each command is executed, and the output is returned as a CSV because of the --csv flag.

Customizing this Script

  1. If your redis server is not on localhost, you will have to provide the host, port and password twice in the command, basically whenever you call redis-cli.
  2. You can run any command instead of hmget

See Also