Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cassandra Shell command


May 17, 2021 Cassandra


Table of contents


In addition to the CQL command, Cassandra provides the recorded shell command. The shell command for Cassandra's record is given below.

Help

The HELP command displays a summary and a brief description of all cqlsh commands. Here's how the help command is used.

cqlsh> help

Documented shell commands:
===========================
CAPTURE COPY DESCRIBE EXPAND PAGING SOURCE
CONSISTENCY DESC EXIT HELP SHOW TRACING.

CQL help topics:
================
ALTER           CREATE_TABLE_OPTIONS       SELECT
ALTER_ADD       CREATE_TABLE_TYPES         SELECT_COLUMNFAMILY
ALTER_ALTER     CREATE_USER                SELECT_EXPR
ALTER_DROP      DELETE                     SELECT_LIMIT
ALTER_RENAME    DELETE_COLUMNS             SELECT_TABLE 

Capture

This command captures the output of the command and adds it to the file. For example, look at the code below, which captures the output to a file called Outputfile.

cqlsh> CAPTURE '/home/hadoop/CassandraProgs/Outputfile'

When we type any command in the terminal, the output is captured by the given file. The following is a snapshot of the commands and output files used.

cqlsh:tutorialspoint> select * from emp;
Cassandra Shell command

You can use the following commands to turn off capture.

cqlsh:tutorialspoint> capture off;

Consistency

This command shows the current consistency level, or sets a new consistency level.

cqlsh:tutorialspoint> CONSISTENCY
Current consistency level is 1.

Copy

This command copies the data from Cassandra to the file and from it. Here's an example of copying a table named emp to the file myfile.

cqlsh:tutorialspoint> COPY emp (emp_id, emp_city, emp_name, emp_phone,emp_sal) TO ‘myfile’;
4 rows exported in 0.034 seconds.

If you open and validate a given file, you can find the copied data, as shown below.

Cassandra Shell command

Describe

This command describes the current cluster of Cassandra and its objects. The variants of this command are described below.

Describe cluster - This command provides information about the cluster.

cqlsh:tutorialspoint> describe cluster;

Cluster: Test Cluster
Partitioner: Murmur3Partitioner

Range ownership:
                  -658380912249644557 [127.0.0.1]
                  -2833890865268921414 [127.0.0.1]
                  -6792159006375935836 [127.0.0.1] 

Describe Keyspaces - This command lists all key spaces in the cluster. H ere's how to use this command.

cqlsh:tutorialspoint> describe keyspaces;

system_traces system tp tutorialspoint

Describe tables - This command lists all tables in the key space. H ere's how to use this command.

cqlsh:tutorialspoint> describe tables;
emp

Describe tables - This command provides a description of the table. H ere's how to use this command.

cqlsh:tutorialspoint> describe table emp;

CREATE TABLE tutorialspoint.emp (
   emp_id int PRIMARY KEY,
   emp_city text,
   emp_name text,
   emp_phone varint,
   emp_sal varint
) WITH bloom_filter_fp_chance = 0.01
   AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
   AND comment = ''
   AND compaction = {'min_threshold': '4', 'class':
   'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy',
   'max_threshold': '32'}
	
   AND compression = {'sstable_compression':
   'org.apache.cassandra.io.compress.LZ4Compressor'}
	
   AND dclocal_read_repair_chance = 0.1
   AND default_time_to_live = 0
   AND gc_grace_seconds = 864000
   AND max_index_interval = 2048
   AND memtable_flush_period_in_ms = 0
   AND min_index_interval = 128
   AND read_repair_chance = 0.0
   AND speculative_retry = '99.0PERCENTILE';
CREATE INDEX emp_emp_sal_idx ON tutorialspoint.emp (emp_sal);

Describe tables

This command is used to describe the user-defined data type. Here's how to use this command.

cqlsh:tutorialspoint> describe type card_details;

CREATE TYPE tutorialspoint.card_details (
   num int,
   pin int,
   name text,
   cvv int,
   phone set<int>,
   mail text
);

Describe Types

This command lists all user-defined data types. H ere's how to use this command. Suppose you have two user-defined data types: card and card_details.

cqlsh:tutorialspoint> DESCRIBE TYPES;

card_details card

Expand

This command is used to extend the output. B efore you can use this command, you must open the spand command. Here's how to use this command.

cqlsh:tutorialspoint> expand on;
cqlsh:tutorialspoint> select * from emp;

@ Row 1
-----------+------------
    emp_id | 1
  emp_city | Hyderabad
  emp_name | ram
 emp_phone | 9848022338
   emp_sal | 50000
  
@ Row 2
-----------+------------
    emp_id | 2
  emp_city | Delhi
  emp_name | robin
 emp_phone | 9848022339
   emp_sal | 50000
  
@ Row 3
-----------+------------
    emp_id | 4
  emp_city | Pune
  emp_name | rajeev
 emp_phone | 9848022331
   emp_sal | 30000
  
@ Row 4
-----------+------------
    emp_id | 3
  emp_city | Chennai
  emp_name | rahman
 emp_phone | 9848022330
   emp_sal | 50000
(4 rows)

Note: You can turn off the expand option using the following command.

cqlsh:tutorialspoint> expand off;
Disabled Expanded output.

Exit

This command is used to terminate the cql shell.

Show

This command displays details of the current cqlsh session, such as the Cassandra version, host, or data type assumptions. Here's how to use this command.

cqlsh:tutorialspoint> show host;
Connected to Test Cluster at 127.0.0.1:9042.

cqlsh:tutorialspoint> show version;
[cqlsh 5.0.1 | Cassandra 2.1.2 | CQL spec 3.2.0 | Native protocol v3]

Source

With this command, you can execute the command in a file. Let's say our input file is as follows:

Cassandra Shell command

You can then execute the file that contains the command, as shown below.

cqlsh:tutorialspoint> source '/home/hadoop/CassandraProgs/inputfile';

 emp_id |  emp_city | emp_name |  emp_phone | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |   ram    | 9848022338 | 50000
      2 | Delhi     |   robin  | 9848022339 | 50000
      3 | Pune      |   rajeev | 9848022331 | 30000
      4 | Chennai   |   rahman | 9848022330 | 50000
(4 rows)