Monday, 15 April 2013

Centos 6/RHEL install Redis Database Server & Jedis Java Connecter

Redis database is an open source persistant key-value store, or data structure server in which keys can contain an assortment of strings, hashes, lists, sets and sorted sets. Written in ANSI C and originally sponsored by VMWare, it works on all POSIX-compliant Unix systems.

Here are some benchmarks for CentOS as a Virtual Machine

GET: 2801 rps
SET: 36101 rps
INCR: 36496 rps
LPUSH: 38759 rps
LPOP: 38610 rps

an average of approximately 36000.

All with 1024 byte payloads

Redis has an extremely light footprint and small file size. It is easy to compile from source but for now I just have it installed straight from the repo as supplied.

$ sudo yum -y install redis

The little Redis book by Karl Seguin is available below

https://github.com/karlseguin/the-little-redis-book

There are also a variety of client plugins available at    http://redis.io/clients and the one I use here for Java is Jedis.

Jedis is a blindingly fast Redis client for Java by Xetorthio on GitHub, so first download it. 

You will need Java JDK & Eclipse installed first.

Below is a video of a small test program in Java using Jedis to connect to a Redis database, store some characters, then retrieve some characters to display on screen.

video

You might also want to take a quick look at Krut screen capture 
or Xvidcap.

To recreate this or similar having installed Redis database as above, download Jedis jar file and set up a little test in Eclipse (New Java project - 'Test Redis', New Package - 'anything', New Class - 'JedisTest') Then create an empty lib folder in your project and copy the code below into your class file.

import redis.clients.jedis.Jedis;

public class JedisTest {

    /*
    //Store a string into Redis then
    //select the characters to print out
    //tinfoil, for no particular reason whatsoever.
    */
   
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        jedis.set("foo", "thetestingstringforredisjavaapplication");
        String value = jedis.get("foo");
        System.out.println(value.charAt(3));
        System.out.println(value.charAt(7));
        System.out.println(value.charAt(14));
        System.out.println(value.charAt(16));
        System.out.println(value.charAt(17));
        System.out.println(value.charAt(22));
        System.out.println(value.charAt(31));
        }
}


Use the import from file system to locate your Jedis jar and import it. Then right click on it and add it to the build path. You will see the jar copied from from the lib to the referenced libraries folder as indicated in the video.

Make sure that you have started Redis server with

$ sudo service redis start

And that your test project is set up correctly with the build path to Jedis and no errors.

Just run as a standard Java Application, Hit Ctrl + F11 your string should be stored and the required
characters retrieved and displayed as shown.

So, Redis and Jedis, fast and lightweight database storage using Java.

Jedis facilitates the following operations
 
  • Sorting
  • Connection handling
  • Commands operating on any kind of values
  • Commands operating on string values
  • Commands operating on hashes
  • Commands operating on lists
  • Commands operating on sets
  • Commands operating on sorted sets
  • Transactions
  • Pipelining
  • Publish/Subscribe
  • Persistence control commands
  • Remote server control commands
  • Connection pooling
  • Sharding (MD5, MurmureHash)
  • Key-tags for sharding
  • Sharding with pipelining
So it is worth checking out if you are using Java with Redis.