Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using CQLSH

Introduction to CQLSH

CQLSH (Cassandra Query Language Shell) is an interactive command-line tool that is used to interact with Cassandra databases. It allows users to execute CQL (Cassandra Query Language) commands, manage keyspaces, tables, and data, and perform administrative tasks.

Installing CQLSH

CQLSH is included with the installation of Cassandra. To use CQLSH, ensure that you have installed Cassandra on your machine. You can download it from the official Apache Cassandra website.

Once installed, you can run CQLSH by navigating to the Cassandra installation directory and executing the following command:

cqlsh

This will launch the CQLSH prompt, indicating that you can start entering CQL commands.

Basic Commands in CQLSH

Once you are in CQLSH, you can execute various commands. Here are some fundamental commands to get started:

1. Show Keyspaces

To view all keyspaces in your Cassandra database, use the following command:

DESCRIBE KEYSPACES;

The output will list all keyspaces available in the database.

2. Create a Keyspace

To create a new keyspace, you can 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.

3. Use a Keyspace

Before creating tables, you need to specify which keyspace to use:

USE my_keyspace;

4. Create a Table

To create a table within the keyspace, use the following command:

CREATE TABLE users (id UUID PRIMARY KEY, name text, email text);

5. Insert Data

To insert data into the table, you can use the following command:

INSERT INTO users (id, name, email) VALUES (uuid(), 'John Doe', 'john@example.com');

6. Query Data

To retrieve data from the table, use a SELECT statement:

SELECT * FROM users;

This will return all records from the users table.

Advanced Features of CQLSH

CQLSH also supports advanced features such as user-defined functions, procedures, and more. Below are examples of some advanced commands.

1. User-Defined Functions

You can create user-defined functions to extend the capabilities of CQL. Here’s an example:

CREATE FUNCTION my_keyspace.add_numbers(a int, b int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return a + b;';

2. Batch Operations

CQL supports batch operations for executing multiple statements at once. Here’s an example:

BEGIN BATCH INSERT INTO users (id, name, email) VALUES (uuid(), 'Jane Doe', 'jane@example.com') APPLY BATCH;

3. Dropping Keyspaces and Tables

To remove a keyspace or table, use the DROP command:

DROP TABLE users;
DROP KEYSPACE my_keyspace;

Exiting CQLSH

To exit CQLSH, simply type:

EXIT;

Conclusion

CQLSH is an essential tool for interacting with Cassandra databases. With the commands and examples provided in this tutorial, you should be able to perform basic and advanced operations in CQLSH. Experiment with different commands to gain a deeper understanding of how to manage your data effectively.