Kundera over Redis

Devender Yadav edited this page Mar 23, 2017 · 4 revisions
Clone this wiki locally

Redis


Redis is an open-source, networked, in-memory, key-value data store. Redis provides support for following data types:

  • String
  • List of string
  • Set of string
  • Sorted set of string
  • HashSet of string

JPA Way

As a JPA provider, Kundera provides support for Redis. It allows to perform CRUD and query operation over Redis in JPA way. Let's look into how to use Kundera over Redis as a database.

  • Entity
*/
@Entity
@Table(name = "PERSON", schema = "RedisK@redis_pu")
public class PersonRedis
{

    /** The person id. */
    @Id
    @Column(name = "PERSON_ID")
    private String personId;

    /** The person name. */
    @Column(name = "PERSON_NAME")
    private String personName;

    /** The age. */
    @Column(name = "AGE")
    private Integer age;
  // setters and getters. 
}


Persistence unit configuration

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
https://raw.github.com/impetus-opensource/Kundera/Kundera-2.0.4/kundera-core/src/test/resources/META-INF/persistence_2_0.xsd"
version="2.0">

<persistence-unit name="redis_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<properties>
<property name="kundera.nodes" value="localhost" />
<property name="kundera.port" value="6379" />
<property name="kundera.keyspace" value="RedisK" />
<property name="kundera.dialect" value="redis" />
<property name="kundera.client" value="redis" />
<property name="kundera.client.lookup.class" value="com.impetus.client.redis.RedisClientFactory" />
<property name="kundera.cache.provider.class"
value="com.impetus.kundera.cache.ehcache.EhCacheProvider" />
<property name="kundera.cache.config.resource" value="/ehcache-test.xml" />
</properties>
</persistence-unit>

</persistence>
  • Authentication

We can enable authentication over redis server for all client operation as:

<property name="kundera.password" value="Kundera@123" />  {look into redis.conf for requirePass value}
  • Transaction

To enable transaction support over Redis, we simply need to provide:

<property name="kundera.transaction.resource.class" value="com.impetus.client.redis.RedisTransaction" />

To configure transaction timeout and connection pool size over Redis connection we need to :

<property name="kundera.transaction.timeout" value="30" /> 
<property name="kundera.pool.size.max.active" value="10" />

CRUD


       EntityManagerFactory emf = Persistence.createEntityManagerFactory("redis_pu");
        EntityManager em = emf.createEntityManager();
     
        //create object. 
        final String originalName = "vivek";
        PersonRedis object = new PersonRedis();
        object.setAge(32);
        object.setPersonId(ROW_KEY);
        object.setPersonName(originalName);

        em.persist(object); // persist object.
        PersonRedis result = (PersonRedis) em.find(PersonRedis.class, ROW_KEY);

Query

  • Find by key
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("redis_pu");
        EntityManager em = emf.createEntityManager();

        // find by key.
        String findById = "Select p from PersonRedis p where p.personId=:personId";
        Query query = em.createQuery(findById);
        query.setParameter("personId", ROW_KEY);
        results = query.getResultList();
  • Find by key and now row key
 // Find by key and now row key
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("redis_pu");
        EntityManager em = emf.createEntityManager();

        String findByAge = "Select p from PersonRedis p where p.age=:age";
        Query query = em.createQuery(findByAge);
        query.setParameter("age", 32);
        results = query.getResultList();
  • Between clause
 // Between clause over rowkey
        String findIdByBetween = "Select p from PersonRedis p where p.personId between :min AND :max";
        query = em.createQuery(findIdByBetween);
        query.setParameter("min", ROW_KEY);
        query.setParameter("max", ROW_KEY + 1);

        results = query.getResultList();
  • Please refer RedisQueryTest for more details over JPA query support.

You may also refer junits for more examples.

Under the hood

Kundera relies on HashSet for storing entity and builds inverted indexes of non-key fields and store them as Sorted set to provide JPQL support over non-key fields.