Connect
cqlsh <host> <port>
Connect to a node (default port 9042)
cqlsh -u <user> -p <password>
<host>
Authenticate on connect
A password on the command line shows up in the process list;
prefer a credentials file.
cqlsh -k <keyspace> <host>
Start in a keyspace
cqlsh --ssl <host>
Connect over TLS
cqlsh -e "<statement>" <host>
Run one statement and quit
cqlsh -f <file.cql> <host>
Run a script file and quit
cqlsh --request-timeout=<sec> <host>
Raise the request timeout (default 10)
cqlsh --cqlshrc=<file> <host>
Use another cqlshrc
cqlsh --no-color <host>
Disable colour output
cqlsh --version
Print the cqlsh version
Session control
USE <keyspace>;
Make a keyspace current
CONSISTENCY <level>
Set consistency level
Levels: ANY, ONE, TWO, THREE, QUORUM, ALL, LOCAL_QUORUM,
LOCAL_ONE, SERIAL, LOCAL_SERIAL.
CONSISTENCY
Show current consistency level
SERIAL CONSISTENCY <level>
Consistency for IF (LWT) writes
Takes SERIAL or LOCAL_SERIAL.
TRACING ON
Print a trace after each query
EXPAND ON
One column per line
EXPAND OFF
Back to table output
PAGING ON
Page results with a prompt
PAGING <rows>
Set the page size
PAGING OFF
Print all rows at once
Basic CQL
CREATE KEYSPACE <ks> WITH replication =
{'class': 'SimpleStrategy',
'replication_factor': 1};
Create a keyspace
CREATE TABLE <table> (<id> uuid PRIMARY KEY,
<col> text);
Create a table
INSERT INTO <table> (<id>, <col>) VALUES
(<id>, '<val>');
Insert a row
SELECT * FROM <table> WHERE <id> =
<value>;
Read by partition key
SELECT * FROM <table> LIMIT <n>;
Limit rows returned
UPDATE <table> SET <col> =
'<val>' WHERE <id> =
<value>;
Change a column
DELETE FROM <table> WHERE <id> =
<value>;
Delete a row
TRUNCATE <table>;
Remove all rows
ALTER TABLE <table> ADD <col>
<type>;
Add a column
DROP TABLE <table>;
Delete a table
CREATE INDEX ON <table> (<col>);
Secondary index on a column
SELECT * FROM <table> WHERE <col> =
'<val>' ALLOW FILTERING;
Filter without index (slow)
Scans many partitions; avoid on large tables.
COPY import and export
COPY <table> TO '<file.csv>' WITH
HEADER = true;
Export a table to CSV
COPY <table> FROM '<file.csv>' WITH
HEADER = true;
Import CSV into a table
COPY <table> (<col1>, <col2>) TO
STDOUT;
Export chosen columns
COPY <table> FROM '<file.csv>' WITH
INGESTRATE = <n>;
Throttle an import
COPY <table> FROM '<file.csv>' WITH
ERRFILE = '<file>';
Write rejected rows to a file
COPY <table> TO '<file>' WITH PAGESIZE
= <n>;
Rows per page when exporting
COPY defaults
| Option |
Default |
Meaning |
PAGESIZE |
1000 |
Rows per page (TO) |
MAXREQUESTS |
6 |
Token ranges at once (TO) |
INGESTRATE |
100000 |
Rows per second (FROM) |
MAXBATCHSIZE |
20 |
Rows per batch (FROM) |
NUMPROCESSES |
16 |
Worker processes |
HEADER |
false |
First line holds names |