Skip to content

Datastax java driver support for Cassandra using Kundera

chhavigangwal edited this page Jan 12, 2015 · 5 revisions

Introduction :

When CQL was introduced initially, it used Thrift as a network transport which had its own set of limitations and in order to overcome these limitations a Cassandra specific binary protocol was introduced later.

With the introduction of the Cassandra's binary protocol and based on user requests :#385,#467,#544 ; its support has been enabled in Kundera from 2.11 release via datastax java driver. Datastax java driver is client driver for Apache Cassandra that enables using (CQL3) via Cassandra's binary protocol.

In order to use it via Kundera :

Maven dependency

In order to use kundera-cassandra-ds-driver with maven based project, need to add:

<dependency>
            <groupId>com.impetus.kundera.client</groupId>
            <artifactId>kundera-cassandra-ds-driver</artifactId>
            <version>2.11</version>
</dependency>

Persistence unit configuration :

<persistence-unit name="ds_pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>
		<properties>
			<property name="kundera.nodes" value="localhost" />
			<property name="kundera.port" value="9042" />
			<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.kundera.client.cassandra.dsdriver.DSClientFactory" />
		</properties>
	</persistence-unit>

###Examples: CRUD :

  • Select
 Query findQuery = entityManager.createQuery("Select p from PersonCassandra p", PersonCassandra.class);
 List<PersonCassandra> allPersons = findQuery.getResultList();

findQuery = entityManager.createQuery("Select p.age from PersonCassandra p where p.personName = vivek");
  • Update
String updateQuery = "update PersonCassandra p set p.personName=''KK MISHRA'' where p.personId=1";
q = entityManager.createQuery(updateQuery);
q.executeUpdate();
  • Delete
 String deleteQuery = "DELETE from PersonCassandra";
 q = entityManager.createQuery(deleteQuery);

External Properties Configuration : DS specific properties can be set externally via property map as well :

DSClientFactory ds = new DSClientFactory();
final String RRP = "com.datastax.driver.core.policies.RoundRobinPolicy";
final String ERP = "com.datastax.driver.core.policies.ExponentialReconnectionPolicy";
final String DCRP = "com.datastax.driver.core.policies.FallthroughRetryPolicy";
Properties connectionProperties = CassandraPropertyReader.csmd.getConnectionProperties();

    ...

KunderaMetadata kunderaMetadata = ((EntityManagerFactoryImpl) emf).getKunderaMetadataInstance();
       ...

ds.initialize(propertyMap);
Object conn = ds.createPoolOrConnection();
Cluster cluster = (Cluster) conn;
HostDistance distance = HostDistance.LOCAL;
Configuration configuration = cluster.getConfiguration();

Pagination : You can paginate the query results fetched from Cassandra using Datastax driver via ResultIterator :

String queryString= "Select t from Token t";
com.impetus.kundera.query.Query query = (com.impetus.kundera.query.Query) em.createQuery(queryString,
                Token.class);
query.setFetchSize(fetchSize);
int count=0;
Iterator<Token> tokens = query.iterate();
while(tokens.hasNext())
{
  ...
 }

Examples to refer :

Clone this wiki locally