Cassandra – Cqlsh

This chapter introduces the Cassandra query language shell and explains how to use its commands.

By default, Cassandra provides a prompt Cassandra query language shell (cqlsh) that allows users to communicate with it. Using this shell, you can execute Cassandra Query Language (CQL).

Using cqlsh, you can

  • define a schema,
  • insert data, and
  • execute a query.

Starting cqlsh

Start cqlsh using the command cqlsh as shown below. It gives the Cassandra cqlsh prompt as output.

[hadoop@linux bin]$ cqlsh
Connected to Test Cluster at 127.0.0.1:9042.

[cqlsh 5.0.1 | Cassandra 2.1.2 | CQL spec 3.2.0 | Native protocol v3]

Use HELP for help. cqlsh>

Cqlsh βˆ’ As discussed above, this command is used to start the cqlsh prompt. In addition, it supports a few more options as well. The following table explains all the options of cqlsh and their usage.

OptionsUsage
cqlsh –helpShows help topics about the options of cqlsh commands.
cqlsh –versionProvides the version of the cqlsh you are using.
cqlsh –colorDirects the shell to use colored output.
cqlsh –debugShows additional debugging information.
cqlsh –executecql_statementDirects the shell to accept and execute a CQL command.
cqlsh –file= β€œfile name”If you use this option, Cassandra executes the command in the given file and exits.
cqlsh –no-colorDirects Cassandra not to use colored output.
cqlsh -u β€œuser name”Using this option, you can authenticate a user. The default user name is: cassandra.
cqlsh-p β€œpass word”Using this option, you can authenticate a user with a password. The default password is: cassandra.

Cqlsh Commands

Cqlsh has a few commands that allow users to interact with it. The commands are listed below.

Documented Shell Commands

Given below are the Cqlsh documented shell commands. These are the commands used to perform tasks such as displaying help topics, exit from cqlsh, describe,etc.

  • HELP βˆ’ Displays help topics for all cqlsh commands.
  • CAPTURE βˆ’ Captures the output of a command and adds it to a file.
  • CONSISTENCY βˆ’ Shows the current consistency level, or sets a new consistency level.
  • COPY βˆ’ Copies data to and from Cassandra.
  • DESCRIBE βˆ’ Describes the current cluster of Cassandra and its objects.
  • EXPAND βˆ’ Expands the output of a query vertically.
  • EXIT βˆ’ Using this command, you can terminate cqlsh.
  • PAGING βˆ’ Enables or disables query paging.
  • SHOW βˆ’ Displays the details of current cqlsh session such as Cassandra version, host, or data type assumptions.
  • SOURCE βˆ’ Executes a file that contains CQL statements.
  • TRACING βˆ’ Enables or disables request tracing.

CQL Data Definition Commands

  • CREATE KEYSPACE βˆ’ Creates a KeySpace in Cassandra.
  • USE βˆ’ Connects to a created KeySpace.
  • ALTER KEYSPACE βˆ’ Changes the properties of a KeySpace.
  • DROP KEYSPACE βˆ’ Removes a KeySpace
  • CREATE TABLE βˆ’ Creates a table in a KeySpace.
  • ALTER TABLE βˆ’ Modifies the column properties of a table.
  • DROP TABLE βˆ’ Removes a table.
  • TRUNCATE βˆ’ Removes all the data from a table.
  • CREATE INDEX βˆ’ Defines a new index on a single column of a table.
  • DROP INDEX βˆ’ Deletes a named index.

CQL Data Manipulation Commands

  • INSERT βˆ’ Adds columns for a row in a table.
  • UPDATE βˆ’ Updates a column of a row.
  • DELETE βˆ’ Deletes data from a table.
  • BATCH βˆ’ Executes multiple DML statements at once.

CQL Clauses

  • SELECT βˆ’ This clause reads data from a table
  • WHERE βˆ’ The where clause is used along with select to read a specific data.
  • ORDERBY βˆ’ The orderby clause is used along with select to read a specific data in a specific order.

Leave a Reply