Apache Cassandra
cqlsh, keyspaces and tables, consistency, and nodetool for cluster status, repair and compaction.
cqlsh
Connect
cqlsh <host> 9042 -u <user>
Run a CQL file
cqlsh <host> -f schema.cql
List keyspaces
DESCRIBE KEYSPACES;Tables in a keyspace
USE <keyspace>; DESCRIBE TABLES;
Table schema
DESCRIBE TABLE <keyspace>.<table>;
Set consistency for the session
CONSISTENCY QUORUM;Show query traceadvanced
TRACING ON;Keyspaces and data
Create a keyspace (multi-DC)
CREATE KEYSPACE <keyspace> WITH replication = {'class': 'NetworkTopologyStrategy', 'dc1': 3};
Create a table
CREATE TABLE <keyspace>.events ( user_id uuid, ts timestamp, event text, PRIMARY KEY ((user_id), ts) ) WITH CLUSTERING ORDER BY (ts DESC);
Insert with a TTL (1 day)
INSERT INTO <keyspace>.events (user_id, ts, event) VALUES (uuid(), toTimestamp(now()), 'login') USING TTL 86400;
Export a table to CSV
cqlsh <host> -e "COPY <keyspace>.<table> TO 'out.csv' WITH HEADER = true"
nodetool
Cluster status
nodetool statusUN = Up/Normal. Anything else (DN, UJ, UL) needs attention.
Node info and load
nodetool infoTable statistics
nodetool tablestats <keyspace>.<table>
Thread pools (pending/blocked tasks)advanced
nodetool tpstatsRepair the primary range of a keyspaceadvanced
nodetool repair -pr <keyspace>
Remove data the node no longer ownsadvanced
nodetool cleanup <keyspace>
Run on every node after adding nodes to the cluster.
Snapshot before risky changesadvanced
nodetool snapshot -t before-change <keyspace>
Flush memtables and stop accepting writes (before shutdown)advanced
nodetool drain