Cassandra
Updated: September 10, 2025Categories: Query, NoSQL
Printed from:
Cassandra CQL (Cassandra Query Language) Cheatsheet
1. Connection and Basic Commands
Connect to Cassandra
Bash
12345678# Using cqlsh
cqlsh [hostname] [port]
cqlsh localhost 9042 # Default connection
# Show cluster information
SHOW VERSION;
SHOW HOST;
Authentication (if enabled)
SQL
123-- Connect with username and password
cqlsh -u [username] -p [password]
2. Keyspace Operations
Create Keyspace
SQL
123456789101112131415161718-- Simple replication
CREATE KEYSPACE my_keyspace
WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': 3
};
-- Network topology replication
CREATE KEYSPACE my_keyspace
WITH replication = {
'class': 'NetworkTopologyStrategy',
'datacenter1': 3,
'datacenter2': 2
};
-- Use keyspace
USE my_keyspace;
Alter Keyspace
SQL
123456ALTER KEYSPACE my_keyspace
WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': 4
};
Drop Keyspace
SQL
12DROP KEYSPACE my_keyspace;
3. Table Operations and Column Families
Create Table
SQL
1234567891011121314151617CREATE TABLE users (
user_id UUID PRIMARY KEY,
username TEXT,
email TEXT,
age INT,
registration_date TIMESTAMP
);
-- Composite Primary Key
CREATE TABLE user_posts (
user_id UUID,
post_id TIMEUUID,
content TEXT,
likes INT,
PRIMARY KEY ((user_id), post_id)
) WITH CLUSTERING ORDER BY (post_id DESC);
Alter Table
SQL
123456-- Add column
ALTER TABLE users ADD phone_number TEXT;
-- Drop column
ALTER TABLE users DROP phone_number;
Drop Table
SQL
12DROP TABLE users;
4. CQL Data Types and Collections
Primitive Types
text: UTF-8 encoded stringint: 32-bit signed integerbigint: 64-bit signed integerfloat,double: Floating-point numbersboolean: true/falsetimestamp: Date and timeuuid: Unique identifiertimeuuid: Time-based unique identifier
Collection Types
SQL
12345678910111213141516-- List
CREATE TABLE example (
id UUID PRIMARY KEY,
tags list<text>
);
-- Set (unique elements)
tags set<text>
-- Map (key-value pairs)
preferences map<text, text>
-- Inserting collections
INSERT INTO example (id, tags) VALUES
(uuid(), ['tag1', 'tag2', 'tag3']);
5. CRUD Operations
Insert
SQL
1234INSERT INTO users (user_id, username, email)
VALUES (uuid(), 'johndoe', 'john@example.com')
USING TTL 86400; # Optional time-to-live
Select
SQL
12345678910111213141516-- Basic select
SELECT * FROM users;
-- Filtering
SELECT * FROM users WHERE username = 'johndoe';
-- Pagination
SELECT * FROM users LIMIT 10;
-- Complex queries
SELECT username, email
FROM users
WHERE user_id IN (uuid1, uuid2)
AND age > 25
ALLOW FILTERING;
Update
SQL
12345678910UPDATE users
SET email = 'new_email@example.com',
age = 30
WHERE user_id = uuid();
-- Increment counter
UPDATE user_stats
SET login_count = login_count + 1
WHERE user_id = uuid();
Delete
SQL
1234567DELETE FROM users WHERE user_id = uuid();
-- Delete specific columns
DELETE email, age
FROM users
WHERE user_id = uuid();
6. Primary Keys, Partition Keys, and Clustering Keys
SQL
12345678910-- Composite Partition Key
CREATE TABLE sensor_data (
datacenter text,
rack text,
sensor_id uuid,
timestamp timeuuid,
temperature float,
PRIMARY KEY ((datacenter, rack), sensor_id, timestamp)
) WITH CLUSTERING ORDER BY (sensor_id ASC, timestamp DESC);
7. Indexing
Secondary Indexes
SQL
123456-- Create index
CREATE INDEX ON users (email);
-- Query with indexed column
SELECT * FROM users WHERE email = 'john@example.com';
Materialized Views
SQL
12345CREATE MATERIALIZED VIEW users_by_email AS
SELECT * FROM users
WHERE email IS NOT NULL
PRIMARY KEY (email, user_id);
8. Time-to-Live (TTL) and Timestamps
SQL
12345678-- Insert with TTL (seconds)
INSERT INTO users (user_id, username)
VALUES (uuid(), 'tempuser')
USING TTL 3600; # Expires in 1 hour
-- Get TTL of a column
SELECT TTL(username) FROM users;
9. Consistency Levels
SQL
1234567-- Consistency levels: ONE, QUORUM, ALL, LOCAL_QUORUM
CONSISTENCY QUORUM;
-- Per-query consistency
SELECT * FROM users
USING CONSISTENCY QUORUM;
10. User-Defined Types and Functions
SQL
123456789101112-- User-Defined Type
CREATE TYPE address (
street text,
city text,
zip_code text
);
-- User-Defined Function
CREATE FUNCTION fahrenheit_to_celsius(f double)
RETURNS double LANGUAGE java AS
'return (f - 32.0) * 5/9;';
11. Batch Operations
SQL
12345BEGIN BATCH
INSERT INTO users (user_id, username) VALUES (uuid1, 'user1');
INSERT INTO user_stats (user_id, login_count) VALUES (uuid1, 1);
APPLY BATCH;
12. Performance Optimization
Best Practices
- Design for horizontal scalability
- Minimize wide rows
- Use appropriate partition keys
- Avoid cross-partition queries
- Leverage time-series data models
- Use
ALLOW FILTERINGsparingly
Denormalization Example
SQL
123456789-- Denormalized user_timeline for faster reads
CREATE TABLE user_timeline (
user_id uuid,
post_id timeuuid,
content text,
author_name text,
PRIMARY KEY ((user_id), post_id)
) WITH CLUSTERING ORDER BY (post_id DESC);
13. Time-Series and IoT Data Patterns
SQL
123456789-- IoT Sensor Data Model
CREATE TABLE sensor_readings (
device_id uuid,
reading_time timeuuid,
temperature float,
humidity float,
PRIMARY KEY ((device_id), reading_time)
) WITH CLUSTERING ORDER BY (reading_time DESC);
Additional Tips
- Use
TRACING ON;to understand query performance - Monitor with
nodetoolfor cluster health - Use
EXPLAINto analyze query execution plans
Note: Cassandra is a distributed, high-availability NoSQL database optimized for write-heavy workloads and linear scalability.
Continue Learning
Discover more cheatsheets to boost your productivity