Posted on August 16th, 2011
(The High Performance Cassandra Cookbook was targeted on version 0.7.X, Composite types entered the Cassandra tree in version 0.8.1 and some minor changes were made in Cassandra to accommodate them)
CompositeTypes are an alternative to packing and serializing your own structures into cassandra’s columns. CompositeType allows a user to create a record which is a list of other Cassandra types such as UTF8Type or TimeUUID type. This recipe shows how to use the CompositeType information inside the comparators metadata.
How to do it…
Using the Cassandra CLI create a column family with a comparator specified as CompositeType that has two components which are UTF8 Strings.
[default@edstest] use edstest;
[default@edstest] create column family composite_test with comparator = ‘CompositeType(UTF8Type,UTF8Type)’;
You can insert into the composite type by using a : inside a single quoted string to separate the parts.
[default@edstest] set composite_test['a']['thing1:thing2']= ‘wow’;
[default@edstest] get composite_test['a'];
=> (column=thing1:thing2, value=776f77, timestamp=1313540404197000)
Returned 1 results.
Create another composite column this time using UTF8Type and IntegerType. Then confirm that you can not insert data of the wrong type.
[default@edstest] create column family composite_test2 with comparator = ‘CompositeType(UTF8Type,IntegerType)’;
[default@edstest] set composite_test2 ['a']['thing:1']=’stuff’;
[default@edstest] set composite_test2 ['a']['thing:z']=’stuff’;
org.apache.cassandra.db.marshal.MarshalException: unable to make int from ‘z’
[default@edstest] get composite_test2 ['a'];
=> (column=thing:1, value=7374756666, timestamp=1313540630087000)
Returned 1 results.
How it works…
Cassandra stores the CompositeType metadata. This information allows method such as get_slice to return data in a sorted order. This has an advantage over using a serialization method like JSON where Cassandra can only sort on the byte level, because it is unaware of the data’s structure.
There is more…
Composite columns may eventually replace ‘super column families’ in cassandra. This change should be transparent to end users of ‘super column families’ as far as the thrift API is concerned.
Leave a Reply
You must be logged in to post a comment.