Introduction:

As you may know Redis is a nosql database.

It uses key-value pairs to store data whereas SQL database works as a table-based relational database structure.

We ever seen how to create entries in Redis using typical commands like HSET , SET ,ZADD and so on ( I will prepare a reminder of these commands with explanations in an incoming blog )

Today the topic is to get back a dataset and inject it in your Redis database.

Redis has many ways to import data into a database:

You can do it from an generated file,using a Redis script or from an existing Redis database.

You can do it through many ways, for example if the user wants to works directly on the data without using classic Redis command ( working with a text editor for example ).

So let’s get started!

Prerequisites

Before adding data in the Redis database I will clean the database in order to see if the tests are working as expected ( If you only want to add directly the data to your database this action is not needed)

To avoid that , I advice you to perform a backup of your existing database before anything.

Start Redis server

Use the redis-server command to start your server:

Check your database status:

  • Open a new Linux Window
  • Use command to check if database is running:

ps -eaf | grep redis

  • Use Redis-cli utility

scan the keys number

Use this command:

info keyspace

Currently we can see that I have a database with a lot of keys , I will flush it as an example for our import data test.

I will use this command ( BE CAREFUL !ALWAYS GET A BACKUP BEFORE DOING SUCH A COMMAND ):

flushdb

Note that this command is different than flushall as it will only clean the current database used whereas flushall will clear ALL your databases so be careful !

Here a link to Redis commands

See all description of the FLUSHDB command

  • Check if database is cleaned
  • Check on Redis Insight

Database is empty

Import data using redis-cli script

1st step create a text file

  • Aim is to create a file containing Redis inputs to create add or delete users for example in your Linux machine type :
vi Best_Players_In_the_World.Redis
  • Then Inside add the Redis command for user creation

HSET 'user:001' first_name 'Nabil' last_name 'SAOUAL' dob '19-FEV-1979'
HSET 'user:002' first_name 'Ronaldo' last_name 'R9' dob '22-SEP-1976'
HSET 'user:003' first_name 'Diego Armando' last_name 'MARADONA' dob '30-OCT-1960'
HSET 'user:004' first_name 'Edson Arantes' last_name 'DO NASCIMENTO' dob '23-OCT-1940'

2nd step : Use the Redis-cli utility to run the script

redis-cli -h localhost -p 6379 < /home/nso/DATA_TO_IMPORT/Best_Players_In_the_World.redis

If the command is successful you will have a message showing the integration of the data

Note:

You must specify the absolute path or launch the command from the script file location to get it work

3rd step Check if change are commit

  • Check on Redis server

Use the command:

info keyspace

We see that 4 keys are now part of our empty database

  • Refresh Redis insight and check if all is fine
  • We can already see that 4 keys are added , press refresh button to see it
  • That’s it , we have imported data using redis-cli utility 🙂

Bonus!

If you want to play around with some dataset you can import the dataset below ( the one used in my RedisInsight introduction blog )

https://github.com/redis-developer/redis-datasets/blob/master/user-database/import_users.redis

But you can also use other datasets like movie or actors dataset.You can find it here:
https://github.com/redis-developer/redis-datasets/tree/master/movie-database

You just have to download them and to inject them to your Database using the Redis-cli script

So, you can see a sample of movie dataset ( you will remark that HSET command is used as for my previous file creation,of course you can use other Redis commands to edit your datasets )

Thanks to tgrall for his work 🙂

Lets’ add it to our DB !

  • Right click on the redis file and chose Save Link As option

Add it to your folder on your Linux machine

if you use WSL you can access form your windows machine form here

\wsl$\YourDistro

Example for me:

\wsl$\openSUSE-Leap-15.4\home\nso\DATA_TO_IMPORT

  • Repeat the import action and see if all is working 🙂

Seems to get some issues during the import but you can do some queries without any trouble ( I have directly made a copy paste of data contained in the import_movies.redis file because some elements were interfering during the import.)

You have now imported some dataset, like this,you will be able to practice and perform some queries

See that it was added in a specific folder, different from my first user creation as we used different fields for that 🙂

Conclusion:

Now you know how to import your own dataset by creating a script and where to find some examples to use, next step we will see how to backup and restore all this work !!

Don’t forget to visit my other blogs and also the dbi bloggers to get more and more interesting tips!