Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Treat Your Data as an Object Using Kundera

DZone's Guide to

Treat Your Data as an Object Using Kundera

Kundera is a polyglot object mapper with a JPA interface. If you are JPA developer, Kundera will make your life easy by supporting multiple NoSQL databases in a generic way and allowing you to easily switch between them. Check it out.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Kundera is a "Polyglot Object Mapper" with a JPA interface. Kundera currently supports Cassandra, MongoDB, HBase, Redis, OracleNoSQL, Neo4j, CouchDB, Kudu, Relational databases, and Apache Spark.

New folks should read Getting Started in 5 minutes.

If you are JPA developer, Kundera will make your life easy by supporting multiple NoSQL databases in a generic way and allowing you to easily switch between them. What if you have not used JPA? What if you don't understand EntityManagerFactory, EntityManager, and many other JPA related things.

Data as Object

Using this lightweight API, the user can perform CRUD and Querying without taking care of JPA-related configurations. Users can simply treat data as an object.

Kundera Normal Approach:

    User user = new User();
    user.setUserId("0001");
    user.setFirstName("John");
    user.setLastName("Smith");
    user.setCity("London");

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu");
    EntityManager em = emf.createEntityManager();

    em.persist(user);
    em.close();    
    emf.close(); 

Data As Object Approach:

    User user = new User();
    user.setUserId("0001");
    user.setFirstName("John");
    user.setLastName("Smith");
    user.setCity("London");
    user.save();

How to Use

Add the following dependency in pom.xml in addition to the client dependencies:

<dependency>
     <groupId>com.impetus.kundera.client</groupId>
     <artifactId>data-as-object</artifactId>
     <version>${kundera.version}</version>
</dependency>

Check out the latest Kundera version here.

JSON Instead of persistence.xml

Rather than creating a META-INF/persistence.xml user needs to add a JSON properties file in the classpath. Example: client-properties.json:

{
  "com.impetus.kundera.dataasobject.entities.Employee,com.impetus.kundera.dataasobject.entities.Department": {
    "kundera.nodes": "localhost",
    "kundera.port": "9160",
    "kundera.client": "cassandra",
    "kundera.keyspace": "DAOTest",
    "kundera.ddl.auto.prepare": "update",
    "cql.version": "3.0.0",
    "kundera.client.lookup.class": "com.impetus.client.cassandra.thrift.ThriftClientFactory"
    }
}

Minor Change in POJO

POJO class needs to extends DefaultKunderaEntity<EntityName, IdDatatype>. All the remaining things are same.

Usual Kundera Entity:

@Entity
public class User 
{
    @Id
    private String userId;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column
    private String city;

    // Getters & Setters
}

Data As Object Entity:

@Entity
public class User extends DefaultKunderaEntity<User, String>
{
    @Id
    private String userId;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column
    private String city;

    // Getters & Setters
}

Basic Configuration

Before performing any operation on an object, User needs to use bind() method.

 User.bind("client-properties.json", User.class);

Internally it will create EntityManagerFactory and EntityManager instances.

After performing all operations, in the end, user needs to use unbind() method

 User.unbind();

Internally this will close instances of EntityManagerFactory and EntityManager.

Supported Operation 

CRUD

 User.bind("client-properties.json", User.class);

 User user = new User();
 user.setUserId("101");
 user.setFirstName("John");
 user.setLastName("Smith");
 user.setCity("London");

 // Save
 user.save();

 //Find
 User u = new User().find(101);

 u.setFirstName("Adam");

 //Update
 u.update();

 //Delete
 u.delete();

Refer Testcase for more Details.

Query

  • JPA QueryJPA queries can be done using 

    List<Book> results = new Book().query("select b from Book b where b.bookId=1");
  • Native Query: Native queries (e.g. CQL queries for Cassandra) can be done using query() method with QueryType.NATIVE

    List results = new Book().query("select \"TITLE\" from \"Book\"", QueryType.NATIVE);

Refer Testcase for more Details.

Check Kundera documentation for more details. 

Conclusion

Kundera, being JPA compliant, makes it easier to work with NoSQL databases. This new feature will make it even easier. This allows users to perform operations on the object itself without taking care of JPA stuff. 

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
kundera ,big data ,orm

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}