Cassandra CLI client

Using Cassandra CLI client

Start Cassandra CLI

bin/cassandra-cli -h

Change to your Cassandra server IP address

Create, show and delete Cassandra keyspace

Create a new Cassandra keyspace

create keyspace store with replication_factor=1 and placement_strategy='org.apache.cassandra.locator.SimpleStrategy';

";" is required for specifying the end of a CLI command

Show user and system keyspaces

show keyspaces;

Delete a Cassandra keyspace

drop keyspace store;

Create, Show and Delete Cassandra Column Family

Switch to a keyspace

use store;

Create a Cassandra column family

create column family users with comparator = UTF8Type and
   column_metadata = [{column_name: userName, validation_class:UTF8Type},
                      {column_name: email, validation_class:UTF8Type}];

create column family products with comparator = UTF8Type and
   column_metadata = [{column_name: skid, validation_class:UTF8Type},
                      {column_name: inventory, validation_class:LongType}];

Out of the box Cassandra column metadata type


Insert Cassandra Column

set users['Dave Jones']['userName']='dave';
set users['Dave Jones']['email']='';

Set time-to-live for a Cassandra column. (The unit for ttl is seconds)

  • After ttl seconds, the column will be expired and ready to be deleted automatically
    set users['Dave Jones']['email']='' with ttl=60;

Retrieve Cassandra Columns

get users['Dave Jones']['email'];
=> (column=email,, timestamp=1304488164426000)
get users['Dave Jones'];

Define the column display type if it display the information as binary data

get users['Dave Jones']['email'] as ascii;

List All Cassandra Columns

list users;

List columns limited to a max of 2

list users limit 2;

Drop a Cassandra column family

drop column family users;

Delete Cassandra row or columns

Delete a row

del users['Dave Jones'];

Delete a column

del users['Dave Jones']['email'];

Running a script for Cassandra CLI

Send a script to Cassandra CLI

bin/cassandra-cli -host -f conf/schema-sample.txt
create keyspace Keyspace1
    with replication_factor = 1
    and placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy';

use Keyspace1;

create column family Standard1
    with comparator = BytesType
    and keys_cached = 10000
    and rows_cached = 1000
    and row_cache_save_period = 0
    and key_cache_save_period = 3600
    and memtable_flush_after = 59
    and memtable_throughput = 255
    and memtable_operations = 0.29;

create column family Standard2
    with comparator = UTF8Type
    and read_repair_chance = 0.1
    and keys_cached = 100
    and gc_grace = 0
    and min_compaction_threshold = 5
    and max_compaction_threshold = 31;

create column family StandardByUUID1
    with comparator = TimeUUIDType;

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)'

create column family Indexed1
    with comparator = UTF8Type
    and default_validation_class = LongType
    and column_metadata = [{
        column_name : birthdate,
        validation_class : LongType,
        index_name : birthdate_idx,
        index_type : 0}