antirez/redis — Issue #175: Support for mass insertion in redis-cli.
Author: antirez
Created: 2011-11-03 11:47:01+00:00
State: open
Labels: 2.4 merge candidate, approved feature, feature request, more design needed, waiting user input

Sometimes especially during experiments to see if Redis fits a given use case it is very useful to mass insert a lot of records in Redis in little time.

The best option so far is generating the Redis protocol performing the insertion, that is very simple, and then pipe the protocol using netcat, like in:

cat protocol.txt | nc localhost 6379

The above is sub optimal at best as forces users to understand the Redis protocol.
This issue is a feature request for a more solid way to import data from CSV files, that are easy generated in many contexts, and are often the format that is used to store the source data file.

The proposal allows to use redis-cli in a way similar to the following:

redis-cli --import-csv mydata.csv SET '$0' '$1'

In the above example the CSV file is read, and for every entry in the file the SET command is called using the first column as key name, and the second one as value. A file with the same layout could be used to populate an hash as well, with the following command:

redis-cli --import-csv mydata.csv HSET myhash '$0' '$1'

A more interesting could be the following. Suppose you have a CSV file with the following layout:

"Rome","size","2309424"
"Rome","population","329402...."
"Milan","size", ....
"Milan","population", ...

and so on. It is possible to populate an hash per every city just using:

redis-cli --import-csv mydata.csv HSET '$0' '$1' '$2'

Even if with this simple syntax the new option should allow a fairly large different imports starting from the same data, especially since the command could be also a Redis Lua script starting from Redis 2.6.

Waiting for user inputs, but this is definitely something I would say in redis-cli.