Cassandra query language (CQL) and Cassandra Java Client Example

Cassandra Table structure/Terminology

  • Before going to learn CQL commands, we just need to know terminology in cassandra.
RDBMS Cassandra Terminology
Database Keyspace
Table Column Family
Primary key Row Key
Column name Column name
Column value column value

CQL Commands

  • Creating a key-space

  • Use the keyspace (will use that key space)

Note: key spaces are equivalent to database/schema in RDBMS

  • Get list of key spaces

  • Create table

  • Get list of tables in a key-space

  • Insert data into table

  • Describe table

  • Create index

  • Update data in table

  • Delete data in table

  • Limitations in CQL

    • Delete query without where condition will not work.
    • Delete/Update query with Non-primary key in where clause will not works.
    • To use Select query with Non-primary key in where clause, we should index that column.
    • Instead of Update we can use insert with same primary key value, will replace the existing record with new values. (Upsert)
    • we cannot use ‘or’ clause in cql instead we have to use ‘in’ clause.

      Java client example

  • Take a Java maven project and Add below dependency

  • Below shows the Sample Java client example

Note: Cluster and Session object should be created only once per the application.

Miscellaneous commands in CQL

  • Get size of table:

  • Flush data into SStable/disk:

  • Copy table content from table to csv file:

Note: if you are below exception while copying 

Cassandra CQLSH TEXT field limit on COPY FROM CSV (field larger than field limit (131072))

then Modify cqlsh file in bin folder as below

Cassandra Pooling options

Below Java code shows the

  • These pooling options we have taken for my project requirement, these may vary as per your requirement.

Leave a comment

Your email address will not be published. Required fields are marked *