Apache Cassandra

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/OptionExampleDescription
cqlshcqlshStart 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/OptionExampleDescription
CREATE KEYSPACECREATE 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 TABLECREATE TABLE users (id UUID PRIMARY KEY, name TEXT, age INT);Create a new table
DROP TABLEDROP TABLE users;Drop a table
DROP KEYSPACEDROP KEYSPACE my_keyspace;Drop a keyspace

CRUD Operations #

Command/OptionExampleDescription
INSERT INTOINSERT INTO users (id, name, age) VALUES (uuid(), 'Alice', 30);Insert a new record
SELECTSELECT * FROM users;Query records from a table
UPDATEUPDATE users SET age = 31 WHERE name = 'Alice';Update a record
DELETEDELETE FROM users WHERE name = 'Alice';Delete a record

Indexes and Query Optimization #

Command/OptionExampleDescription
CREATE INDEXCREATE INDEX ON users (age);Create an index on a column
DROP INDEXDROP INDEX users_age_idx;Drop an index
ANALYZE TABLEANALYZE TABLE users;Analyze a table for query optimization

Data Import and Export #

Command/OptionExampleDescription
COPY FROMCOPY users (id, name, age) FROM 'users.csv' WITH DELIMITER = ',';Import data from a CSV file
COPY TOCOPY users TO 'users.csv' WITH DELIMITER = ',';Export data to a CSV file

Cluster Management #

Command/OptionExampleDescription
nodetool statusnodetool statusCheck the status of the cluster
nodetool repairnodetool repairRepair inconsistencies in the cluster
nodetool flushnodetool flushFlush memtables to SSTables
nodetool compactnodetool compactCompact SSTables

Backup and Restore #

Command/OptionExampleDescription
nodetool snapshotnodetool snapshot -t snapshot_name my_keyspaceCreate a snapshot backup
sstableloadersstableloader -d localhost:9042 /path/to/sstableLoad 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.

Apache Cassandra

Explore our comprehensive cheatsheets to enhance your knowledge and efficiency. Each cheatsheet provides detailed command options, examples, and descriptions to help you master various tools and technologies.