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

Polyglot Persistence With Kundera (Example With KuduDB and MongoDB)

DZone's Guide to

Polyglot Persistence With Kundera (Example With KuduDB and MongoDB)

The polyglot persistence feature of Kundera can be used to create applications on multiple datastores for real-world use cases.

· Database Zone ·
Free Resource

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 

In the world of Big Data, we have a lot of datastores solving various problems in application development and data storage. Some are better for storing fast data, some for storing relationship graphs, some provide consistency in data, and some give high availability. Using a single datastore would compromise few features that an application might need, resulting in poor performance.

Kundera solves this problem by providing polyglot persistence out of the box. You can store data in more than one datastore as simply changing a configuration file.

Kundera is a powerful JPA-based object-datastore mapping library (similar to an ORM). It significantly reduces programming efforts required for performing CRUD operations on NoSQL datastores. With the polyglot feature of Kundera, you can make your business objects distributed across multiple datastores. All you have to do is create entity classes and their relationships and let Kundera know which entity should be stored in which datastore. CRUD operations can be performed using the JPA interface and Kundera automatically stores/searches different parts of domain entity on their respective datastores.

Example: We will try to store a Person entity in KuduDB and an Address entity in MongoDB.

Person.java:

@Entity
@Table(name = "PERSON", schema = "kundera_kudu@kudu_pu")
public class Person {

 @Id
 @Column(name = "PERSON_ID")
 private String personId;

 @Column(name = "PERSON_NAME")
 private String personName;

 @Column(name = "AGE")
 private int age;

 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
 @JoinColumn(name = "ADDRESS_ID")
 private List < Address > addresses;

}

Address.java:

@Entity
@Table(name = "ADDRESS", schema = "kundera_mongo@mongo_pu")
public class Address {

 @Id
 @Column(name = "ADDRESS_ID")
 private String addressId;

 @Column(name = "STREET")
 private String street;

 @Column(name = "CITY")
 private String city;

 @Column(name = "COUNTRY")
 private String country;

}

persistence.xml:

<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="mongo_pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>
		<class>com.impetus.kundera.entities.Address</class>
		<exclude-unlisted-classes>true</exclude-unlisted-classes>
		<properties>
			<property name="kundera.nodes" value="localhost" />
			<property name="kundera.port" value="27017" />
			<property name="kundera.keyspace" value="kundera_mongo" />
			<property name="kundera.dialect" value="mongodb" />
			<property name="kundera.ddl.auto.prepare" value="create" />
			<property name="kundera.client.lookup.class"
value="com.impetus.client.mongodb.MongoDBClientFactory" />
		</properties>
	</persistence-unit>
	<persistence-unit name="kudu_pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>
		<class>com.impetus.kundera.entities.Person</class>
		<exclude-unlisted-classes>true</exclude-unlisted-classes>
		<properties>
			<property name="kundera.nodes" value="quickstart.cloudera" />
			<property name="kundera.port" value="7051" />
			<property name="kundera.keyspace" value="kundera_kudu" />
			<property name="kundera.client" value="kudu" />
			<property name="kundera.ddl.auto.prepare" value="create" />
			<property name="kundera.client.lookup.class" value="com.impetus.client.kudu.KuduDBClientFactory" />
		</properties>
	</persistence-unit>
</persistence>

With the above minimal configurations, we will be able to store Person in KuduDB and Address in MongoDB.

CRUD operations can be performed in a simple way as follows:

Insert:

Person p = new Person();
p.setPersonId("101");
p.setPersonName("dev");
p.setAge(24);

Address a1 = new Address("a1", "str1", "noida", "india");
Address a2 = new Address("a2", "str2", "bglr", "india");

List < Address > adds = new ArrayList < Address > ();
adds.add(a1);
adds.add(a2);

p.setAddresses(adds);

em.persist(p);

Read:

Person person = em.find(Person.class, "101");

Update:

Person person = em.find(Person.class, "101");
person.setPersonName("devender");
em.merge(person);

Delete:

Person p = em.find(Person.class, "101");
em.remove(p);

Conclusion

Kundera reduces the learning curve for NoSQL datastores incredibly and makes application development faster than ever. The polyglot persistence feature of Kundera can be used to create applications on multiple datastores for real-world use cases.

Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

Topics:
database ,kundera ,mongodb ,kududb ,datastores ,persistence ,tutorial ,polyglot

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}