Spring Data Redis Example
In the past few examples, we have been integrating Spring Data with the NoSql Databases. In this example, we shall integrate Spring Data with Redis, a key-value based NoSQL Database.
Spring Data offers a level of abstraction over the traditional way of executing query by exposing a Repository
. As such, the user need not write queries and call specific methods, depending upon the underlying Database.
Redis employs a key-store Data Structure to store data. It can be used to store complex data structure like List
, Set
, Hashes
etc, which is why it is also referred to as Data-Structure Server. Like Gemfire, Redis too uses in-memory datasets
for quicker access.
1. Implementation
Redis can be downloaded from here for Linux systems. We are using Redis 3.0.3
for this demonstration. Once the Redis Server is up and running we can start connecting to it through Spring Data.
Then, we need to have following JAR Files to connect to Redis Server:
- commons-logging.jar
- commons-pool.jar
- jackson-core-asl.jar
- jackson-mapper.jar
- jedis.jar
- spring-asm.jar
- spring-beans.jar
- spring-context.jar
- spring-core.jar
- spring-data-redis.jar
- spring-expression.jar
- spring-tx.jar
Create a project in eclipse or any IDE and add the JAR files downloaded above. Now that the project is setup, we start with the coding phase :
First, we create an Entity that is to be persisted in the Redis Database.
Person.java
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | package com.jcg.examples.bean; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = -8243145429438016231L; public enum Gender{Male, Female} private String id; private String name; private Gender gender; private int age; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this .gender = gender; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } @Override public int hashCode() { final int prime = 31 ; int result = 1 ; result = prime * result + age; result = prime * result + ((gender == null ) ? 0 : gender.hashCode()); result = prime * result + ((id == null ) ? 0 : id.hashCode()); result = prime * result + ((name == null ) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if ( this == obj) return true ; if (obj == null ) return false ; if (getClass() != obj.getClass()) return false ; Person other = (Person) obj; if (age != other.age) return false ; if (gender == null ) { if (other.gender != null ) return false ; } else if (!gender.equals(other.gender)) return false ; if (id == null ) { if (other.id != null ) return false ; } else if (!id.equals(other.id)) return false ; if (name == null ) { if (other.name != null ) return false ; } else if (!name.equals(other.name)) return false ; return true ; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]" ; } } |
One difference here from the previous confgiuration of PoJos the reader will find is that there is no configuration information provided in the Entity. The Data-base simply serializes and stores the PoJo against the Key passed. That is why it is important to implement the Serializable
interface. Not implementing Serializable
interface leads to silly Serialization Exceptions at the time of persisting to the Database.
Next, we configure the repository which will help us in persisting the PoJo to the Redis Server:
PersonRepo.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | package com.jcg.examples.repo; import java.util.Map; import com.jcg.examples.bean.Person; public interface PersonRepo { public void save(Person person); public Person find(String id); public Map<Object, Object> findAll(); public void delete(String id); } |
Next, we implement PersonRepo
in the PersonRepoImpl
class:
PersonRepoImpl.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package com.jcg.examples.repo.impl; import java.util.Map; import org.springframework.data.redis.core.RedisTemplate; import com.jcg.examples.bean.Person; import com.jcg.examples.repo.PersonRepo; public class PersonRepoImpl implements PersonRepo { private RedisTemplate<String, Person> redisTemplate; private static String PERSON_KEY = "Person" ; public RedisTemplate<String, Person> getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate<String, Person> redisTemplate) { this .redisTemplate = redisTemplate; } @Override public void save(Person person) { this .redisTemplate.opsForHash().put(PERSON_KEY, person.getId(), person); } @Override public Person find(String id) { return (Person) this .redisTemplate.opsForHash().get(PERSON_KEY, id); } @Override public Map<Object,Object> findAll() { return this .redisTemplate.opsForHash().entries(PERSON_KEY); } @Override public void delete(String id) { this .redisTemplate.opsForHash().delete(PERSON_KEY,id); } } |
PersonRepoImpl
uses the RedisTemplate
to communicate with the Redis Server. Since we are using Hash based Operations, we are using the Redistemplate#opsForHash()
. The method returns an instance of HashOperations
class. We use the methods in this class to store retrieve the Keys
.
Next is XML
configuration.
spring-config.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Redis Connection Factory --> < bean id = "jedisConnFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool = "true" /> <!-- Redis Template Configuration--> < bean id = "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref = "jedisConnFactory" /> < bean id = "personRepo" class = "com.jcg.examples.repo.impl.PersonRepoImpl" > < property name = "redisTemplate" ref = "redisTemplate" /> </ bean > </ beans > |
The RedisTemplate
injected into the PersonRepoImpl
class by the Spring BeanFactory
. The RedisTemplate
requires the JedisConnectionFactory
instance from the Jedis
JAR. Next we inject the RedisTemplate
instance into the PersonRepoImpl
bean as a reference. We can also use @Autowired
to configure the same and add the component-scan directive in the XML.
Now that all is set, let’s run the application and test out the code! Here’s Application class that loads the XML file to instantiate the Spring Container and run CRUD
commands on the server.
Application.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package com.jcg.examples.test; import java.util.Map; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import com.jcg.examples.bean.Person; import com.jcg.examples.bean.Person.Gender; import com.jcg.examples.repo.PersonRepo; public class Application { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new ClassPathResource( "resources/spring-config.xml" ).getPath()); PersonRepo personRepo = (PersonRepo)context.getBean( "personRepo" ); Person person = new Person(); person.setId( "1" ); person.setAge( 55 ); person.setGender(Gender.Female); person.setName( "Oracle" ); personRepo.save(person); Person person2 = new Person(); person2.setId( "2" ); person2.setAge( 60 ); person2.setGender(Gender.Male); person2.setName( "TheArchitect" ); personRepo.save(person2); Person person3 = new Person(); person3.setId( "3" ); person3.setAge( 25 ); person3.setGender(Gender.Male); person3.setName( "TheOne" ); personRepo.save(person3); System.out.println( "Finding the One : " +personRepo.find( "3" )); Map <Object,Object> personMatrixMap = personRepo.findAll(); System.out.println( "Currently in the Redis Matrix" ); System.out.println(personMatrixMap); System.out.println( "Deleting The Architect " ); personRepo.delete( "2" ); personMatrixMap = personRepo.findAll(); System.out.println( "Remnants .. : " ); System.out.println(personMatrixMap); context.close(); } } |
In Application
class, we are creating an instances of Person Class and save them into the Redis database. We can then retrieve and delete them.
Here’s the sample output of the program :
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | Aug 09, 2015 4:02:57 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@42b1b290: startup date [Sun Aug 09 16:02:57 IST 2015]; root of context hierarchy Aug 09, 2015 4:02:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [resources /spring-config .xml] Aug 09, 2015 4:02:57 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@322558e: defining beans [jedisConnFactory,redisTemplate,personRepo]; root of factory hierarchy Finding the One : Person [ id =3, name=TheOne, gender=Male, age=25] Currently in the Redis Matrix {1=Person [ id =1, name=Oracle, gender=Female, age=55], 3=Person [ id =3, name=TheOne, gender=Male, age=25], 2=Person [ id =2, name=TheArchitect, gender=Male, age=60]} Deleting The Architect Remnants .. : {1=Person [ id =1, name=Oracle, gender=Female, age=55], 3=Person [ id =3, name=TheOne, gender=Male, age=25]} Aug 09, 2015 4:02:58 PM org.springframework.context.support.AbstractApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@42b1b290: startup date [Sun Aug 09 16:02:57 IST 2015]; root of context hierarchy Aug 09, 2015 4:02:58 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@322558e: defining beans [jedisConnFactory,redisTemplate,personRepo]; root of factory hierarchy |
2. Download the Source Code
Here we demonstrated how to configure and manage a Redis Data Repository using Spring Data.
You can download the source code of this example here: SpringDataRedisExample.zip
Hmmm…interesting list. I however was thinking that you may want to include Engati as well. Engati is a chatbot platform that allows you to build, manage, integrate, train, analyse and publish your personalized bot in a matter of minutes. It presently supports eight major messaging platforms including messenger, kik, telegram, line, viber, skype, slack and webchat with a focus on customer engagement, conversational commerce, and customer service and fulfillments.
Read more about it here http://www.engati.com/?utm_source=backlinks