cql_cheatsheet
cql_cheatsheet
html
1 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
ALTER COLUMNFAMILY
Changing the type of a typed column
Adding a typed column
Dropping a typed column
Common Idioms
Specifying Consistency
Versioning
Changes
Syntax conventions
To aid in specifying the CQL syntax, we will use the following conventions in this
document:
Overall syntax
CQL consists of statements. As in SQL, some statements directly make changes to
data, some look up data, and some change the way data is stored.
2 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
;
<dataChangeStatement> ::= <insertStatement>
| <updateStatement>
| <batchStatement>
| <deleteStatement>
| <truncateStatement>
;
<schemaChangeStatement> ::= <createKeyspaceStatement>
| <createColumnFamilyStatement>
| <createIndexStatement>
| <dropKeyspaceStatement>
| <dropColumnFamilyStatement>
| <dropIndexStatement>
| <alterTableStatement>
;
String literals and identifiers (including keyspace and column family names) are
case-sensitive, but CQL keywords are not. We show CQL keywords in this document
in UPPERCASE merely as a convention to aid readability.
Comments in CQL can begin with a double dash (--) or a double slash (//) and
extend to the end of the line. Multiline comments are enclosed in /* ... */.
3 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
Syntax:
<storageType> ::= "ascii"
| "bigint"
| "blob"
| "boolean"
| "counter"
| "decimal"
| "double"
| "float"
| "int"
| "text"
| "timestamp"
| "uuid"
| "varchar"
| "varint"
;
The following table gives additional information on the available data types.
type description
ascii ASCII character string
bigint 64-bit signed long
blob Arbitrary bytes (no validation)
boolean true or false
counter Counter column (64-bit long)
decimal Variable-precision decimal
double 64-bit IEEE-754 floating point
float 32-bit IEEE-754 floating point
int 32-bit signed int
text UTF8 encoded string
timestamp A timestamp. See Working with dates below for more information.
uuid Type 1 or type 4 UUID
varchar UTF8 encoded string
varint Arbitrary-precision integer
Note: In addition to the recognized types listed above, it is also possible to supply a
string containing the name of a class (a sub-class of AbstractType loadable by
Cassandra). The class name should either be fully qualified, or relative to the
org.apache.cassandra.db.marshal package.
Values serialized with the timestamp type are encoded as 64-bit signed integers
representing a number of milliseconds since the standard base time known as “the
epoch”: January 1 1970 at 00:00:00 GMT.
4 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
Timestamp types can be input in CQL as simple long integers, giving the number of
milliseconds since the epoch, as defined above.
Timestamp types can also be input as string literals in any of the following ISO 8601
formats, each representing the time and date Jan 2, 2003, at 04:05:00 AM, GMT.:
2011-02-03 04:05+0000
2011-02-03 04:05:00+0000
2011-02-03T04:05+0000
2011-02-03T04:05:00+0000
The +0000 above is an RFC 822 4-digit time zone specification; +0000 refers to GMT.
US Pacific Standard Time is -0800. The time zone may be omitted if desired— the
date will be interpreted as being in the time zone under which the coordinating
Cassandra node is configured.
2011-02-03 04:05
2011-02-03 04:05:00
2011-02-03T04:05
2011-02-03T04:05:00
There are clear difficulties inherent in relying on the time zone configuration being
as expected, though, so it is recommended that the time zone always be specified
for timestamps when feasible.
The time of day may also be omitted, if the date is the only piece that matters:
2011-02-03
2011-02-03+0000
In that case, the time of day will default to 00:00:00, in the specified or default time
zone.
USE
Syntax:
<useStatement> ::= "USE" <term>
;
Sample:
USE myApp;
A USE statement consists of the USE keyword, followed by a valid keyspace name. Its
purpose is to assign the per-connection, current working keyspace. All subsequent
keyspace-specific actions will be performed in the context of the keyspace selected,
unless otherwise specified, until another USE statement is issued or the connection
terminates.
5 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
SELECT
Syntax:
<selectStatement> ::= "SELECT" <whatToSelect>
"FROM" ( <name> "." )? <name>
( "USING" "CONSISTENCY" <consistencylevel> )?
( "WHERE" <selectWhereClause> )?
( "LIMIT" <integer> )?
;
<whatToSelect> ::= <term> ( "," <term> )*
| ("FIRST" <integer> )? "REVERSED"? <columnRange>
| "COUNT" "(" <countTarget> ")"
;
<columnRange> ::= <term> ".." <term>
| "*"
;
<countTarget> ::= "*"
| "1"
;
<name> ::= <identifier>
| <stringLiteral>
| <integer>
;
<selectWhereClause> ::= <relation> ( "AND" <relation> )*
| <term> "IN" "(" <term> ( "," <term> )* ")"
;
<relation> ::= <term> <relationOperator> <term>
;
<relationOperator> ::= "=" | "<" | ">" | "<=" | ">="
;
Sample:
SELECT Name, Occupation FROM People WHERE key IN (199, 200, 207);
SELECT FIRST 3 REVERSED 'time199'..'time100' FROM Events;
SELECT COUNT(*) FROM system.Migrations;
A SELECT is used to read one or more records from a Cassandra column family. It
returns a result-set of rows, where each row consists of a key and a collection of
columns corresponding to the query.
Specifying Columns
SELECT col1, col2 FROM ...
SELECT range_lo..range_hi FROM ...
SELECT * FROM ...
SELECT FIRST 4 REVERSED range_hi..range_lo FROM ...
The SELECT expression determines which columns will appear in the results and can
take a few different forms, as shown above. The simplest is a comma-separated list
of column names. Note that column names in Cassandra can be specified with string
literals or integers, in addition to identifiers.
6 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
It is also possible to specify a range of column names. The range notation consists of
start and end column names, separated by two periods (..). The set of columns
returned for a range is start and end inclusive. A single star (*) may be used as a
range to request “all columns”.
When using a range, it is sometimes useful to limit the number of columns that can
be returned as part of each row (since Cassandra is schemaless, it is not necessarily
possible to determine ahead of time how many columns will be in the result set). To
accomplish this, use the FIRST clause with an integer to specify an upper limit on the
number of columns returned per row. The default limit is 10,000 columns.
The REVERSED option causes the sort order of the columns returned to be reversed.
This affects the FIRST clause; when limiting the columns returned, the columns at the
end of the range will be selected instead of the ones at the beginning of the range.
A SELECT expression may also be COUNT(*). In this case, the result will be only one
value: the number of rows which matched the query.
It is worth noting that unlike the projection in a SQL SELECT, there is no guarantee
that the results will contain all of the columns specified, because Cassandra is
schemaless.
Column Family
SELECT ... FROM MyApp.LocationSnapshots ...;
SELECT ... FROM EventTimeline ...;
The FROM clause is used to specify the Cassandra column family applicable to a SELECT
query. Unlike other operations on column families, the keyspace in which the
column family exists may also be specified by giving its name before the column
family name, and separating them by a dot (.). If the keyspace is not specified, the
current keyspace will be used, as per normal.
Consistency Level
SELECT ... USING CONSISTENCY QUORUM;
Filtering rows
SELECT ... WHERE KEY = 11194251 AND startdate = '2011-10-08-0500';
SELECT ... WHERE KEY >= 'AM' and KEY =< 'AZ' AND module = 17;
SELECT ... WHERE keyalias IN ('key1', 'key2', 'key3', ...);
The WHERE clause provides for filtering the rows that appear in results. The clause can
filter on a key name, or range of keys, and in the case of indexed columns, on
column values. Key filters are specified using the KEY keyword or key alias name,
7 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
followed by a relational operator (one of =, >, >=, <, and <=), and then a term value.
When terms appear on both sides of a relational operator it is assumed the filter
applies to an indexed column. With column index filters, the term on the left of the
operator must be the name of the indexed column, and the term on the right is the
value to filter on.
Note: The greater-than and less-than operators (> and <) result in key ranges that
are inclusive of the terms. There is no supported notion of “strictly” greater-than or
less-than; these operators are merely supported as aliases to >= and <=.
Limits
SELECT ... WHERE favoriteArtist = 'The Mighty Mighty Bosstones' LIMIT 90000;
The LIMIT option to a SELECT expression limits the number of rows returned by a query.
LIMIT defaults to 10,000 when left unset.
INSERT
Syntax:
Sample:
The first column name in the INSERT list must be the name of the column family key.
Also, there must be more than one column name specified (Cassandra rows are not
considered to exist with only a key and no associated columns).
Unlike in SQL, the semantics of INSERT and UPDATE are identical. In either case a
record is created if none existed before, and updated when it does. For information
on query modifiers and types, see the UPDATE section below.
UPDATE
8 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
Syntax:
<updateStatement> ::= "UPDATE" <name>
( "USING" <usingOption> ( "AND" <usingOption> )* )?
"SET" <assignment> ( "," <assignment> )*
"WHERE" <updateWhereClause>
;
<assignment> ::= <term> "=" <term>
| <term> "=" <term> "+" <term>
| <term> "=" <term> "-" <term>
;
<updateWhereClause> ::= <term> "=" <term>
| <term> "IN" "(" <term> ( "," <term> )* ")"
;
Sample:
Column Family
Statements begin with the UPDATE keyword followed by a Cassandra column family
name.
Consistency Level
UPDATE Foo USING CONSISTENCY EACH_QUORUM ...
Following the column family identifier is an optional USING clause, which can specify
the consistency level for the update, or the timestamp and/or the TTL for the new
columns.
Timestamp
UPDATE Foo USING TIMESTAMP=1318452291034 ...
TTL
UPDATE Foo USING TTL=6800 ...
UPDATE supports setting a time to live (TTL), in seconds, for each of the added
9 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
columns.
Rows are created or updated by supplying column names and values, after the SET
keyword, in term assignment format. Multiple columns can be set by separating the
name/value pairs using commas. Each update statement requires a precise set of
row keys to be specified using a WHERE clause and the KEY keyword or key alias.
DELETE
Syntax:
<deleteStatement> ::= "DELETE" ( <term> ( "," <term> )* )?
"FROM" <name>
( "USING" <deleteOption> ( "AND" <deleteOption> )* )?
"WHERE" <updateWhereClause>
;
<deleteOption> ::= "CONSISTENCY" <consistencylevel>
| "TIMESTAMP" <integer>
;
Sample:
DELETE col1, col2, col3 FROM Planeteers USING CONSISTENCY ONE WHERE KEY = 'Captain';
DELETE FROM MastersOfTheUniverse WHERE KEY IN ('Man-At-Arms', 'Teela');
A DELETE is used to perform the removal of one or more columns from one or more
rows. The key can be given using the KEY keyword or by the key alias set per column
family.
Specifying Columns
10 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
Column Family
The column family name follows the list of column names and the keyword FROM.
Consistency Level
Specifying Rows
DELETE ... WHERE KEY = 'some_key_value';
DELETE ... WHERE keyalias IN (key1, key2);
The WHERE clause is used to determine to which row(s) a DELETE applies. The first form
allows the specification of a single keyname using the KEY keyword (or by key alias)
and the = operator. The second form allows a list of keyname terms to be specified
using the IN notation and a parenthesized list of comma-delimited keyname terms.
TRUNCATE
Syntax:
Sample:
TRUNCATE super_important_data;
accepts a single argument for the column family name, and permanently
TRUNCATE
removes all data from said column family.
BATCH
Syntax:
<batchStatement> ::= "BEGIN" "BATCH"
( "USING" <usingOption> ( "AND" <usingOption> )* )?
<batchStatementMember> ( ";" <batchStatementMember> )*
"APPLY" "BATCH"
;
<batchStatementMember> ::= <insertStatement>
| <updateStatement>
| <deleteStatement>
;
Sample:
11 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
A single consistency level is used for the entire batch. It appears after the BEGIN BATCH
statement, and uses the standard consistency level specification. Batched
statements default to CONSISTENCY.ONE when left unspecified.
Only data modification statements (specifically, UPDATE, INSERT, and DELETE) are allowed
in a BATCH statement. BATCH is not an analogue for SQL transactions.
NOTE: While there are no isolation guarantees, UPDATE queries are atomic within a
given record.
CREATE KEYSPACE
Syntax:
<createKeyspaceStatement> ::= "CREATE" "KEYSPACE" <name>
"WITH" <optionName> "=" <optionVal>
( "AND" <optionName> "=" <optionVal> )*
;
<optionName> ::= <identifier>
| <optionName> ":" <identifier>
| <optionName> ":" <integer>
;
<optionVal> ::= <stringLiteral>
| <identifier>
| <integer>
;
Sample:
The CREATE KEYSPACE statement creates a new top-level namespace (aka “keyspace”).
Valid names are any string constructed of alphanumeric characters and
underscores. Names which do not work as valid identifiers or integers should be
quoted as string literals. Properties such as replication strategy and count are
specified during creation using the following accepted keyword arguments:
12 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
CREATE COLUMNFAMILY
Syntax:
Sample:
When creating a new column family, you must specify the key type. The list of
possible types is identical to column comparators/validators (see Data Storage
13 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
Types), except it probably does not make sense to use counter for a key. It’s
important to note that the key type you use must be compatible with the partitioner
in use. For example, OrderPreservingPartitioner and CollatingOrderPreservingPartitioner
both require UTF-8 keys. If you use an identifier for the primary key name, instead
of the KEY keyword, a key alias will be set automatically.
14 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
comment.
A factory
for the
SerializingCacheProvider if JNA is
cache with
row_cache_provider present, otherwise
which to
ConcurrentHashMapCacheProvider
back the
row cache.
Number of
rows whose
entire
row_cache_size 0
contents to
cache in
memory.
Number of
keys per
SSTable
whose
key_cache_size 200000 locations
are kept in
memory in
“mostly
LRU” order.
The
probability
with which
read repairs
read_repair_chance 1.0
should be
invoked on
non-quorum
reads.
Time to wait
before
garbage
gc_grace_seconds 864000 collecting
tombstones
(deletion
markers).
Determines
the default
storage type
of column
default_validation text
values
(which itself
determines
15 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
the
validation
for column
values).
This option
does not
affect the
types of
columns
which were
defined in a
CREATE
COLUMNFAMILY
statement—
only new
columns.
Valid values
are listed in
the Data
Storage
Types table
above.
Minimum
number of
SSTables
min_compaction_threshold 4 needed to
start a
minor
compaction.
Maximum
number of
SSTables
allowed
max_compaction_threshold 32
before a
minor
compaction
is forced.
Number of
seconds
row_cache_save_period_in_seconds 0 between
saving row
caches.
Number of
key_cache_save_period_in_seconds 14400 seconds
between
16 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
saving key
caches.
replicate_on_write false
CREATE INDEX
Syntax:
<createIndexStatement> ::= "CREATE" "INDEX" <identifier>? "ON"
<name> "(" <term> ")"
;
Sample:
CREATE INDEX userIndex ON NerdMovies (user);
CREATE INDEX ON Mutants (abilityId);
A CREATE INDEX statement is used to create a new, automatic secondary index on the
given column family, for the named column. A name for the index itself can be
specified before the ON keyword, if desired. A single column name must be specified
inside the parentheses. It is not necessary for the column to exist on any current
rows (Cassandra is schemaless), but the column must already have a type (specified
during the CREATE COLUMNFAMILY, or added afterwards with ALTER COLUMNFAMILY.
DROP KEYSPACE
Syntax:
<dropKeyspaceStatement> ::= "DROP" "KEYSPACE" <name>
;
Sample:
DROP COLUMNFAMILY
Syntax:
<dropColumnFamilyStatement> ::= "DROP" "COLUMNFAMILY" <name>
;
Sample:
17 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
DROP INDEX
Syntax:
<dropIndexStatement> ::= "DROP" "INDEX" <name>
;
Sample:
ALTER COLUMNFAMILY
Syntax:
<alterTableStatement> ::= "ALTER" "COLUMNFAMILY" <name> <alterInstructions>
;
<alterInstructions> ::= "ALTER" <name> "TYPE" <storageType>
| "ADD" <name> <storageType>
| "DROP" <name>
;
Specify the name of the column family to be changed after the ALTER COLUMNFAMILY
keywords, and the name of the column to be changed, added, or dropped after the
keyword corresponding to the type of change desired (ALTER, ADD, DROP).
18 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
The ALTER COLUMNFAMILY ... ADD variant adds a typed column to a column family. The
column must not already have a type in the column family metadata. The same
warnings from the above ALTER section, about there being no validation of existing
data, apply here as well.
An ALTER COLUMNFAMILY ... DROP statement removes the type of a column from the
column family metadata. Note that this does not remove the column from current
rows; it just removes the metadata saying that the bytes stored under that column
are expected to be deserializable according to a certain type.
Common Idioms
Specifying Consistency
<consistency> ::= "ANY"
| "ONE"
| "QUORUM"
| "ALL"
| "LOCAL_QUORUM"
| "EACH_QUORUM"
;
Consistency level specifications are made up the keywords USING CONSISTENCY, followed
by a consistency level identifier. Valid consistency level identifiers are as listed
above. When not specified, USING CONSISTENCY ONE is the default.
Versioning
Versioning of the CQL language adheres to the Semantic Versioning guidelines.
Versions take the form X.Y.Z where X, Y, and Z are integer values representing
major, minor, and patch level respectively. There is no correlation between
Cassandra release versions and the CQL language version.
version description
The major version must be bumped when backward incompatible changes
Major
are introduced. This should rarely (if ever) occur.
19 of 20 27-02-2015 17:59
CQL https://cassandra.apache.org/doc/cql/CQL.html
Changes
Wed, 12 Oct 2011 16:53:00 -0500 - paul cannon
* Rework whole doc, adding syntax specifics and additional explanations
20 of 20 27-02-2015 17:59