View on GitHub

Cassandra-driver-mapping

JPA addon for DataStax Java Driver for Cassandra

Download this project as a .zip file Download this project as a tar.gz file

cassandra-driver-mapping

Entity mapping Add-on for the DataStax Java Driver (Driver) for Cassandra (C*).
This Add-on allows you to generate schema automatically and persist JPA annotated entities in C*.

Add-on is not replacement for the Driver but lightweight utility for it.
You still can utilize full power of the Driver API and Datastax documentation.
Mapping Add-on relies on Driver version 2.XXX and JPA 2.1.

Read more about Datastax Java Driver, Cassandra and CQL3.

More Usage Samples in Unit Tests

Table of Contents

Features

The features provided by the module include:

No mapping files, no scripts, no configuration files.
You don't have to worry about creating the Table and Indexes for your Entity manually.
All is built-in and taken care of. Entity definition will be automatically synchronized with C*.

Jump Start

    <dependency>
      <groupId>com.valchkou.datastax</groupId>
      <artifactId>cassandra-driver-mapping</artifactId>
      <version>2.1.0-rc1</version>
    </dependency>

All new changes and bugfixes are released within the latest version as soon as coded. Module versioning policy matches underlying datastax driver core versioning.

    import com.datastax.driver.core.Session;
    import com.datastax.driver.mapping.MappingSession;
    ...

    Session session; // initialize datastax session.
    MappingSession mappingSession = new MappingSession("keyspace_name", session);

If you wish your mapping session do not synchronize your entities with C* you may turn synch off:

    MappingSession mappingSession = new MappingSession("keyspace_name", session, true);
    // OR
    MappingSession mappingSession = new MappingSession("keyspace_name", session);
    mappingSession.setDoNotSync(true);

Underlying Datastax Session does all the heavylifting and is expansive.
Prior using MappingSession you need to open the Datastax Session and create the Keyspace using the standard Datastax Driver API. If you are not familiar with procedure please refer to Datastax Dcumentation.
Or look at the Spring Framework Example.

    Entity entity = new Entity();
    mappingSession.save(entity);

OR

    import com.datastax.driver.mapping.option.WriteOptions;
    import com.datastax.driver.core.policies.DefaultRetryPolicy;
    import com.datastax.driver.core.ConsistencyLevel;
    ...
    // using options
    WriteOptions options = new WriteOptions()
        .setTtl(300)
        .setTimestamp(42)
        .setConsistencyLevel(ConsistencyLevel.ANY)
        .setRetryPolicy(DefaultRetryPolicy.INSTANCE);

    Entity entity = new Entity();
    mappingSession.save(entity, options);

    Entity entity = mappingSession.get(Entity.class, id);

OR

    import com.datastax.driver.mapping.option.ReadOptions;
    import com.datastax.driver.core.policies.DefaultRetryPolicy;
    import com.datastax.driver.core.ConsistencyLevel;
    ...
    // using options
    ReadOptions options = new ReadOptions()
        .setConsistencyLevel(ConsistencyLevel.ANY)
        .setRetryPolicy(DefaultRetryPolicy.INSTANCE);

    Entity entity = mappingSession.get(Entity.class, id, options);

    mappingSession.delete(entity);  

Various Mappings

IMPORTANT!!!  
- If entity or field is not annotated it will provide its name as default.    
- Id field is required and must be annotated with @Id or @EmbeddedId.
- Index name must be unique within the keyspace.  
- C* supports only single-column-index.

Basic Mapping

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column

@Table (name="mytable")
public class Entity {

    @Id
    private long Id;

    @Column(name = "myname")
    private String name;

    // @Column is not required
    private int age;

    @Transient
    private BigDecimal calculable;

    // public getters/setters ...
}

CQL3 Statement

   CREATE TABLE IF NOT EXISTS ks.mytable (id bigint, myname text, age int, PRIMARY KEY(id))

Mapping Indexes

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column
import javax.persistence.Index
import java.util.UUID

@Table (name="mytable", 
indexes = {
    @Index(name="entity_email_idx", columnList="email" ), 
    @Index(name="entity_name_idx", columnList="myname" ) 
})
public class Entity {

    @Id
    private java.util.UUID code;

    @Column(name = "myname")
    private String name;
    private String email;
    // public getters/setters ...
}

CQL3 Statement

    CREATE TABLE IF NOT EXISTS ks.mytable (code uuid, myname text, email text,  PRIMARY KEY(code)); 
    CREATE INDEX IF NOT EXISTS entity_email_idx ON ks.mytable(email);  
    CREATE INDEX IF NOT EXISTS entity_name_idx ON ks.mytable(myname);

Compound Primary Key

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.Id;
import javax.persistence.Table;

@Table(name="entity")
public class Entity {
    @Id
    private java.util.UUID id;
    private List<String> cats;
    private Set<Date> dogs;
    private Map<String, BigInteger> pets;

    // public getters/setters ...
}
import javax.persistence.Embeddable;    

@Embeddable
public class CompositeKey {
    private String name;
    private int rank;
    // public getters/setters ...
}
import javax.persistence.Table;
import javax.persistence.EmbeddedId;    

@Table(name="entity")
public class Entity {
    @EmbeddedId
    private CompositeKey key;
    private String email;
    // public getters/setters ...
}

CQL3 Statement

   CREATE TABLE IF NOT EXISTS ks.entity (name text,  rank int, email text,  PRIMARY KEY(name, rank))

Composite Partition Key

import javax.persistence.Embeddable;    

@Embeddable
public class PartitionKey {
    private String firstName;
    private String lastName;
    // public getters/setters ...
}
import javax.persistence.Embeddable;    

@Embeddable
public class CompositeKey {
    @EmbeddedId
    private PartitionKey key;
    private int age;
    // public getters/setters ...
}
import javax.persistence.Table;
import javax.persistence.EmbeddedId;    

@Table(name="entity")
public class Entity {
    @EmbeddedId
    private CompositeKey key;
    private String email;
    // public getters/setters ...
}

CQL3 Statement

   CREATE TABLE IF NOT EXISTS ks.entity (firstname text, lastname text, age int, email text,  PRIMARY KEY((firstname, lastname), age))

Table Properties

This feature is not JPA standard! Read more about C* Table properties

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column

import com.datastax.driver.mapping.annotation.TableProperties;
import com.datastax.driver.mapping.annotation.TableProperty;

@Table (name="mytable")
@TableProperties(values = {
    @TableProperty(value="comment='Important records'"),
    @TableProperty(value="read_repair_chance = 1.0"),
    @TableProperty(value="compression ={ 'sstable_compression' : 'DeflateCompressor', 'chunk_length_kb' : 64 }")
})
public class Entity {

    @Id
    private long Id;
    private String name;
    // public getters/setters ...
}

CQL3 Statement

   CREATE TABLE IF NOT EXISTS ks.mytable (id bigint, name text, PRIMARY KEY(id)) WITH comment='Important records' AND read_repair_chance = 1.0 AND compression ={ 'sstable_compression' : 'DeflateCompressor', 'chunk_length_kb' : 64 }

Override Column Data Type.

Datastax defines data type mapping from Java to C*.
This addon defines opposite way mapping. You can explore daults here.
But in case you don't like defaults you are able to override the type on the column level.
For example you want to leverage "time UUID" for timeseries data instead of "random UUID".

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column

@Table (name="mytable")
public class Entity {

    @Id
    @Column(name="uid", columnDefinition="timeuuid") // case insensitive
    private UUID uid;       

    @Column(name="name", columnDefinition="VarChaR") // case insensitive
    private String name;
    // public getters/setters ...
}

CQL3 Statement

   CREATE TABLE IF NOT EXISTS ks.mytable (uid timeuuid, name varchar, PRIMARY KEY(uid))

Mixed Case for Column Names

C* converts all names to lowercase. This is default and recommended approach.
But in case you need enforce the case you will need to wrap you names in double quotes.

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column

@Table (name="mytable")
public class Entity {

    @Id
    @Column(name = "\"KEY\"")
    private int id;
    private String firstName;

    @Column(name = "\"last_NAME\"")
    private String lastName;

    @Column(name = "AGE")
    private int age;
    // public getters/setters ...
}

CQL3 Statement

   CREATE TABLE IF NOT EXISTS ks.mytable ("KEY" int, firstName text, "last_NAME" text, AGE int, PRIMARY KEY("KEY"))

Collections

Mapping

Collections must have generic type defined. Only java.util.List, Map and Set are allowed.
By default implementation of HashMap, HashSet and ArrayList are used. If you are unhappy with that fact and would like your data to be baked with specific collection implementation you can apply an annotation as shown below.

    import com.datastax.driver.mapping.annotation.CollectionType;
    ...
    @CollectionType(LinkedList.class)
    private List<String> cats;

    @CollectionType(TreeSet.class)
    private Set<Date> dogs;

    @CollectionType(TreeMap.class)
    private Map<String, BigInteger> pets;
}

NOTE: this is strictly java side feature and does not effect how your data stored in C*.

CQL3 Statement

   CREATE TABLE IF NOT EXISTS ks.entity (id uuid, cats list<text>, dogs set<timestamp>, pets map<text, varint>,  PRIMARY KEY(id))

For more info on collections please refer Datastax Using Collection

Optimized operations

You can work with your collection properties as you would normally work with other entity properties.
In addition C* provides optimized operations on collections. Those operations do not require to load and save the whole entity. C* allows us directly manipulate collections.

// append item to list
mappingSession.append(id, Entity.class, "cats", "Black Cat");

// append item to be expired in 5 sec
mappingSession.append(id, Entity.class, "cats", "Expired Cat", new WriteOptions().setTtl(5));

// prepend item
mappingSession.prepend(id, Entity.class, "cats", "First Cat");

// replace item at specified index
mappingSession.replaceAt(id, Entity.class, "cats", "Grey Cat", 1);

// append List of items
List<String> addCats = new ArrayList<String>();
addCats.add("Red Cat");
addCats.add("Green Cat");
mappingSession.append(id, Entity.class, "cats", addCats);

// remove item
mappingSession.remove(id, Entity.class, "cats", "Grey Cat");

// remove List of items
List<String> removeCats = new ArrayList<String>();
removeCats.add("Red Cat");
removeCats.add("Green Cat");
mappingSession.remove(id, Entity.class, "cats", removeCats);

// remove all items
mappingSession.deleteValue(id, Entity.class, "cats");

// append item
mappingSession.append(id, Entity.class, "dogs", "Black Dog");

// append item to be expired in 5 sec
mappingSession.append(id, Entity.class, "dogs", "Expired Dog", new WriteOptions().setTtl(5));

// append Set of items
Set<String> addDogs = new HashSet<String>();
addDogs.add("Red Dog");
addDogs.add("Green Dog");
mappingSession.append(id, Entity.class, "dogs", addDogs);

// remove item
mappingSession.remove(id, Entity.class, "dogs", "Black Dog");

// remove Set of items
Set<String> removeDogs = new HashSet<String>();
removeDogs.add("Red Dog");
removeDogs.add("Green Dog");
mappingSession.remove(id, Entity.class, "dogs", removeDogs);

// remove all items
mappingSession.deleteValue(id, Entity.class, "dogs");

/** append item */
Map<String, BigInteger> pets = new HashMap<String, BigInteger>();
pets.put("Red Dogs", 25);
pets.put("Black Cats", 50);
mappingSession.append(id, Entity.class, "pets", pets);

/** append items to be expired in 5 sec */
Map<String, BigInteger> pets = new HashMap<String, BigInteger>();
pets.put("Green Dogs", 25);
pets.put("Brown Cats", 50);
mappingSession.append(id, Entity.class, "pets", pets, new WriteOptions().setTtl(5));

/** remove all items */
mappingSession.deleteValue(id, Entity.class, "pets");

Optimistic Lock

C* does not support locking. But it provides ability for Optimistic Concurrency Control.
While running, transactions use data resources without acquiring locks on those resources. Before committing, each transaction verifies that no other transaction has modified the data it has read. If the check reveals conflicting modifications, the committing transaction rolls back and can be restarted.
This section explains how you can achieve this with C* and Mapping Add-on

    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Version;   

    @Table(name="entity")
    public class EntityWithVersion {
        @Id
        private java.util.UUID id;

        @Version
        private long version;   
        // public getters/setters ...
    }

    @Test
    public void entityWithVersionTest() throws Exception {
        UUID id = UUID.randomUUID();
        EntityWithVersion obj = new EntityWithVersion();
        obj.setId(id);
        obj.setName("ver1"); 

        EntityWithVersion loaded = mappingSession.get(EntityWithVersion.class, id);
        assertNull(loaded);

        // save object ver1 
        EntityWithVersion saved = mappingSession.save(obj);

        // get object ver1
        EntityWithVersion obj1 = mappingSession.get(EntityWithVersion.class, id);
        assertEquals(obj1, saved);
        assertEquals(1, saved.getVersion());

        // save object ver2
        saved = mappingSession.save(saved);
        EntityWithVersion obj2 = mappingSession.get(EntityWithVersion.class, id);
        assertEquals(obj2, saved);
        assertEquals(2, saved.getVersion());        

        saved = mappingSession.save(obj1);
        assertNull(saved);
    }       

Batch

    mappingSession.withBatch()
        .save(entityA)
        .save(entityB)
        .delete(entityC)
        .delete(entityD)
        .execute();

Nested Entities

This section shows how you can support nested entities with C* and Mapping Add-on.

    @Table(name="entity_a")
    public class EntityA {
        @Id
        private UUID id;

        // public getters/setters ...
    }

    @Table(name="entity_b")
    public class EntityB {
        @Id
        private UUID id;

        // reference on EntityA 
        private UUID refA;
        // public getters/setters ...
    }   

    public class TestNested() {

        @Test
        public void saveNested() throws Exception {
            EntityA a = new EntityA();
            mappingSession.save(a);

            EntityB b = new EntityB();
            b.setRefA(a.getId());
            mappingSession.save(b);
        }

        @Test
        public void loadNested() throws Exception {
            UUID bId = some_id;
            EntityB b = mappingSession.load(bId);
            EntityA a = mappingSession.load(b.getRefA());
        }

    }

Mapping Custom Queries

    public class AnyObject {
        private String name;
        private int age;
        // public getters/setters ...
    }

You can populate this object from any ResultSet which contains 'name' and 'age' columns.

    ResultSet rs = session.execute("SELECT name, age, birth_date, salary FROM person"); 
    List<AnyObject> result = mappingSession.getFromResultSet(AnyObject.class, rs);

In this particular case 'name' and 'age' will be populated on 'AnyObject'. 'birth_date' and 'salary' will be ignored and no errors will be thrown.
The biggest advantage that we can reuse the same entity to query different results from even different tables. Entity doesn't have to map, match or relate to the table at all. Many thank to magic gnomes under the hood making all these work.

Building Custom Queries

    import com.datastax.driver.mapping.MappingSession;
    ... 

    // build query
    String query = "SELECT name, age, birth_date, salary FROM person"); 

    // run query                        
    List<Entity> result = mappingSession.getByQuery(Entity.class, query);   

    import com.datastax.driver.core.Statement;
    import com.datastax.driver.core.querybuilder.QueryBuilder;
    import com.datastax.driver.mapping.MappingSession;
    ...

    // build query
    Statement query = QueryBuilder.select().all()
        .from("your_keyspace", "your_table").where(eq("column", value));

    // run query                        
    List<Entity> result = mappingSession.getByQuery(Entity.class, query);

    import com.datastax.driver.core.Statement;
    import com.datastax.driver.core.querybuilder.QueryBuilder;
    import com.datastax.driver.mapping.MappingSession;
    import com.datastax.driver.mapping.EntityFieldMetaData;
    import com.datastax.driver.mapping.EntityTypeMetadata;  
    ...         

    // get Entity Metadata
    EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(Entity.class);

    // get field metadata by property/field name
    EntityFieldMetaData fmeta = emeta.getFieldMetadata(field_name); 

    // build query.
    Statement query = QueryBuilder.select().all()
        .from("your_keyspace", emeta.getTableName()).where(eq(fmeta.getColumnName(), value));

    // run query
    List<Entity> result = mappingSession.getByQuery(Entity.class, query);

Under The Hood

Prepared Statement Cache

For the performance gain most update/select/delete statements are built as Prepared Statements. Prepared Statements are reusable and placed in the static cache. Cache is Guava Cache implementation initialized as:

.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(1000)
.concurrencyLevel(4)

If you want to tune the cache for better performance you can do it as:

Cache<String, PreparedStatement> cache = CacheBuilder
    .newBuilder()
    .expireAfterAccess(60, TimeUnit.MINUTES)
    .maximumSize(10000)
    .concurrencyLevel(8)
    .build();

MappingSession.setStatementCache(cache);

More about Guava Cache

How Entity get synchronized

The table structure is automatically synchronized with the entity definition on the first use of the entity.
Any SessionMapping call internally will check if the entity has already been synchronized and if not
it will run SchemaSync.sync. You can use sync API directly as:

    // create or alter
    import com.datastax.driver.mapping.schemasync.SchemaSync;
    ...
    SchemaSync.sync(keyspace, session, Entity.class);
    // drop table
    import com.datastax.driver.mapping.schemasync.SchemaSync;
    ...
    SchemaSync.drop(keyspace, session, Entity.class);

You don't need to use this API unless you have reasons.
Such as unittests or if you want to gain few milliseconds on the first use
you may want to invoke the synchronization on the application start up instead.

As the project is evolving sometimes there is need to refactor entity, add or delete properties and indexes. Again this all taken care automatically but with certain restrictions.
Please read to understand what will and will not be altered and synchronized.

Not Alterable

Alterable

Entity Metadata and Data Types

You may want to access Entity metadata if you are building custom Statements.
Entity Metadata contains corresponding table and column names.
Entity Metadata can be easily accessed anywhere in your code as:

    EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(Entity.class)
    emeta.getTableName(); // corresponding table name in C*

    // get field meta info by property name
    EntityFieldMetaData fdata = emeta.getFieldMetadata("email");

    // corresponding column name in C*
    String columnName = fdata.getColumnName(); 

     // all the persistent fields on entity
    List<EntityFieldMetaData> fields = emeta.getFields();

Datastax driver has mapping of datastax types to java. But not all types are mapped as 1-to-1.
CQL3 data types to Java types
In order the mapping to work the module defines backward mapping for the types.

Java type CQL3 data type
int int
long bigint
float float
double double
boolean boolean
java.lang.Double double
java.nio.ByteBuffer blob
java.math.BigDecimal decimal
java.lang.String text
java.util.Date timestamp
java.lang.Boolean boolean
java.lang.Integer int
java.lang.Long bigint
java.util.Map map
java.lang.Float float
java.util.Set set
java.math.BigInteger varint
java.util.UUID uuid
java.util.List list

You can override defaults as:

    import com.datastax.driver.core.DataType;
    ...
    Map<Class<?>, DataType.Name> mapping = new HashMap<Class<?>, DataType.Name>();
    .... populate the map
    EntityTypeParser.setDataTypeMapping(mapping);

Or override individual type:

    import com.datastax.driver.core.DataType;
    ...
    EntityTypeParser.overrideDataTypeMapping(javaClass, DataType.Name)

Spring Framework Example