Before we start
In this blog, you will learn how to install a NoSQL database Redis. You will also learn about starting redis-server, using redis-cli.
In the upcoming series of this article, we will create a demo java application using spring-data-redis exploring various data structures supported by the redis and also go through publish subscribe messaging with Redis PubSub. I will also show you about the replication setup for Redis server.
The official stable version of redis can be installed for unix like system only. Download, extract and compile Redis with following commands on the terminal:
$ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
$ tar xzf redis-2.6.14.tar.gz
$ cd redis-2.6.14
Redis is now installed. Now run the redis using the command :
By default, redis run on port 6379. Port can be configured in /. Your terminal screen should have message like:
A two-minute introduction to Redis data types using redis-cli
Any binary sequence can be used as the redis key, from string to jpeg content. Redis support following data types for value:
Redis command line interface can be started on the terminal by the command:
This is the simplest data type. The simple string key/value data can be saved and retrieved as on redis-cli as :
$ set author.name "Omanand Jha Vatsa"
$ get author.name
"Omanand Jha Vatsa"
Redis lists are ordered collection implemented using linked list. Adding an element to the head or tail of the list is performed in the constant
$ rpush scores 30
$ rpush scores 40
$ rpush scores 50
$ lrange scores 0 2
$ lrange scores -3 -1
Integer reply: the length of the list after the push operations.
Redis Sets are a unordered collection of binary-safe strings. SADD command adds a new element to a set. SMEMBERS command gives the elements of the set. SISMEMBER command returns 1, if the element exists in the set otherwise 0.
$ sadd users "user1"
$ sadd users "user2"
$ sadd users "user3"
$ sadd users "user1"
$ smembers users
$ sismember users "user1"
$ sismember users "user4"
We will start developing a demo Java application using spring-data-redis and maven in the second part of this tutorial.