Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Cassandra CQLSH Tutorial

Introduction to CQLSH

CQLSH (Cassandra Query Language Shell) is an interactive shell for communicating with a Cassandra database using CQL (Cassandra Query Language). CQL is similar to SQL but designed specifically for Cassandra's architecture. Using CQLSH, you can execute CQL commands to create, modify, and query data in a Cassandra database.

Installing Cassandra

Before using CQLSH, you need to have Apache Cassandra installed on your system. You can download the latest version from the official Apache Cassandra website and follow the installation instructions provided for your operating system.

Starting CQLSH

Once Cassandra is installed and running, you can start CQLSH by opening a terminal and executing the following command:

cqlsh

This command opens the CQL shell, allowing you to enter CQL commands.

CQLSH Basics

In CQLSH, commands are executed by typing them in and pressing Enter. If you enter a valid CQL statement, CQLSH will execute it and return the results. If the command is invalid, it will display an error message. Here are a few basic commands:

SELECT now() FROM system.local;

Output:
now()
2023-10-01 12:00:00.000000+0000

Creating a Keyspace

A keyspace in Cassandra is similar to a database in relational databases. It defines how data is replicated on nodes. To create a keyspace, use the following command:

CREATE KEYSPACE my_keyspace WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};

This command creates a keyspace named my_keyspace with a simple replication strategy.

Creating a Table

Once the keyspace is created, you can create tables to store your data. Use the following command to create a table:

CREATE TABLE my_keyspace.users (id UUID PRIMARY KEY, name text, age int);

This command creates a table named users with three columns: id, name, and age.

Inserting Data

You can insert data into your table using the INSERT command. Here’s how to insert a record into the users table:

INSERT INTO my_keyspace.users (id, name, age) VALUES (uuid(), 'Alice', 30);

This command inserts a new user named Alice with an age of 30 into the users table.

Querying Data

To retrieve data from your table, use the SELECT statement. Here’s how to query all users:

SELECT * FROM my_keyspace.users;

Output:
id | name | age
----+-------+-----
... | Alice | 30

Updating Data

To update existing records, use the UPDATE statement. For example, to change Alice's age:

UPDATE my_keyspace.users SET age = 31 WHERE name = 'Alice';

This command updates Alice's age to 31.

Deleting Data

To delete data, use the DELETE statement. For instance, to remove Alice from the table:

DELETE FROM my_keyspace.users WHERE name = 'Alice';

This command deletes the record of the user named Alice.

Exiting CQLSH

To exit CQLSH, simply type the following command:

exit;

This command will close the CQL shell and return you to your terminal.

Conclusion

CQLSH is a powerful tool for interacting with Cassandra databases. By mastering the commands covered in this tutorial, you can effectively manage your data using CQL. Practice using CQLSH with different commands to enhance your understanding and proficiency.