Connect Java Apps. to Redis on the cloud

I am having fun Redis and It’s really exciting! If you donot know about Redis,
Redis is a Super fast <key>-<value> store,Data structure server. It is one of the most interesting ,hot and happening technology and software in today’s world.

The values it stores for example can be lists, hashes,sets and sorted sets.

Redis performs atomic operations on each of those data types (examples : union and intersection methods on sets) . It is widely used today for web scale production Apps powering popular websites such as Craigslist, GitHub, The Guardian, Digg so on…

Redis has a number of clients available ( implemented in different languages/technology). Jedis is a snappy Java client with which you can connect to Redis from your Java applications.

The following bit of code provides basic information on how you can connect to Redis using the Jedis library.

First download Jedis library (jar archive file) from the website http://code.google.com/p/jedis/
I
nclude the JAR in the project dependency settings (class path) if you are using IntelliJ Idea or Eclipse IDE.

Install Redis on your computer (localhost) or get a Redis Server from a hosting company such as Redisgoto.

In the below code snippet , I have written a Groovy script to connect to my Redis Cloud Server running  at host IP 20.40.36.9 and port 2654. Password ‘Auth’ for my server instance is also supplied as a part of this as you can see below.

Once I connect to the server, I create an instance of the Jedis driver object and then proceed to create Redis <Key> <Value> pairs ,assigning values to the keys through a ‘for loop’.  For instance, for the key -> address, the value of -> “#100,Big Street” is gets assigned.  Finally, I print the value of  this address was assigned.

import redis.clients.jedis.Jedis

def host = "20.40.36.9"
def keyIndex1 = ["name","address","phone"]
def valueList1 = ["sankar","#100,Big Street",95578838]
Jedis jedis = new Jedis(host,2654,15000)
jedis.auth("be39c84bafb7779991103672c4880eb0")
jedis.connect
for (i in 1..2) {jedis.set(keyIndex1[i],valueList1[i].toString())}
def value = jedis.get(keyIndex1[1])
println value

Next,If  you want to remove all the keys from the current Redis DB,use

       jedis.flushDB()

Have Fun with Redis and Jedis!

Advertisements

About Cloudshoring

Cloudshoring is a popular blog on Cloud computing and Big data
This entry was posted in Cloud Computing and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s