4

I have recently started taking much interest in CQL as I am thinking to use Datastax Java driver. Previously, I was using column family instead of table and I was using Astyanax driver. I need to clarify something here-

I am using the below column family definition in my production cluster. And I can insert any arbitrary columns (with its value) on the fly without actually modifying the column family schema.

create column family FAMILY_DATA
with key_validation_class = 'UTF8Type'
and comparator = 'UTF8Type'
and default_validation_class = 'BytesType'
and gc_grace = 86400;

But after going through this post, it looks like- I need to alter the schema every time whenever I am getting a new column to insert which is not what I want to do... As I believe CQL3 requires column metadata to exist...

Is there any other way, I can still add arbitrary columns and its particular value if I am going with Datastax Java driver?

Any code samples/example will help me to understand better.. Thanks..

Community
  • 1
  • 1
arsenal
  • 23,366
  • 85
  • 225
  • 331
  • Any solid reasons to move away from Astayanax, is not supported for Cassandra 2.0 ? (*Asking this as I am also planning to move away from Hector to Astayanax or DS java driver, but moving from thrift to CQL altogether, does not look such a simple step*) – Rajat Gupta May 28 '14 at 09:17

2 Answers2

4

I believe in CQL you solve this problem using collections.

You can define the data type of a field to be a map, and then insert arbitrary numbers of key-value pairs into the map, that should mostly behave as dynamic columns did in traditional Thrift.

Something like:

CREATE TABLE data ( data_id int PRIMARY KEY, data_time long, data_values map );
INSERT INTO data (data_id, data_time, data_values) VALUES (1, 21341324, {'sum': 2134, 'avg': 44.5 });

Here is more information.

Additionally, you can find the mapping between the CQL3 types and the Java types used by the DataStax driver here.

Nikhil
  • 2,298
  • 13
  • 14
  • Thanks Nikhil.. What will be the data type for the Map? Can we insert string as the Key Name and BytesType as the value name within the map everytime? – arsenal Oct 01 '13 at 15:27
  • @TechGeeky Absolutely. You can specify the key/value types of the map in the `create table` definition. Eg: `create table data( data_id int primary key, data_time long, data_values map );`. When you insert data into said table, you will have to utilize a matching java.util.Map type. I have not tested it out myself, but I believe it should work fine. See the [CQL reference](http://cassandra.apache.org/doc/cql3/CQL.html#types) for more details. – Nikhil Oct 01 '13 at 18:32
0

If you enable compact storage for that table, it will be backwards compatible with thrift and CQL 2.0 both of which allow you to enter dynamic column names.

You can have as many columns of whatever name you want with this approach. The primary key is composed of two things, the first element which is the row_key and the remaining elements which when combined as a set form a single column name.

See the tweets example here

Though you've said this is in production already, it may not be possible to alter a table with existing data to use compact storage.

cs_alumnus
  • 1,579
  • 16
  • 24