Skip to content

Getting Started in 5 minutes

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

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.

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.impetus.poc</groupId>
	<artifactId>KunderaPOC</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>KunderaPOC</name>
	<url>http://maven.apache.org</url>
	
	<dependencies>
		<dependency>
			<groupId>com.impetus.kundera.client</groupId>
			<artifactId>kundera-cassandra</artifactId>
			<version>3.9</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Write persistence.xml file

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

<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
	http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	version="2.0">
	<persistence-unit name="cassandra_pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>		
		<properties>			
			<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="kundera.ddl.auto.prepare" value="create" />
			<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />			
		</properties>		
	</persistence-unit>
</persistence>

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;

@Entity
@Table(name = "users", schema = "KunderaExamples@cassandra_pu")
public class User 
{
    @Id
    private String userId;
    
    @Column(name="first_name")
    private String firstName;
    
    @Column(name="last_name")
    private String lastName;
    
    @Column(name="city")
    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)
    {
        this.city = city;
    } 
}

Moment of Truth!

Create Keyspace and Column family in cassandra

If you are using kundera.ddl.auto.prepare 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 [email protected] 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();
        user.setUserId("0001");
        user.setFirstName("John");
        user.setLastName("Smith");
        user.setCity("London");
        
        //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();
        
        em.persist(user);
        em.close();    
        emf.close();    
    }
}

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

		<dependency>
			<groupId>com.impetus.client</groupId>
			<artifactId>kundera-cassandra</artifactId>
			<version>2.9</version>
		</dependency>

TO

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

Home

Clone this wiki locally