-
Notifications
You must be signed in to change notification settings - Fork 233
Getting Started in 5 minutes
We are going to give an example on persisting an object into Cassandra. But you are free to choose your own favorite data-store.
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)
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="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>
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.
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;
}
}
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.
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();
}
}
[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.
-
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>
-
Datastores Supported
- Releases
-
Architecture
-
Concepts
-
Getting Started in 5 minutes
-
Features
- Object Mapper
- Polyglot Persistence
- Queries Support
- JPQL (JPA Query Language)
- Native Queries
- Batch insert update
- Schema Generation
- Primary Key Auto generation
- Transaction Management
- REST Based Access
- Geospatial Persistence and Queries
- Graph Database Support
-
Composite Keys
-
No hard annotation for schema
-
Support for Mapped superclass
-
Object to NoSQL Data Mapping
-
Cassandra's User Defined Types and Indexes on Collections
-
Support for aggregation
- Scalar Queries over Cassandra
- Connection pooling using Kundera Cassandra
- Configuration
-
Kundera with Couchdb
-
Kundera with Elasticsearch
-
Kundera with HBase
-
Kundera with Kudu
-
Kundera with RethinkDB
-
Kundera with MongoDB
-
Kundera with OracleNoSQL
-
Kundera with Redis
-
Kundera with Spark
-
Extend Kundera
- Sample Codes and Examples
-
Blogs and Articles
-
Tutorials
* Kundera with Openshift
* Kundera with Play Framework
* Kundera with GWT
* Kundera with JBoss
* Kundera with Spring
-
Performance
-
Troubleshooting
-
FAQ
- Production deployments
- Feedback