Cassandra: How to create column in a super column family? - cassandra

I am defining my database model in a schema file in order to easily create the keyspace and column families from scratch later on. I looked at the schema-sample.txt that comes with the Cassandra distribution and it shows how to create standard column families using the column_metadata such as this:
create column family User
with comparator = UTF8Type
and default_validation_class = UTF8Type
and column_metadata = [
{column_name : name, validation_class : UTF8Type}
{column_name : username, validation_class : UTF8Type}
{column_name : City, validation_class : UTF8Type}
];
But how can I use a similar syntax to create a super column family? I would like to define the column names of the super columns I will put in the super column family.

To create a Super Column Family you only need to add the column_type property:
create column family User
with column_type = 'Super'
and comparator = UTF8Type
and default_validation_class = UTF8Type;
You can't define the names of the super columns in the metadata. The metadata is used for either validating column values or creating indexes. Since you can't validate the value of a super column (the value is more columns) and you can't create indexes on super columns, there is no reason to set metadata for them.
You can create metadata about the sub columns with super columns but it will apply to all super columns in the row. For example:
create column family User
with column_type = 'Super'
and comparator = UTF8Type
and default_validation_class = UTF8Type
and column_metadata = [
{column_name : name, validation_class : UTF8Type}
{column_name : age, validation_class : LongType}
];
In this super column family any sub column called 'name' will have to be UTF8 and any sub column called age will have to be a long.

Related

Update single column in cassandra

I have following cassandra column family:
create column family cfn
with comparator = UTF8Type
and key_validation_class = UUIDType
and column_metadata =[
{column_name:email, validation_class: UTF8Type,index_type: KEYS}
{column_name:full_name, validation_class: UTF8Type}
];
I want to update "full_name" of given "email" but i don't know the row key i only have "email". How can i do that using hector thrift api?
I know that i will have to insert a new column as there is no update kind of thing in cassandra. Will it be necessary to get row key before inserting new column for the same row?
Inserting using cassandra-cli or hector is the most basic thing. May be you need to brush your cassandra repo Read This.
Try this using cli
SET cfn[RowKey]['full_name']='XYZ';
/*
*Remember you have mentioned your key as UUID Type. So while providing a Rowkey it should
*be in UUID type only. e.g 8aff3820-1e55-11b2-a248-41825ac3edd8
*/
SET cfn[RowKey]['email']='XYZ#GMAIL.COM';
Retrieve Data,
list cfn;

Possible to specify individual Comparators on Column Name?

So far, I have been defining the Comparator (which specifies the data type of a Column Name) at the same time when creating Column Family and all the Columns in the family use that same Comparator Data Type.
[default#Voting] create column family User with comparator = UTF8Type;
I want to have Columns with different Comparator Data types in a column family.
My Question is this: Is it possible to specify Comparator when defining the meta data for individual Columns in the same way the validation_class for that Column in defined?
E.g.
update column family User with column_metadata = [{column_name: firstName, validation_class: UTF8Type}];
The comparator tells Cassandra how to sort the column names, not the values. So it would be impossible to have multiple comparators in a column family. You may be confusing them with validators, which enforce data typing. You can specify your validator in the column metadata.
...
with column_metadata = [
{column_name: firstName, validation_class: UTF8Type}
...
];

Cassandra: create CompositeType column

Im new to cassandra and Im trying to create a schema that will allow me to have a row as
users: {
name: <value>,
...
phone_numbers:1: <value>,
phone_numbers:2: <value>,
...
phone_numbers: <value>
}
I've been reading up about Composite Columns, and have only been able to find examples where the primary key is a composite. So is it possible to have the above in one CF?
My attempt was:-
create column family users with comparator=AsciiType
and key_validation_class=AsciiType
and column_metadata=[
{column_name: name, validation_class: AsciiType, index_type: KEYS},
{column_name: phone_numbers, validation_class: CompositeType(AsciiType, IntegerType), index_type: KEYS}];
the above fails.
any help is appreciated
cheers
CompositeType in cassandra can be used either as comparator [Type of column name] or key_validation_class [Type of row key] but not as default_validation_class [Type of the column value]
create column family users with
comparator='CompositeType(UTF8Type, IntegerType)'
and key_validation_class='UTF8Type'
Should work for you. I have shared a detailed post on Composite Types in cassandra
In cql 3 you could declare it as: (using type ascii for sake of simplicity)
create table users (user ascii,phoneid ascii,phonenumber ascii,
primary key(user,phonenumber)) with compact storage;
fill it as
insert into users (user,phoneid,phonenumber values ('john smith','home','555-121345')
insert into users (user,phoneid,phonenumber values ('john smith','office','555-121346')
and retrieve it as
select * from users where user = 'john smith'
user,phoneid,phonenumber
'john smith','home','555-121345'
'john smith','office','555-121346'
Hope it helps

create super column and key

I not too familiar with creating a super column. Is column_type=Super the right way to assign Super1 as super column?
I could not get the id as index_type: KEYS to:
create column family Super1 with comparator=UTF8Type and
column_type=Super and key_validation_class=UTF8Type and
column_metadata = [
{column_name: id, validation_class:UTF8Type, index_type: KEYS},
{column_name: username, validation_class:UTF8Type},
{column_name: email, validation_class:UTF8Type}];
Please advice.
As found in \apache-cassandra-0.8.0\conf\schema-sample.txt:
create column family Super1
with column_type = Super
and comparator = BytesType
and subcomparator = BytesType;
create column family Super2
with column_type = Super
and subcomparator = UTF8Type
and rows_cached = 10000
and keys_cached = 50
and comment = 'A column family with supercolumns, whose column and subcolumn names are UTF8 strings';
create column family Super3
with column_type = Super
and comparator = LongType
and comment = 'A column family with supercolumns, whose column names are Longs (8 bytes)';
Three different examples to suit your needs...
You can't create column indexes on supercolumns.

Set Super Column Family in Cassandra Cli

I'm using Cassandra 0.8.0.
I cannot figure out how to use the Cassandra-Cli to add values to a currently existing SuperColumn. For example: I have added the following to my keyspace
create column family authors
with comparator = UTF8Type and subcomparator = UTF8Type
and default_validation_class = UTF8Type
and column_metadata = [{
column_name: tags, validation_class: UTF8Type},
{column_name: url, validation_class:UTF8Type},
{column_name: title, validation_class: UTF8Type},
{column_name: publisher, validation_class: UTF8Type},
{column_name: email, validation_class: UTF8Type}];
I tried adding a row to this super column doing:
(input)[default#testspace] set authors[1]['1']['url'] = 'www.henry.com';
(output) [default#testspace] null
which is an error^
I try this too:
[default#testspace] set authors['henry']['url']['1'] = 'www.henry.com';
and get:
org.apache.cassandra.db.marshal.MarshalException: cannot parse 'henry' as hex bytes
what is the proper syntax for manipulating SuperColumns in the Cassandra-Cli? Can you provide an example to set/get values using a supercolumn in cassandra-cli?
Thank you
You need to add
column_type = 'Super' and key_validation_class = UTF8Type
to your column family definition.
You cannot create secondary indexes on the sub-columns of a super column. Therefore, the use of super columns is best suited for use cases where the number of sub-columns is a relatively small number.

Resources