-
Notifications
You must be signed in to change notification settings - Fork 0
Cassandra Specific Features
Kundera allows you to specify data-store specific hooks into a configuration file. You need to add below property into persistence.xml for your persistence unit. Here kundera-cassandra.properties is the name of configuration file that must be in classpath of your application:
<property name="kundera.client.property" value="kundera-cassandra.properties" />
You specify properties in this file as key-value pairs.
In order to set replication factor, just add below line into kundera-cassandra.properties:
replication_factor=n
where 'n' is an integer number, e.g. 3
For setting placement strategy, add below line into kundera-cassandra.properties:
placement_strategy=org.apache.cassandra.locator.SimpleStrategy
OR
placement_strategy=org.apache.cassandra.locator.NetworkTopologyStrategy
Counter column families, available starting with Cassandra 0.8.0, are supported in Kundera. They are specified in kundera-cassandra.properties file as comma separated list:
cf_defs=<Counter column family Name>|<Default Validation Class>|<Comparator Type>
An example entry is:
cf_defs=MyCounterColumnFamily1|CounterColumnType|UTF8Type,MyCounterColumnFamily2|CounterColumnType|UTF8Type
Please note that should be same as the table name specified at entity class definition, like this:
@Entity
@Table(name = "MyCounterColumnFamily1", schema = "KunderaExamples@cassandra_pu")
public class WebPageHitCounter
{
@Id
private String id;
@Column
private int counter;
//getters and setters
}
A sample property file is available here.
For example on how to use this feature, refer to test class CountersTest and SuperCountersTest.
As a default behavior, Kundera uses Cassandra's secondary indexing mechanism for querying on columns in a column family. A limitation of secondary indexing is that it doesn't support querying on columns within super columns.
To overcome this limitation, users have an option to use Lucene Indexing, which can be switched on by specifying "index.home.dir" property in persistence.xml. It enables users to run queries in column families as well as super column families.
For those who don't want to use Lucene indexing because of performance or other reasons, inverted indexing is an alternative. You can turn it on via specifying below property in kundera-cassandra.properties:
inverted.indexing.enabled=true
It enables insertion of records in inverted index column family when user inserts records in the original column family. When you run JPA query for searching a column within a super column, Kundera searches in inverted index first, retrieves Row key, and then finds records from original column family.
For an example: See TwissandraTest.
Before running native queries (CQL in Cassandra), you can specify CQL version. Here is how to do it:
EntityManagerFactoryImpl emf = Persistence.createEntityManagerFactory("cassandra_pu");
EntityManager em = emf.createEntityManager();
Map<String, Client> clientMap = (Map<String, Client>) em.getDelegate();
PelopsClient pc = (PelopsClient)clientMap.get("cassandra_pu");
pc.setCqlVersion("3.0.0"); //Other possible value is 2.0.0
String nativeQuery = "SELECT * FROM users WHERE state='UT' AND birth_date > 1970";
Query q = em.createNativeQuery(nativeQuery, User.class); //User is entity class
List<User> results = q.getResultList();
An example test case is available here.