Apache Cassandra Cheatsheet
#
Apache Cassandra is a distributed NoSQL database designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure.
General Commands
#
Command/Option | Example | Description |
---|
cqlsh | cqlsh | Start the Cassandra Query Language shell |
DESCRIBE KEYSPACES; | DESCRIBE KEYSPACES; | List all keyspaces |
DESCRIBE TABLE <table_name>; | DESCRIBE TABLE users; | Describe a specific table |
SHOW TABLES; | SHOW TABLES; | List all tables in the current keyspace |
Keyspace and Table Management
#
Command/Option | Example | Description |
---|
CREATE KEYSPACE | CREATE KEYSPACE my_keyspace WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 3 }; | Create a new keyspace |
USE <keyspace>; | USE my_keyspace; | Switch to a specific keyspace |
CREATE TABLE | CREATE TABLE users (id UUID PRIMARY KEY, name TEXT, age INT); | Create a new table |
DROP TABLE | DROP TABLE users; | Drop a table |
DROP KEYSPACE | DROP KEYSPACE my_keyspace; | Drop a keyspace |
CRUD Operations
#
Command/Option | Example | Description |
---|
INSERT INTO | INSERT INTO users (id, name, age) VALUES (uuid(), 'Alice', 30); | Insert a new record |
SELECT | SELECT * FROM users; | Query records from a table |
UPDATE | UPDATE users SET age = 31 WHERE name = 'Alice'; | Update a record |
DELETE | DELETE FROM users WHERE name = 'Alice'; | Delete a record |
Indexes and Query Optimization
#
Command/Option | Example | Description |
---|
CREATE INDEX | CREATE INDEX ON users (age); | Create an index on a column |
DROP INDEX | DROP INDEX users_age_idx; | Drop an index |
ANALYZE TABLE | ANALYZE TABLE users; | Analyze a table for query optimization |
Data Import and Export
#
Command/Option | Example | Description |
---|
COPY FROM | COPY users (id, name, age) FROM 'users.csv' WITH DELIMITER = ','; | Import data from a CSV file |
COPY TO | COPY users TO 'users.csv' WITH DELIMITER = ','; | Export data to a CSV file |
Cluster Management
#
Command/Option | Example | Description |
---|
nodetool status | nodetool status | Check the status of the cluster |
nodetool repair | nodetool repair | Repair inconsistencies in the cluster |
nodetool flush | nodetool flush | Flush memtables to SSTables |
nodetool compact | nodetool compact | Compact SSTables |
Backup and Restore
#
Command/Option | Example | Description |
---|
nodetool snapshot | nodetool snapshot -t snapshot_name my_keyspace | Create a snapshot backup |
sstableloader | sstableloader -d localhost:9042 /path/to/sstable | Load SSTables into Cassandra |
This cheatsheet provides essential Apache Cassandra commands and options for managing keyspaces, tables, and data, as well as performing backups and maintaining cluster health.