Cassandra Query Language (CQL) v2.0 reference


This is an update to my two previous posts:
http://crlog.info/2011/03/29/cassandra-query-language-aka-cql-syntax/ AND
http://crlog.info/2011/06/13/cassandra-query-language-cql-v1-0-0-updated/
If you’re using versions of Cassandra prior v1.0 beta one of the above links may be more appropriate as little things may have changed here and there. The official doc is on Gitub in textile markup here https://github.com/apache/cassandra/blob/trunk/doc/cql/CQL.textile

Cassandra Query Language (CQL) v2.0

Table of Contents

  1. Cassandra Query Language (CQL) v2.0
    1. Table of Contents
    2. USE
    3. SELECT
      1. Specifying Columns
      2. Column Family
      3. Consistency Level
      4. Filtering rows
      5. Limits
    4. ALTER TABLE
    5. INSERT
      1. Column Family
      2. Consistency Level
      3. Timestamp
      4. TTL
      5. Specifying Columns and Row
      6. Updating Counter Columns
    6. DELETE
      1. Specifying Columns
      2. Column Family
      3. Consistency Level
    7. BATCH
    8. TRUNCATE
    9. CREATE KEYSPACE
    10. CREATE COLUMNFAMILY
      1. Column Family Options
    11. CREATE INDEX
    12. DROP INDEX
    13. DROP
    14. Common Idioms
  2. Versioning
  3. Changes

USE

Synopsis:

 USE <KEYSPACE>;

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 supplied value.

SELECT

Synopsis:

 SELECT [FIRST N] [REVERSED] <SELECT EXPR> FROM <COLUMN FAMILY> [USING <CONSISTENCY>] [WHERE <CLAUSE>] [LIMIT N];

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 [FIRST N] [REVERSED] name1, name2, name3 FROM ... SELECT [FIRST N] [REVERSED] name1..nameN FROM ...

The SELECT expression determines which columns will appear in the results and takes the form of either a comma separated list of names, or a range. The range notation consists of a start and end column name separated by two periods (..). The set of columns returned for a range is start and end inclusive.

The FIRST option accepts an integer argument and can be used to apply a limit to the number of columns returned per row. When this limit is left unset it defaults to 10,000 columns.

The REVERSED option causes the sort order of the results to be reversed.

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. This is because Cassandra is schema-less and there are no guarantees that a given column exists.

Column Family

 SELECT ... FROM <COLUMN FAMILY> ...

The FROM clause is used to specify the Cassandra column family applicable to a SELECT query.

bc.
SELECT count(*) FROM …

The count aggregate function returns a single row, with a single column “count” whose value is the number of rows from the pre-aggregation resultset.

Currently, count is the only function supported by CQL.

Consistency Level

 SELECT ... [USING <CONSISTENCY>] ...

Following the column family clause is an optional consistency level specification.

Filtering rows

 SELECT ... WHERE <KEY> = keyname AND name1 = value1 SELECT ... WHERE <KEY> >= startkey and <KEY> =< endkey AND name1 = value1 SELECT ... WHERE <KEY> IN ('<key>', '<key>', '<key>', ...)

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, a relational operator, (one of =>>=<, and <=), and 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 is the name, 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 <CLAUSE> [LIMIT N] ...

Limiting the number of rows returned can be achieved by adding the LIMIT option to a SELECT expression. LIMIT defaults to 10,000 when left unset.

ALTER TABLE

Synopsis:

bc.
ALTER TABLE ADD ;
ALTER TABLE ALTER TYPE ;
ALTER TABLE DROP ;

An ALTER is used to manipulate with ColumnFamily columns. It allows you to add new columns, alter and drop existing columns. No results are returned.

INSERT

Synopsis:

 INSERT INTO <COLUMN FAMILY> (<KEY>, <col>, <col>, ...) VALUES (<key>, <val>, <val>, ...) [USING CONSISTENCY <LEVEL> [AND TIMESTAMP <timestamp>] [AND TTL <timeToLive>]];

An INSERT is used to write one or more columns to a record in a Cassandra column family. No results are returned.

Unlike 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 modifiers and types, see theUPDATE section below.

UPDATE

Synopsis:

 UPDATE <COLUMN FAMILY> [USING <CONSISTENCY> [AND TIMESTAMP <timestamp>] [AND TTL <timeToLive>]] SET name1 = value1, name2 = value2 WHERE <KEY> = keyname;

An UPDATE is used to write one or more columns to a record in a Cassandra column family. No results are returned. Key can be given using KEY keyword or by alias set per ColumnFamily.

Column Family

 UPDATE <COLUMN FAMILY> ...

Statements begin with the UPDATE keyword followed by a Cassandra column family name.

Consistency Level

 UPDATE ... [USING <CONSISTENCY>] ...

Following the column family identifier is an optional consistency level specification.

Timestamp

bc.
UPDATE … [USING TIMESTAMP ] …

UPDATE supports setting client-supplied optional timestamp for modification.

TTL

bc.
UPDATE … [USING TTL ] …

UPDATE supports setting time to live (TTL) for each of the columns in UPDATE statement.

Specifying Columns and Row

 UPDATE ... SET name1 = value1, name2 = value2 WHERE <KEY> = keyname; UPDATE ... SET name1 = value1, name2 = value2 WHERE <KEY> IN ('<key>', '<key>', ...)

Rows are created or updated by supplying column names and values in term assignment format. Multiple columns can be set by separating the name/value pairs using commas. Each update statement requires exactly one key to be specified using a WHERE clause and the KEY keyword or key alias.

Updating Counter Columns

 UPDATE ... SET name1 = name1 + <value> ... UPDATE ... SET name1 = name1 - <value> ...

Counter columns can be incremented or decremented by an arbitrary numeric value though the assignment of an expression that adds or substracts the value.

DELETE

Synopsis:

 DELETE [COLUMNS] FROM <COLUMN FAMILY> [USING <CONSISTENCY>] WHERE <KEY> = keyname1 DELETE [COLUMNS] FROM <COLUMN FAMILY> [USING <CONSISTENCY>] WHERE <KEY> IN (keyname1, keyname2);

DELETE is used to perform the removal of one or more columns from one or more rows. Key can be given using KEY keyword or by alias set per ColumnFamily.

Specifying Columns

 DELETE [COLUMNS] ...

Following the DELETE keyword is an optional comma-delimited list of column name terms. When no column names are specified, the remove applies to the entire row(s) matched by the WHERE clause

Column Family

 DELETE ... FROM <COLUMN FAMILY> ...

The column family name follows the list of column names.

Consistency Level

 DELETE ... [USING <CONSISTENCY>] ...

Following the column family identifier is an optional consistency level specification.

Specifying Rows

 DELETE ... WHERE <KEY> = keyname1 DELETE ... WHERE <KEY> IN (keyname1, keyname2)

The WHERE clause is used to determine which row(s) a DELETE applies to. 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.

BATCH

Synopsis:

bc.
BATCH BEGIN BATCH [USING CONSISTENCY [AND TIMESTAMP ]]
INSERT or UPDATE or DELETE statements separated by semicolon or “end of line”
APPLY BATCH

BATCH supports setting client-supplied optional global timestamp which will be used for each of the operations included in batch.

A single consistency level is used for the entire batch, it appears after the BEGIN BATCH statement, and uses the standard consistency level specification. Batch default to CONSISTENCY.ONE when left unspecified.

NOTE: While there are no isolation guarantees, UPDATE queries are atomic within a give record.

Example:

bc.
BEGIN BATCH USING CONSISTENCY QUORUM
INSERT INTO users (KEY, password, name) VALUES (‘user2’, ‘ch@ngem3b’, ‘second user’)
UPDATE users SET password = ‘ps22dhds’ WHERE KEY = ‘user2’
INSERT INTO users (KEY, password) VALUES (‘user3’, ‘ch@ngem3c’)
DELETE name FROM users WHERE key = ‘user2’
INSERT INTO users (KEY, password, name) VALUES (‘user4’, ‘ch@ngem3c’, ‘Andrew’)
APPLY BATCH

TRUNCATE

Synopsis:

 TRUNCATE <COLUMN FAMILY>

Accepts a single argument for the column family name, and permanently removes all data from said column family.

CREATE KEYSPACE

Synopsis:

 CREATE KEYSPACE <NAME> WITH AND strategy_class = <STRATEGY> AND strategy_options.<OPTION> = <VALUE> [AND strategy_options.<OPTION> = <VALUE>];

The CREATE KEYSPACE statement creates a new top-level namespace (aka “keyspace”). Valid names are any string constructed of alphanumeric characters and underscores, but must begin with a letter. Properties such as replication strategy and count are specified during creation using the following accepted keyword arguments:

keyword required description
strategy_options no Most strategies require additional arguments which can be supplied by appending the option name to the strategy_options keyword, separated by a colon (:). For example, a strategy option of “DC1” with a value of “1” would be specified as strategy_options:DC1 = 1; replication_factor for SimpleStrategy could bestrategy_options:replication_factor=3.

CREATE COLUMNFAMILY

Synopsis:

 CREATE COLUMNFAMILY <COLUMN FAMILY> (<KEY> <type> PRIMARY KEY [, name1 type, name2 type, ...]); CREATE COLUMNFAMILY <COLUMN FAMILY> (<KEY> <type> PRIMARY KEY [, name1 type, name2 type, ...]) [WITH keyword1 = arg1 [AND keyword2 = arg2 [AND ...]]];

CREATE COLUMNFAMILY statements create new column family namespaces under the current keyspace. Valid column family names are strings of alphanumeric characters and underscores, which begin with a letter.

Specifying Key Type

 CREATE ... (<KEY> <type> PRIMARY KEY) ...

When creating a new column family, you must specify key type. The list of possible key types is identical to column comparators/validators, (see Specifying Column Type). It’s important to note that the key type must be compatible with the partitioner in use, for example OrderPreservingPartitioner and CollatingOrderPreservingPartitioner both require UTF-8 keys. If you use name instead of KEY keyword, name alias will be set automatically.

Specifying Column Type (optional)

 CREATE ... (<KEY> <type> PRIMARY KEY, name1 type, name2 type) ...

It is possible to assign columns a type during column family creation. Columns configured with a type are validated accordingly when a write occurs. Column types are specified as a parenthesized, comma-separated list of column term and type pairs. The list of recognized types are:

type description
ascii ASCII character string
bigint 8-byte long
blob Arbitrary bytes (no validation)
boolean true or false
counter Counter column, (8-byte long)
decimal Variable-precision decimal
double 8-byte floating point
float 4-byte floating point
int 4-byte int
text UTF8 encoded string
timestamp Date + Time, encoded as 8 bytes since epoch
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), either fully qualified, or relative to theorg.apache.cassandra.db.marshal package.

Column Family Options (optional)

 CREATE COLUMNFAMILY ... WITH keyword1 = arg1 AND keyword2 = arg2;

A number of optional keyword arguments can be supplied to control the configuration of a new column family.

keyword default description
comparator text Determines sorting and validation of column names. Valid values are identical to the types listed in Specifying Column Type above.
comment none A free-form, human-readable comment.
row_cache_provider SerializingCacheProvider if JNA is present, otherwise ConcurrentHashMapCacheProvider A factory for the cache with which to back the row cache.
row_cache_size 0 Number of rows whose entire contents to cache in memory.
key_cache_size 200000 Number of keys per SSTable whose locations are kept in memory in “mostly LRU” order.
read_repair_chance 1.0 The probability with which read repairs should be invoked on non-quorum reads.
gc_grace_seconds 864000 Time to wait before garbage collecting tombstones (deletion markers).
default_validation text Determines validation of column values. Valid values are identical to the types listed inSpecifying Column Type above.
min_compaction_threshold 4 Minimum number of SSTables needed to start a minor compaction.
max_compaction_threshold 32 Maximum number of SSTables allowed before a minor compaction is forced.
row_cache_save_period_in_seconds 0 Number of seconds between saving row caches.
key_cache_save_period_in_seconds 14400 Number of seconds between saving key caches.
replicate_on_write false

CREATE INDEX

Synopsis:

CREATE INDEX [index_name] ON <column_family> (column_name);

CREATE INDEX statement is used to create a new, automatic secondary index for the named column.

DROP INDEX

Synopsis:

DROP INDEX <INDEX_NAME>

DROP INDEX statement is used to drop an existing secondary index.
DROP INDEX statement will search all ColumnFamilies in the current Keyspace for specified index and delete it if found.

DROP

Synopsis:

DROP <KEYSPACE|COLUMNFAMILY> namespace;

DROP statements result in the immediate, irreversible removal of keyspace and column family namespaces.

Common Idioms

Specifying Consistency

 ... USING <CONSISTENCY> ...

Consistency level specifications are made up the keyword USING, followed by a consistency level identifier. Valid consistency levels are as follows:

  • CONSISTENCY ANY
  • CONSISTENCY ONE (default)
  • CONSISTENCY QUORUM
  • CONSISTENCY ALL
  • CONSISTENCY LOCAL_QUORUM
  • CONSISTENCY EACH_QUORUM

Term specification

Terms are used in statements to specify things such as keyspaces, column families, indexes, column names and values, and keyword arguments. The rules governing term specification are as follows:

  • Any single quoted string literal (example: 'apple').
  • Unquoted alpha-numeric strings that begin with a letter (example: carrot).
  • Unquoted numeric literals (example: 100).
  • UUID strings in hyphen-delimited hex notation (example: 1438fc5c-4ff6-11e0-b97f-0026c650d722).

Terms which do not conform to these rules result in an exception.

How column name/value terms are interpreted is determined by the configured type.

type term
ascii Any string which can be decoded using ASCII charset
text / varchar Any string which can be decoded using UTF8 charset
uuid Standard UUID string format (hyphen-delimited hex notation)
uuid Standard UUID string format (hyphen-delimited hex notation)
uuid The string now, to represent a type-1 (time-based) UUID with a date-time component based on the current time
uuid Numeric value representing milliseconds since epoch
uuid An iso8601 timestamp
int Integer value capable of fitting in 8 bytes (same as bigint)
bigint Integer value capable of fitting in 8 bytes
varint Integer value of arbitrary size
bytea Hex-encoded strings (converted directly to the corresponding bytes)

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
Patch The patch version is incremented when bugs are fixed.
Minor Minor version increments occur when new, but backward compatible, functionality is introduced.
Major The major version must be bumped when backward incompatible changes are introduced. This should rarely (if ever) occur.

Changes

Fri, 09 Sep 2011 11:43:00 -0500 - Jonathan Ellis
 * add int data type

Wed, 07 Sep 2011 09:01:00 -0500 – Jonathan Ellis

  • Updated version to 2.0; Documented row-based count()
  • Updated list of supported data types

Wed, 10 Aug 2011 11:22:00 -0500 – Eric Evans

  • Improved INSERT vs. UPDATE wording.
  • Documented counter column incr/descr.

Sat, 01 Jun 2011 15:58:00 -0600 – Pavel Yaskevich

  • Updated to support ALTER (CASSANDRA-1709)

Tue, 22 Mar 2011 18:10:28 -0700 – Eric Evans <eevans@rackspace.com>

  • Initial version, 1.0.0

18 Responses to Cassandra Query Language (CQL) v2.0 reference

  1. Pingback: Cassandra Query Language AKA CQL syntax « Courtney Robinson 's log

  2. Pingback: Cassandra Query Language (CQL) v1.0.0 (UPDATED) « Courtney Robinson 's log

  3. tamilhere says:

    Hi Dude,
    How do I interpret value stored in a counter column family returned by cql?
    Currently it is like for 14 -> ‘\x00\x00\x00\x00\x00\x00\x00\x0e’ [HexRepresentation]

    Will I be able to change its type to int or long??

    Reason is my PHP Client is not able to recognize this format.

    Regards,
    Tamil

    • Courtney says:

      Counter columns store values as 64 bit integers. What you have is the HEX representation hence the 8 bytes.
      In PHP you’d need to unpack it
      for example

      <?php
       $dec='x00x00x00x00x00x00x00x0e';
       var_dump(unpack("cchars/nint", $dec));
      ?>
      

      Results in :
      array(2) {
      ["chars"]=>
      int(120)
      ["int"]=>
      int(12336)
      }

      More info:
      http://wiki.apache.org/cassandra/Counters
      https://issues.apache.org/jira/browse/CASSANDRA-1072
      https://issues.apache.org/jira/secure/attachment/12459754/Partitionedcountersdesigndoc.pdf
      http://www.datastax.com/docs/0.8/dml/using_cli#creating-a-counter-column-family
      http://php.net/manual/en/function.unpack.php

    • Courtney says:

      Actually I just came accross this page randomly just now:
      http://wiki.apache.org/cassandra/FAQ#a_long_is_exactly_8_bytes

              /**
               * Takes php integer and packs it to 64bit (8 bytes) long big endian binary representation.
               * @param  $x integer
               * @return string eight bytes long binary repersentation of the integer in big endian order.
               */
              public static function pack_longtype($x) {
                      return pack('C8', ($x &gt;&gt; 56) &amp; 0xff, ($x &gt;&gt; 48) &amp; 0xff, ($x &gt;&gt; 40) &amp; 0xff, ($x &gt;&gt; 32) &amp; 0xff,
                                      ($x &gt;&gt; 24) &amp; 0xff, ($x &gt;&gt; 16) &amp; 0xff, ($x &gt;&gt; 8) &amp; 0xff, $x &amp; 0xff);
              }
      
              /**
               * Takes eight bytes long big endian binary representation of an integer and unpacks it to a php integer.
               * @param  $x
               * @return php integer
               */
              public static function unpack_longtype($x) {
                      $a = unpack('C8', $x);
                      return ($a[1] &lt;&lt; 56) + ($a[2] &lt;&lt; 48) + ($a[3] &lt;&lt; 40) + ($a[4] &lt;&lt; 32) + ($a[5] &lt;&lt; 24) + ($a[6] &lt;&lt; 16) + ($a[7] &lt;&lt; 8) + $a[8];
              }
      
  4. Klaus says:

    In the table all the way down, I suppose this is a typo:

    uuid An iso8601 timestamp

    • Courtney says:

      Why do you say that? Maybe its too early in the morning but I’m not seeing why that’d be a typo.

  5. Malls says:

    Hi Courtney,

    First, great job !

    Do you know if it’s possible to make a select on timestamp range, like it is with cassandra_SliceRange in Thrift API ?

    • Courtney says:

      Thanks Malls.
      No you can’t query against Column timestamps. You could however use timestamps for your column names and perform a column slice query on those, if it fits your data model. On the other hand, you could also create a Secondary index on your columns that’ll allow you to do GTE,LTE or Equals comparisons…
      http://www.datastax.com/dev/blog/whats-new-cassandra-07-secondary-indexes may be useful

      • Malls says:

        Thanks Courtney,
        But I have explained badly, In fact I already use the Timestamp as my column name.
        With Thrift SliceRange, I can set the column name to start the slice with, and the column name to stop the slice at.
        I’m trying to do the same thing with CQL, but it’s seems that’s not possible or I don’t understand something (that’s most possible)
        Because if I correctly understand, we can only filter on the KEY and the value of an indexed column, but not on the column name.

        • Malls says:

          Ok, I hadn’t seen the select range
          SELECT range_lo..range_hi FROM …

          • Courtney says:

            Yeah sorry I misunderstood. And yes the select range is what you’re after so select ‘time1′..’time5′ from CF where x=y

  6. Dinesh says:

    hii it’s possible get all column family name using CQL like mysql show tables.

    • Courtney says:

      Not with CQL, use the thrift method describe_keyspaces. See http://wiki.apache.org/cassandra/API#describe_keyspaces

  7. bee says:

    how do i create a counter column family?

    • Courtney says:

      create column family counterCF with default_validation_class=CounterColumnType and replicate_on_write=true;

      http://wiki.apache.org/cassandra/Counters

  8. pravat kumar dash says:

    how i go to cqlsh on ubuntu.please help me

    • zcourts says:

      You need to download and install Apache Cassandra. Cqlsh is in the bin folder

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,385 other followers

%d bloggers like this: