When running Cassandra in a Docker container, cqlsh provides a seamless way to access and manage your database directly from the container. In this guide, we’ll walk through the steps to access a Cassandra container and use cqlsh for database operations.
Press enter or click to view image in full size
CassandraCQLClient
Access Container
docker exec -it cassandra bash # cassandra is the container name here, or you can use the container id in place of the container name.
## OUTPUT: root@cassandra-db:/# cqlsh Connected to SolarSystem at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.11.10 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh>
cqlsh> use ramayana ; cqlsh:ramayana> CREATE TABLE ramayana.characters ( id UUID PRIMARY KEY, name TEXT, description TEXT );
CRUD Operations
Create
cqlsh> use ramayana ; cqlsh:ramayana> INSERT INTO characters (id, name, description) VALUES (uuid(), 'Rama', 'Hero of the Ramayana, seventh avatar of Vishnu.'); cqlsh:ramayana> INSERT INTO characters (id, name, description) VALUES (uuid(), 'Sita', 'Wife of Rama and central character of the Ramayana.'); cqlsh:ramayana> INSERT INTO characters (id, name, description) VALUES (uuid(), 'Lakshmana', 'Brother of Rama and loyal companion.');
Read or Select
All Data
cqlsh:ramayana> SELECT * FROM characters;
Select data by id
cqlsh:ramayana> SELECT * FROM characters WHERE id = <ID>;
Update data
cqlsh:ramayana> UPDATE characters SET name = 'Raama' WHERE id = <ID>;
Delete data
cqlsh:ramayana> DELETE FROM characters WHERE id = <ID>;
Ref: Check the below for all the usecases of cassandra