1

I am designing the Column Family for our use case in Cassandra. I am planning to go with Dynamic Column Structure.

Below is my requirement per our use case-

user-id   column1                                        column2                                     column3
123      (Column1-Value  Column1-SchemaName  LMD)       (Column2-Value Column2-SchemaName  LMD)     (Column3-Value  Column3-SchemaName  LMD)

For each user-id, we will be storing column1 and its value and that value will store these three things always-

(Column1-Value   Column1-SchemaName     LMD)

In my above example, I have show only three columns but it might have more columns.

Now I am not sure, how to store these three thing always at a column value level? Should I use composite columns at a column level? if yes, then I am not sure how to make a column family like this in Cassandra.

Column1-value will be in binary, Column1-SchemaName will be String, LMD will be DateType.

This is what I have so far-

create column family USER_DATA
with key_validation_class = 'UTF8Type'
and comparator = 'UTF8Type'
and default_validation_class = 'UTF8Type'
and gc_grace = 86400
and column_metadata = [ {column_name : 'lmd', validation_class : DateType}];

Can anyone help me in designing the column family for this?

2 Answers 2

1

@TechGeeky

Change the comparator to :

comparator = 'CompositeType(ByteType,UTF8Type,DateType)'

ByteType for Column-Value

UTF8Type for Column-SChemaName

DateType for LMD

Be careful though, with this design, querying your data other than by user-id would be hard. Especially you would be able to get slices of columns by providing column data (in bytes), if you know them before hand...

8
  • I am confuse with this approach.. comparator is for column name.. Right? But I need composite values.. Each columns will have composite values. In my case, I need to have three values inside a single column. And that column name can be e1, e2 or e3 or anything in my case..
    – arsenal
    Sep 20, 2013 at 21:18
  • "comparator is for column name.. Right?" --> Exact. In this case your column name will be a composite value of v1:v2:v3. v1 is a byte[], v2 is a string and v3 is a date
    – doanduyhai
    Sep 20, 2013 at 22:12
  • I see but what I need is - columnName should be single string like e1 and column value should be a composite value of v1:v2:v3. v1 is a byte[], v2 is a string and v3 is a date. Is this possible to do?
    – arsenal
    Sep 20, 2013 at 22:19
  • " column value should be a composite value of v1:v2:v3" --> In this case, create in Java a class that encapsulate v1,v2 & v3. Then serialize this class as JSON. You could store this JSON payload by defining column value type as UTF8Type
    – doanduyhai
    Sep 21, 2013 at 9:02
  • Thanks doanduyhai for the suggestion. That was my last solution to serialize everything as JSON. But is there any way to use any feature of Cassandra itself before going this route?
    – arsenal
    Sep 21, 2013 at 17:49
1

I would suggest you use CQL3. If you use Cassandra 1.2+ and CQL3, the following table will result in the partition (row) layout you describe.

CREATE TABLE user_data (
    userid text,
    data bytes,
    schema_name string,
    lmd timestamp,
    PRIMARY KEY (userid, data, schema_name, lmd)
)

You can see the following article for more information on how CQL3 ends up as composite columns under the hood, and makes them much easier to use:
http://www.datastax.com/dev/blog/cql3-for-cassandra-experts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.