Getting Started in 5 minutes

Devender Yadav edited this page Aug 7, 2017 · 70 revisions
Clone this wiki locally

We are going to give an example on persisting an object into Cassandra. But you are free to choose your own favorite data-store.

Set up Cassandra server

We're not going to delve into this as we assume you already have Cassandra server up and running on your machine. If not, there is a pretty good link to help you out. (This example assumes your cassandra server version is 1.x)

Download and include Kundera Jar

You can download Kundera dependency jar (only kundera-cassandra required for this example) from maven repository by including below code snippet into your pom.xml for your maven project.


<project xmlns="" xmlns:xsi=""

Write persistence.xml file

Make sure to put it under a META-INF folder in your classpath.

<persistence xmlns="" xmlns:xsi=""
	<persistence-unit name="cassandra_pu">
			<property name="kundera.nodes" value="localhost"/>
			<property name="kundera.port" value="9160"/>
			<property name="kundera.keyspace" value="KunderaExamples"/>
			<property name="kundera.dialect" value="cassandra"/>
            <property name="" value="create" />
			<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />			

You can build the project by running mvn clean install -DskipTests command.

Write Entity class

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "users", schema = "KunderaExamples@cassandra_pu")
public class User 
    private String userId;
    private String firstName;
    private String lastName;
    private String city;    

    public User()

    public String getUserId()
        return userId;
    public void setUserId(String userId)
        this.userId = userId;
    public String getFirstName()
        return firstName;
    public void setFirstName(String firstName)
        this.firstName = firstName;
    public String getLastName()
        return lastName;
    public void setLastName(String lastName)
        this.lastName = lastName;
    public String getCity()
        return city;
    public void setCity(String city)
    { = city;

Moment of Truth!

Create Keyspace and Column family in cassandra

If you are using property in your persistence.xml (included above), there is no need to create keyspace and tables in Cassandra.

Run below program.

If it throws any error, check our Troubleshooting section. If you are stuck somewhere, you can raise your issue on or here.

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class KunderaExample
    public static void main(String[] args)
        User user = new User();
        //enable CQL3
        Map<String, String> props = new HashMap<>();
        props.put(CassandraConstants.CQL_VERSION, CassandraConstants.CQL_VERSION_3_0);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu", props);
        EntityManager em = emf.createEntityManager();

Validate the persisted data from cassandra-cli

[default@unknown] use KunderaExamples;
Authenticated to keyspace: KunderaExamples
[default@KunderaExamples] list users;
Using default limit of 100
RowKey: 0001
=> (column=city, value=London, timestamp=1323551942371)
=> (column=first_name, value=John, timestamp=1323551942371)
=> (column=last_name, value=Smith, timestamp=1323551942371)

1 Row Returned.

Note :

  • Kundera from 2.12 release onwards has become JPA2.1 compliant whose one of the per-requisite is JDK1.7. Support over JPA 2.0 can be found in older branches (kundera-{project-version}-1.x)

  • With 2.9.1 release onwards artifact group id has been changed from