Cassandra: create CompositeType column - cassandra

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

Related

need to search data using part of composite row key in cassandra

I am new to Cassandra and just playing around with it. I have created a Column family having composite key and composite column. Following is the script for same:
create column family TestCompositeKey with key_validation_class='CompositeType(UTF8Type, TimeUUIDType)' and comparator='CompositeType(UTF8Type, UTF8Type, UTF8Type, UTF8Type)' and default_validation_class='UTF8Type';
After inserting data in the column family using Hector following is the view I am getting on CLI:
RowKey: AB:e9a87550-c84b-11e2-8236-180373b60c1a
=> (column=0007:TAR:PUB:BD_2013_01_11_0125094813, value=TESTSEARCH, timestamp=1369823914277000)
Now I want to search for data just by 'AB' given in row key as second part of key will be dynamic. It works fine when I give complete row key. Please tell me how can this be done. I am supplying search criteria on column too along with specifying key.
Thanks
Harish Kumar
You can't do this (efficiently, at least): to lookup by row key you need the whole key. In general, using TimeUUIDs as row keys should be avoided, unless you have some other table acting as an index to retrieve TimeUUIDs for a query.
If you want to lookup just by the first component of the key you should move the second component to the column composite and just have a single component as the row key. The definition would be
create column family TestCompositeKey with key_validation_class='UTF8Type' and comparator='CompositeType(TimeUUIDType, UTF8Type, UTF8Type, UTF8Type, UTF8Type)' and default_validation_class='UTF8Type';
If you used the CQL3 definition:
CREATE TABLE TestCompositeKey (
a varchar,
b timeuuid varchar,
c varchar,
d varchar,
e varchar,
f varchar,
PRIMARY KEY (a, b, c, d, e, f)
);
you would get essentially the same schema as I described. The row key (partition key in CQL language) is a, and the column names are a composite of b:c:d:e:f.

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}
...
];

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