Skip to content

Getting Started in 5 minutes

Vivek Mishra edited this page Jan 9, 2014 · 70 revisions

We are going to give an example on persisting an object into Cassandra. But you are free to choose your own favourite data-store. (HBase, MongoDB and any relational database are other supported options in Kundera as of now.)

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 you pom.xml for your maven project and running

mvn eclipse:clean eclipse:eclipse

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>
	<repositories>
		<repository>
		    <id>sonatype-nexus</id>
		    <name>Kundera Public Repository</name>
		    <url>https://oss.sonatype.org/content/repositories/releases</url>
		    <releases>
			<enabled>true</enabled>
		    </releases>
		    <snapshots>
			<enabled>false</enabled>
		    </snapshots>
	      </repository>
  	      <repository>
		<id>kundera-missing</id>
		<name>Kundera Public Missing Resources Repository</name>
		<url>http://kundera.googlecode.com/svn/maven2/maven-missing-resources</url>
		<releases>
			<enabled>true</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</repository>
	</repositories>
	<dependencies>
		<dependency>
			<groupId>com.impetus.client</groupId>
			<artifactId>kundera-cassandra</artifactId>
			<version>2.6</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Note

With 2.10 release artifact group id has been changed from

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

TO com.impetus.kundera.client kundera-cassandra ${kundera.version}

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.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
			<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>

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

amresh@impetus-ubuntu:/usr/local/apache-cassandra-1.2.5/bin$ ./cassandra-cli --host localhost --port 9160
Connected to: "Test Cluster" on localhost/9160
Welcome to the Cassandra CLI.
Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.
[default@unknown]create keyspace KunderaExamples;
[default@unknown]use KunderaExamples;
[default@KunderaExamples]create column family users with comparator=UTF8Type and default_validation_class=UTF8Type and key_validation_class=UTF8Type;

81852270-2374-11e1-0000-242d50cf1fdd
Waiting for schema agreement...
... schemas agree across the cluster

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");
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu");
        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.

Home

Clone this wiki locally