Introduction
Apache Cassandra is a highly scalable, distributed NoSQL database designed to handle large amounts of data across many commodity servers with no single point of failure. This cheat sheet provides a quick reference to some of the most commonly used Cassandra commands and CQL (Cassandra Query Language) statements.
Apache Cassandra Commands Cheat Sheet
Keyspace Management
Command |
Description |
Syntax |
Example |
CREATE KEYSPACE |
Creates a new keyspace. |
CREATE KEYSPACE keyspace_name WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}; |
CREATE KEYSPACE school WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}; |
ALTER KEYSPACE |
Modifies an existing keyspace. |
ALTER KEYSPACE keyspace_name WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': 3}; |
ALTER KEYSPACE school WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': 3}; |
DROP KEYSPACE |
Deletes a keyspace and all its data. |
DROP KEYSPACE keyspace_name; |
DROP KEYSPACE school; |
DESCRIBE KEYSPACE |
Displays the schema of a keyspace. |
DESCRIBE KEYSPACE keyspace_name; |
DESCRIBE KEYSPACE school; |
Table Management
Command |
Description |
Syntax |
Example |
CREATE TABLE |
Creates a new table. |
CREATE TABLE keyspace_name.table_name (column_definitions PRIMARY KEY (column_name)); |
CREATE TABLE school.students (student_id UUID PRIMARY KEY, first_name TEXT, last_name TEXT, age INT); |
ALTER TABLE |
Modifies an existing table. |
ALTER TABLE keyspace_name.table_name ADD column_name data_type; |
ALTER TABLE school.students ADD email TEXT; |
DROP TABLE |
Deletes a table and all its data. |
DROP TABLE keyspace_name.table_name; |
DROP TABLE school.students; |
TRUNCATE TABLE |
Deletes all data from a table. |
TRUNCATE TABLE keyspace_name.table_name; |
TRUNCATE TABLE school.students; |
DESCRIBE TABLE |
Displays the schema of a table. |
DESCRIBE TABLE keyspace_name.table_name; |
DESCRIBE TABLE school.students; |
Data Manipulation
Command |
Description |
Syntax |
Example |
INSERT |
Adds a new record to a table. |
INSERT INTO keyspace_name.table_name (column1, column2) VALUES (value1, value2); |
INSERT INTO school.students (student_id, first_name, last_name, age) VALUES (uuid(), 'John', 'Doe', 20); |
UPDATE |
Modifies existing records in a table. |
UPDATE keyspace_name.table_name SET column1 = value1 WHERE condition; |
UPDATE school.students SET age = 21 WHERE student_id = 'some-uuid'; |
DELETE |
Removes records from a table. |
DELETE FROM keyspace_name.table_name WHERE condition; |
DELETE FROM school.students WHERE student_id = 'some-uuid'; |
SELECT |
Retrieves data from a table. |
SELECT column1, column2 FROM keyspace_name.table_name WHERE condition; |
SELECT first_name, last_name FROM school.students WHERE age > 18; |
Batch Operations
Command |
Description |
Syntax |
Example |
BEGIN BATCH |
Begins a batch operation. |
BEGIN BATCH ... APPLY BATCH; |
BEGIN BATCH INSERT INTO school.students (student_id, first_name, last_name, age) VALUES (uuid(), 'Jane', 'Doe', 22); INSERT INTO school.students (student_id, first_name, last_name, age) VALUES (uuid(), 'Tom', 'Smith', 23); APPLY BATCH; |
User Management
Command |
Description |
Syntax |
Example |
CREATE USER |
Creates a new user. |
CREATE USER username WITH PASSWORD 'password'; |
CREATE USER john WITH PASSWORD 'password123'; |
ALTER USER |
Modifies an existing user. |
ALTER USER username WITH PASSWORD 'new_password'; |
ALTER USER john WITH PASSWORD 'newpassword123'; |
DROP USER |
Deletes a user. |
DROP USER username; |
DROP USER john; |
GRANT |
Grants a role or permission to a user. |
GRANT permission ON resource TO username; |
GRANT ALL ON KEYSPACE school TO john; |
REVOKE |
Revokes a role or permission from a user. |
REVOKE permission ON resource FROM username; |
REVOKE ALL ON KEYSPACE school FROM john; |
Index Management
Command |
Description |
Syntax |
Example |
CREATE INDEX |
Creates an index on a table column. |
CREATE INDEX index_name ON keyspace_name.table_name (column_name); |
CREATE INDEX student_last_name_idx ON school.students (last_name); |
DROP INDEX |
Deletes an index from a table. |
DROP INDEX keyspace_name.index_name; |
DROP INDEX school.student_last_name_idx; |
CQL Functions
Command |
Description |
Syntax |
Example |
TTL |
Returns the time-to-live value for a column. |
SELECT TTL(column_name) FROM keyspace_name.table_name WHERE condition; |
SELECT TTL(age) FROM school.students WHERE student_id = 'some-uuid'; |
WRITETIME |
Returns the write timestamp of a column. |
SELECT WRITETIME(column_name) FROM keyspace_name.table_name WHERE condition; |
SELECT WRITETIME(age) FROM school.students WHERE student_id = 'some-uuid'; |
Conclusion
Mastering Cassandra commands and CQL is essential for efficiently managing and querying Cassandra databases. This cheat sheet provides a quick reference to some of the most commonly used commands, helping you navigate and operate your Cassandra databases more effectively. Keep this guide handy to make the most of Apache Cassandra. Happy coding!
By understanding and using these commands, you can simplify your database management processes, enhance your efficiency, and ensure your databases are well-maintained.
Comments
Post a Comment
Leave Comment