Cassandra basic commands:

Create keyspace:

create keyspace Keyspace1;

Create column family:

// This command is used to create column family and mention comparator, default_validation_class and key_validation_class to insert rows for undefined column.

create column family column_family1 with comparator=UTF8Type and default_validation_class=UTF8Type and key_validation_class=UTF8Type;

Update column family:

update column family column_family1 with
column_metadata =
[
{column_name: first, validation_class: UTF8Type},
{column_name: last, validation_class: UTF8Type},
{column_name: age, validation_class: UTF8Type, index_type: KEYS}
];

Insert/Update data:

set User[‘jsmith’][‘first’] = ‘John’;
set User[‘jsmith’][‘last’] = ‘Smith’;
set User[‘jsmith’][‘age’] = ’38’;

Retrieve Data:

get User[‘jsmith’][‘first’];
get User[‘jsmith’];
get User where age = 38;

list User;

Create indexing column:

UPDATE COLUMN_FAMILY users WITH comparator = UTF8Type AND column_metadata = [{column_name: birth_year, validation_class: LongType, index_type: KEYS}];

Set data expiring time:

set User[‘jsmith’] [utf8(‘coupon_code’)] = utf8(‘SAVE20’) WITH ttl=864000;

Delete Row and Column:

del User[‘jsmith’][‘first’];
del User[‘jsmith’];

Drop column family and keyspace:

drop column family column_family1;
drop keyspace Keyspace1;

Super column family:

create column_family FbUsers with comparator=UTF8Type and default_validation_class=UTF8Type and key_validation_class=UTF8Type and subcomparator=UTF8Type and column_type=’Super’;

Encode column value of specific column family:

ASSUME COLUMN_FAMILY KEYS AS ascii;
ASSUME COLUMN_FAMILY COMPARATOR AS ascii;
ASSUME COLUMN_FAMILY VALIDATOR AS ascii;
ASSUME COLUMN_FAMILY SUBCOMPARATOR AS ascii;

Permanent link to this article: https://blog.openshell.in/2013/01/cassandra-basic-commands/

Leave a Reply

Your email address will not be published.