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:
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:
Once connected, you should see a command prompt with the IP address and port of the Redis server:
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:
Open the Redis CLI and run the command as shown below:
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. Paste the code below into a file called STATES.TXT
SET "AK" "Alaska"
SET "AS" "American Samoa"
SET "AZ" "Arizona"
SET "AR" "Arkansas"
SET "CA" "California"
SET "CO" "Colorado"
SET "CT" "Connecticut"
SET "DE" "Delaware"
SET "DC" "District Of Columbia"
SET "FM" "Federated States Of Micronesia"
SET "FL" "Florida"
SET "GA" "Georgia"
SET "GU" "Guam"
SET "HI" "Hawaii"
SET "ID" "Idaho"
SET "IL" "Illinois"
SET "IN" "Indiana"
SET "IA" "Iowa"
SET "KS" "Kansas"
SET "KY" "Kentucky"
SET "LA" "Louisiana"
SET "ME" "Maine"
SET "MH" "Marshall Islands"
SET "MD" "Maryland"
SET "MA" "Massachusetts"
SET "MI" "Michigan"
SET "MN" "Minnesota"
SET "MS" "Mississippi"
SET "MO" "Missouri"
SET "MT" "Montana"
SET "NE" "Nebraska"
SET "NV" "Nevada"
SET "NH" "New Hampshire"
SET "NJ" "New Jersey"
SET "NM" "New Mexico"
SET "NY" "New York"
SET "NC" "North Carolina"
SET "ND" "North Dakota"
SET "MP" "Northern Mariana Islands"
SET "OH" "Ohio"
SET "OK" "Oklahoma"
SET "OR" "Oregon"
SET "PW" "Palau"
SET "PA" "Pennsylvania"
SET "PR" "Puerto Rico"
SET "RI" "Rhode Island"
SET "SC" "South Carolina"
SET "SD" "South Dakota"
SET "TN" "Tennessee"
SET "TX" "Texas"
SET "UT" "Utah"
SET "VT" "Vermont"
SET "VI" "Virgin Islands"
SET "VA" "Virginia"
SET "WA" "Washington"
SET "WV" "West Virginia"
SET "WI" "Wisconsin"
SET "WY" "Wyoming"
After creating the file, pipe the commands into Redis using the following code:
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:
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.
As shown in the example above, the command should return all the keys in the database.
You can also use the redis-cli to get the list of all keys using the following syntax:
Or you can limit the keys returned with a pattern as such:
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!!