Possible to specify individual Comparators on Column Name? - cassandra

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

Related

programmatically determining Cassandra columns at runtime

I'm accessing a Cassandra database and I only know the table names.
I want to discover the names & types of the columns.
This will give me the column names:
select column_name
from system.schema_columns
where columnfamily_name = 'customer'
allow filtering;
Is this reasonable?
Does anyone have suggestions about determining column types?
Depending on what driver you're using, you should be able to use the metadata API.
A couple examples:
http://datastax.github.io/python-driver/api/cassandra/metadata.html#schemas
https://datastax.github.io/java-driver/features/metadata/#schema-metadata
The drivers query the system schema metadata to create these models.
You can infer the column types by looking at the classes used for the validator. The validator column is just a string.
The string has one of 3 formats:
org.apache.cassandra.db.marshal.XXXType for simple column types, where XXX is the Java type for the column (e.g. for bigint columns, XXX is "Long", for varchar/text, XXX is "UTF8", etc.)
org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.XXXType) for set columns, where the type in parenthesis is the type of each set element
org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.XXXType,org.apache.cassandra.db.marshal.XXXType) for maps
Quite old but still valid question. There is a class variable of your model that describe columns (field name and column class):
class Tweet(cqldb.Model):
"""
Object representing the tweet column family in Cassandra
"""
__keyspace__ = 'my_ks'
# follows model definition
...
...
print(Tweet._defined_columns)
# output
OrderedDict([('tweetid',
<cassandra.cqlengine.columns.Text at 0x7f4a4c9b66a0>),
('tweet_id',
<cassandra.cqlengine.columns.BigInt at 0x7f4a4c9b6828>),
('created_at',
<cassandra.cqlengine.columns.DateTime at 0x7f4a4c9b6748>),
('ttype',
<cassandra.cqlengine.columns.Text at 0x7f4a4c9b6198>),
('tweet',
<cassandra.cqlengine.columns.Text at 0x7f4a4c9b6390>),
('lang',
<cassandra.cqlengine.columns.Text at 0x7f4a4c9b3d68>)])

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;

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

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.

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