Redis

How can I see All Redis Keys

If you have ever used a Key-value database, you probably have no problem with the Redis database’s workings. However, if you are new to the concept of key-value databases, they can be a little confusing compared to relational databases.

This tutorial will try to demystify how to work with Redis by showing how to work with keys.

NOTE: We do not intend this article to be a complete guide to Redis. It only plays a part in the entire concept of how Redis works.

Redis Insert Key

Let us start at the basics and discuss how you insert keys into a Redis database. Redis uses native commands using the Redis CLI. The Redis CLI is an interactive command-line environment for interacting with the Redis Cluster.

To open the Redis CLI, enter the command as:

redis-cli

The command above will connect to the Redis server using the default port (6379) and localhost. If Redis is hosted on a different host or port, you can specify them with -h and -p options, respectively.

For example:

redis-cli -h 192.168.0.6 -p 6300

Once connected, you should see a command prompt with the IP address and port of the Redis server:

127.0.0.1:6379>

To insert a key into Redis, we use the SET command. The command takes two arguments. The first argument acts as the key, and the second argument acts as the value for the specified key.

Let’s take a dataset containing state information. We can use the abbreviation as the key and the full name as the value.

For example:

CO -> "Colorado"

Open the Redis CLI and run the command as shown below:

SET CO "Colorado"

The above command will insert the key and its associated value to the Redis database. However, this method is not very efficient when inserting multiple values.

To solve this, we can use the Redis mass insertion.

Start by downloading the provided file containing a set of Redis commands and key-value pairs.

https://www.dropbox.com/s/ckwe40mbake6odk/STATES.TXT?dl=0

Once the file is downloaded, open the terminal and enter the command below to import the datasets.

cat STATES.TXT | redis-cli --pipe

The command above will export the data from the text file Redis.

Redis Retrieve Key

Use the GET command to get the value stored in a specific key. The command takes the key name as the argument.

For example:

GET  CO

The above should return the value associated with the specified key.

Redis GET all Keys

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern.

In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

KEYS *

As shown in the example above, the command should return all the keys in the database.

Closing

This guide shows you how to work with the Redis, from inserting keys to retrieving the keys matching a specific pattern.

Thank you for reading!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list