Tutorial
Import Data into Redis
February 27, 20263 minute read
TL;DR:The fastest way to bulk load data into Redis is withredis-cli --pipe, which uses the Redis protocol to stream commands directly into the server. For structured data like CSV, JSON, or SQL, use RIOT-X to transform and import in a single step.
Redis offers multiple ways to import data into a database: from a file, a script, or from an existing Redis database. Whether you need to bulk load CSV data, restore an RDB snapshot, or migrate from an RDBMS, this guide covers the most common approaches.
#What you'll learn
- How to bulk insert data using a redis-cli script
- How to use redis-cli pipe mode for mass insertion
- How to restore data from an RDB file
- How to import and synchronize data using RIOT (CSV, JSON, SQL)
- How to load data into Redis Cloud
#How do I bulk insert data using a redis-cli script?
1. Create a simple file
users.redis with all the commands you want to run2. Use the
redis-cli tool to execute the scriptThis approach runs each command sequentially and will not impact the existing data, except if you modify existing keys in the script.
INFOSample dataset: You can find sample datasets ready to be imported using this method in theredis-datasetsrepository.
#How do I use redis-cli pipe mode for mass insertion?
For large-scale bulk loading,
redis-cli --pipe is significantly faster than running commands one at a time. Pipe mode sends data using the Redis protocol in a streaming fashion, avoiding the round-trip latency of individual commands.The input file must use the Redis serialization protocol (RESP). You can generate RESP-formatted commands from CSV or other data sources using a script and then pipe the output into Redis.
#How do I restore data from an RDB file?
If you have an RDB file
dump.rdb that contains the data you want, you can use this file to create a new database.-
Copy the
dump.rdbfile into the Redis working directory.If you do not know which folder this is, you can run the commandCONFIG get diron your running Redis instance. -
Start the Redis service with
redis-server. -
The file
dump.rdbis automatically imported. -
Connect to the database using
redis-clior any other client to verify the data has been imported (for example, usingSCAN).
#Related Articles
#How do I import data using RIOT?
Redis Input/Output Tools (RIOT-X) is a set of import/export command line utilities for Redis. RIOT-X is the recommended tool when you need to bulk import CSV, JSON, or SQL data into Redis.
- RIOT DB: migrate from an RDBMS to Redis, Search, JSON, and more.
- RIOT File: bulk import/export data from/to files (CSV, JSON).
- RIOT Gen: generate sample Redis datasets for new feature development and proof of concept.
- RIOT Redis: live replication from any Redis database (including AWS ElastiCache) to another Redis database.
- RIOT Stream: import/export messages from/to Kafka topics.
#How do I import data into Redis Cloud?
You can easily import data into Redis Cloud. See the following documentation:
#Next steps
Once your data is loaded into Redis, you may want to create indexes to query it efficiently. See the Indexing and Querying guide to learn how to build secondary indexes using Sorted Sets, Hashes, and Redis Query Engine.