NoSQL Zone is brought to you in partnership with:

My name is Sebastian Pietrowski. Currently I work as Expert Software Development Java. I'm focused on application performance and tuning with success in my daily work. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django and of course Java. This is because I try to be pragmatic programmer. You can find news on my blog http://pietrowski.info/category/news/. Sebastian has posted 1 posts at DZone. View Full User Profile

Spring Data with Redis

02.03.2011
| 12260 views |
  • submit to reddit

The Spring Data project provides a solution for accessing data stored in new emerging technologies like NoSQL databases and cloud based services. When we look into the SpringSource git repository we see a lot of spring-data sub-projects:

  • spring-data-commons: common interfaces and utility class for other spring-data projects.
  • spring-data-column: support for column based databases. It has not started yet, but there will be support for Cassandra and HBase
  • spring-data-document: support for document databases. Currently MongoDB and CouchDB are supported.
  • spring-data-graph: support for graph based databases. Currently Neo4j is supported.
  • spring-data-keyvalue: support for key-value databases. Currently Redis and Riak are supported and probably Membase will be supported in future.
  • spring-data-jdbc-ext: JDBC extensions, as example Oracle RAC connection failover is implemented.
  • spring-data-jpa: simplifies JPA based data access layer.

I would like to share with you how you can use Redis. The first step is to download it from the redis.io web page. try.redis-db.com is a useful site where we can run Redis commands. It also provides a step by step tutorial. This tutorial shows us all structures that Redis supports (list, set, sorted set and hashes) and some useful commands. A lot of reputable sites use Redis today.

After download and unpacking we should compile Redis (version 2.2, the release candidate is the preferable one to use since some commands do not work in version 2.0.4).

make

sudo make install

Once we run these commands we are all set to run the following five commands:

  • redis-benchmark - for benchmarking Redis server
  • redis-check-aof - check the AOF (Aggregate Objective Function), and it can repair that.
  • redis-check-dump - check rdb files for unprocessable opcodes.
  • redis-cli - Redis client.
  • redis-server - Redis server.

We can test Redis server.

redis-server

[1055] 06 Jan 18:19:15 # Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'

[1055] 06 Jan 18:19:15 * Server started, Redis version 2.0.4

[1055] 06 Jan 18:19:15 * The server is now ready to accept connections on port 6379

[1055] 06 Jan 18:19:15 - 0 clients connected (0 slaves), 1074272 bytes in use

and Redis client.

 redis-cli

redis> set my-super-key "my-super-value"

OK

Now we create a simple Java project in order to show how simple a spring-data-redis module really is.

mvn archetype:create -DgroupId=info.pietrowski -DpackageName=info.pietrowski.redis 
-DartifactId=spring-data-redis -Dpackage=jar

Next we have to add in pom.xml milestone spring repository, and add spring-data-redis as a dependency. After that all required dependencies will be fetched.

Next we create a resources folder under the main folder, and create application.xml which will have all the configuration.

We can configure the JedisConnectionFactory, in two different ways, One - we can provide a JedisShardInfo object in shardInfo property. Two - we can provide host (default localhost), port (default 6379), password (default empty) and timeout (default 2000) properties. One thing to keep in mind is that the JedisShardInfo object has precedence and allows to setup weight, but only allows constructor injection.

We can setup the factory to use connection pooling by setting the value of the pooling property to 'true' (default).

See application.xml comments to see three different way of configuration.

Note: There are two different libraries supported: Jedis and JRedis. They have very similar names and both have the same factory name. See the difference:

  • org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory
  • org.springframework.data.keyvalue.redis.connection.jredis.JredisConnectionFactory

Similar to what we do in Spring, we configure the template object by providing it with a connection factory. We will perform all the operations through this template object. By default we need to provide only Connection Factory, but there are more properties we can provide:

  • exposeConnection (default false) - if we return real connection or proxy object.
  • keySerializer, hashKeySerializer, valueSerializer, hashValueSerializer (default JdkSerializationRedisSerializer) which delegates serialization to Java serialization mechanism.
  • stringSerializer (default StringRedisSerializer) which is simple String to byte[] (and back) serializer with UTF8 encoding.

We are ready to execute some code which will be cooperating with the Redis instance. Spring-Data provides us with two ways of interaction, First is by using the execute method and providing a RedisCallback object. Second is by using *Operations helpers (these will be explained later)

When we are using RedisCallback we have access to low level Redis commands, see this list of interfaces (I won't put all the methods here because it is huge list):

Check RedisCallbackExample class, this was the hard way and the problem is we have to convert our objects into byte arrays in both directions, the second way is easier. Spring Data provides for us with Operations objects, so we have much more simpler API and all byte<->object conversion is made by serializer we setup (or the default one). Higher level API (you will easily recognize *Operation *Commands equivalents):

Most of methods get key as first parameters so we have an even better API for multiple operations on the same key:

Check RedisCallbackExample class to see some easy examples of *Operations usage. One important thing to mention is that you should use stringSerializers for keys, otherwise you will have problems from other clients, because standard serialization adds class information. Otherwise you end up keys such as:

  1. "\xac\xed\x00\x05t\x00\x05atInt"
  2. "\xac\xed\x00\x05t\x00\nmySuperKey"
  3. "\xac\xed\x00\x05t\x00\bsuperKey"

Up until now we have just checked the API for Redis, but Spring Data offers more for us. All the cool stuff is in org.springframework.data.keyvalue.redis.support package and all sub-packages. We have:

  • RedisAtomicInteger - Atomic integer (CAS operation) backed by Redis.
  • RedisAtomicLong - Same as previous for Long.
  • RedisList - Redis extension for List, Queue, Deque, BlockingDeque and BlockingQueue with two additional methods List range(start, end) and RedisList trim(start, end).
  • RedisSet - Redis extension for Set with additional methods: diff, diffAndStore, intersect, intersectAndStore, union, unionAndStore.
  • RedisZSet - Redis extension for SortedSet. Note that Comparator is not applicable here so this interface extends normal Set and provide proper methods similar to SortedSet.
  • RedisMap - Redis extension for Map with additional Long increment(key, delta) method

Every interface currently has one Default implementation. Check application-support.xml for examples of configuration and RedisSupportClassesExample for examples of use. There is lot of useful information in the comments as well.

Summary

The library is a first milestone release so there are minor bugs, the documentation isn't as perfect as we used to and the current version needs no stable Redis server. But this is definitely a great library which allows us to use all this cool NoSQL stuff in a "standard" Spring Data Access manner.

Awesome job!

This post is only useful if you checkout the code: from bitbucket , for the lazy ones here is spring-data-redis zip file as well.

This post is originally from http://pietrowski.info/2011/01/spring-data-redis-tutorial/
Published at DZone with permission of its author, Sebastian Pietrowski.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Thomas Kern replied on Thu, 2012/09/06 - 10:50am

Maybe we can make a Serializer for those just want to get spring-data-keyvalue work as soon as possible, they may be confused when they set a key value with spring Data but want to get the value with that key in another client such as redis-cli. As default, even the key does not exist because of the signature of the class. The document doesn’t mention this and I think another more proper default serializer would preffer.

http://www.java-tips.org 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.