Cassandra Cqlsh stands for the Cassandra Query Language Shell is a utility provided by Cassandra to run the Cassandra Query Language on the Local system or the remote system. It comes with the Cassandra package by default and is present in the /bin directory. The implementation of Cqlsh is done with the Python native protocol driver.
We can use Cassandra Cqlsh to perform the CQL DDL(Data Definition Language operations such as Create, Alter, Drop, Truncate as well as we can perform the CQL DML(Data Manipulation Language such as Insert, Update, Delete, Batch, and CQL Clauses which are Select, Where, Order by.
Start Cassandra Cqlsh
To Start Cassandra Cqlsh we can use the cqlsh command. But before starting the Cqlsh the Cassandra Datacenter must be running.
So let's start the Cassandra Datacenter first using the below steps command and then we will start the Cassandra Cqlsh.
Step 1. Go to the Cassandra Bin directory using the below command and run the ./cassandra command to start the Cassandra.cloudduggu@ubuntu:~$ cd apache-cassandra-4.0.1
cloudduggu@ubuntu:~/apache-cassandra-4.0.1$ cd bin
Once the Cassandra Datacenter is started we will see the following output.
We can also use the ./nodetool status command to verify the status of the Cassandra Datacenter as mentioned below.cloudduggu@ubuntu:~/apache-cassandra-4.0.1/bin$ ./nodetool status
Step 2. Now the Cassandra Datacenter has been started, we can start the cqlsh from the bin directory, using the ./cqlsh command as mentioned below.
Cassandra Cqlsh Options
The following are the option available with Cassandra Cqlsh which can be used to perform different types of operations such as checking the Version, Running the Cqlsh script file, debugging, and so on.
|Cassandra Cqlsh Options||Cqlsh Options Usage|
|cqlsh --help||This option is used to show the help of Cassandra Cqlsh commands.|
|cqlsh --version||This option will show the version of Cassandra.|
|cqlsh --color||This option will help in coloring the Shell output.|
|cqlsh --debug||This option will help to show the additional information on Debugging.|
|cqlsh --execute cql_statement||This option will accept the Cassandra Cqlsh command and execute it.|
|cqlsh --file="file_name"||This option is used to execute the Cqlsh script file.|
|cqlsh --no-color||This option will force Cassandra to not use any colored output.|
|cqlsh -u "user_name"||This option will be used to authenticate the user. Cassandra is the default username.|
|cqlsh-p "password"||This option will be used to authenticate the user with the password and the Cassandra is the default password.|
|cqlsh --ssl||This option will SSL while connecting to Cassandra.|
|cqlsh --encoding||The default encoding in Cassandra is UTF-8, by using this option we can specify the encoding.|
Now let us see some of the examples in the following section. We will use the basic help, Version commands to see them working, and based on that other options can also be used.
Cassandra cqlsh --help:
The Cassandra cqlsh Help option shows the list of cqlsh options also shows the detailed usage of those options.cloudduggu@ubuntu:~/apache-cassandra-4.0.1/bin$ ./cqlsh --help
Cassandra cqlsh --version:
The Cassandra cqlsh Version option shows the Version of Cassandra that is being used. The Cassandra Version command is mentioned below.cloudduggu@ubuntu:~/apache-cassandra-4.0.1/bin$ ./cqlsh --version
Cassandra cqlsh --debug:
Using the Cassandra Debug option the shell is started in Debugging mode and provides the additional information as mentioned in the below output.cloudduggu@ubuntu:~/apache-cassandra-4.0.1/bin$ ./cqlsh --debug
Cassandra Cqlsh Commands
Cassandra Cqlsh supports the Data definition commands, Data Manipulation Commands, and the Clauses to perform different types of operations such as creating a table, dropping a table, creating a user, dropping a user, creating a function, dropping a function, using different clauses to perform sorting, selection and data restriction.
The following is the list of Cassandra DDL, DML, and clauses.
|Cassandra Data Definition Commands||Cqlsh Options Usage|
|CREATE KEYSPACE||This command is used to create the Keyspace in Cassandra.|
|USE||By using this command we can connect to the already created Cassandra Keyspace.|
|ALTER KEYSPACE||Use this command to alter the already created Cassandra Keyspace.|
|DROP KEYSPACE||This command will drop the already created Cassandra Keyspace.|
|CREATE TABLE||Using this command we can create the table in Cassandra.|
|ALTER TABLE||Alter command is used to perform the structural changes in the existing table such as changing the data type of the column and so on.|
|DROP TABLE||We can use this command to drop a Cassandra table.|
|TRUNCATE||This command will remove all data from a Cassandra table except the structure.|
|CREATE INDEX||Use this command to create a new index on the Cassandra table.|
|DROP INDEX||This command is used to drop an index from the Cassandra table.|
|CREATE_FUNCTION||This command is used to create a Function in Cassandra.|
|DROP_FUNCTION||This command is used to drop a Function in Cassandra.|
|CREATE_USER||Using this command we can create a new user in Cassandra.|
|DROP_USER||This command will drop an existing user from Cassandra.|
|CREATE_ROLE||We can create a Cassandra role using this command.|
|DROP_ROLE||To drop an existing Cassandra role we can use this command.|
|Cassandra Data Manipulation Commands||Cqlsh Options Usage|
|INSERT||This command is used to add rows to the table.|
|UPDATE||This command is used to update the rows in the table.|
|DELETE||Using the command the data will be deleted from the table.|
|BATCH||Using this command we can execute multiple DML statements.|
|Cassandra Clauses||Cqlsh Options Usage|
|SELECT||Using the SELECT command we can read the data from the table.|
|WHERE||The WHERE clause is used to restrict the rows based on the business requirement.|
|ORDERBY||The ORDER BY clause is used to perform the shorting of rows in ascending and descending order.|