Sunday, 11 December 2016

The List Column Type in Cassandra

1. Inserting and Updating

import collection.JavaConversions._
val query = QueryBuilder.insertInto(keyspace, table) .value("postid", post.postid) .value("typeid", post.typeid) .value("tags", seqAsJavaList(post.tags)) .value("creationdate", post.creationdate)

val query = QueryBuilder.update(keyspace, table)
.`with`(QueryBuilder.set("tags", seqAsJavaList(tags)))
.and(QueryBuilder.set("creationdate", epoch))
.where(QueryBuilder.eq("postid", postid))
session.execute(query)

Since Cassandra has no Scala library, we have to make sure the type are interoperatable between Scala and Java. The atomic types are interoperatable by default. But we have to convert Scala collection to Java collection.


2. Querying

To query the elements of a collection column:
cqlsh> create index on test.posts (tags);
cqlsh> select * from test.posts where tags contains 'AB';

To ensure type safety, collection getters are generic. You need to provide type parameters matching your CQL type when calling the methods:

// Assuming given_names is a list<text>:

List<String> givenNames = row.getList("given_names", String.class);

Like a typical distributed database, Cassandra stores all changes as immutable events. It can’t simply go back and mutate a record like a relational database would do. Every time a change occurs a new, immutable event is created (which is represented as an sstable row and can be further compacted, flushed, propagated). 
When a delete occurs of a column or row a tombstone event is created which is appended, in a temporal sense, to the events that have occurred so far. A tombstone is cassandra’s record of a deletion.

Reference:
http://docs.datastax.com/en/developer/java-driver/3.1/manual/

No comments:

Post a Comment