blob: a29227929a291e9a3da77c94c822c96bef0eb8b7 [file] [log] [blame]
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body><h1 id="CassandraQueryLanguageCQLv1.0.0">Cassandra Query Language (CQL) v1.0.0</h1><h2 id="TableofContents">Table of Contents</h2><ol style="list-style: none;"><li><a href="#CassandraQueryLanguageCQLv1.0.0">Cassandra Query Language (CQL) v1.0.0</a><ol style="list-style: none;"><li><a href="#TableofContents">Table of Contents</a></li><li><a href="#USE">USE</a></li><li><a href="#SELECT">SELECT</a><ol style="list-style: none;"><li><a href="#SpecifyingColumns">Specifying Columns</a></li><li><a href="#ColumnFamily">Column Family</a></li><li><a href="#ConsistencyLevel">Consistency Level</a></li><li><a href="#Filteringrows">Filtering rows</a></li><li><a href="#Limits">Limits</a></li></ol></li><li><a href="#UPDATE">UPDATE</a><ol style="list-style: none;"><li><a href="#ColumnFamily2">Column Family</a></li><li><a href="#ConsistencyLevel2">Consistency Level</a></li><li><a href="#SpecifyingColumnsandRow">Specifying Columns and Row</a></li></ol></li><li><a href="#DELETE">DELETE</a><ol style="list-style: none;"><li><a href="#SpecifyingColumns2">Specifying Columns</a></li><li><a href="#ColumnFamily3">Column Family</a></li><li><a href="#ConsistencyLevel3">Consistency Level</a></li><li><a href="#deleterows">Specifying Rows</a></li></ol></li><li><a href="#TRUNCATE">TRUNCATE</a></li><li><a href="#CREATEKEYSPACE">CREATE KEYSPACE</a></li><li><a href="#CREATECOLUMNFAMILY">CREATE COLUMNFAMILY</a><ol style="list-style: none;"><li><a href="#keytypes">Specifying Key Type</a></li><li><a href="#columntypes">Specifying Column Type (optional)</a></li><li><a href="#ColumnFamilyOptionsoptional">Column Family Options (optional)</a></li></ol></li><li><a href="#CREATEINDEX">CREATE INDEX</a></li><li><a href="#DROP">DROP</a></li><li><a href="#CommonIdioms">Common Idioms</a><ol style="list-style: none;"><li><a href="#consistency">Specifying Consistency</a></li><li><a href="#terms">Term specification</a></li></ol></li></ol></li><li><a href="#Versioning">Versioning</a></li><li><a href="#Changes">Changes</a></li></ol><h2 id="USE">USE</h2><p><i>Synopsis:</i></p><pre><code>USE &lt;KEYSPACE&gt;;
</code></pre><p>A <code>USE</code> statement consists of the <code>USE</code> 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.</p><h2 id="SELECT">SELECT</h2><p><i>Synopsis:</i></p><pre><code>SELECT [FIRST N] [REVERSED] &lt;SELECT EXPR&gt; FROM &lt;COLUMN FAMILY&gt; [USING &lt;CONSISTENCY&gt;]
[WHERE &lt;CLAUSE&gt;] [LIMIT N];
</code></pre><p>A <code>SELECT</code> 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.</p><h3 id="SpecifyingColumns">Specifying Columns</h3><pre><code>SELECT [FIRST N] [REVERSED] name1, name2, name3 FROM ...
SELECT [FIRST N] [REVERSED] name1..nameN FROM ...
</code></pre><p>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 (<code>..</code>). The set of columns returned for a range is start and end inclusive.</p><p>The <code>FIRST</code> 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.</p><p>The <code>REVERSED</code> option causes the sort order of the results to be reversed.</p><p>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.</p><h3 id="ColumnFamily">Column Family</h3><pre><code>SELECT ... FROM &lt;COLUMN FAMILY&gt; ...
</code></pre><p>The <code>FROM</code> clause is used to specify the Cassandra column family applicable to a <code>SELECT</code> query.</p><h3 id="ConsistencyLevel">Consistency Level</h3><pre><code>SELECT ... [USING &lt;CONSISTENCY&gt;] ...
</code></pre><p>Following the column family clause is an optional <a href="#consistency">consistency level specification</a>.</p><h3 id="Filteringrows">Filtering rows</h3><pre><code>SELECT ... WHERE KEY = keyname AND name1 = value1
SELECT ... WHERE KEY &gt;= startkey and KEY =&lt; endkey AND name1 = value1
</code></pre><p>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 <code>KEY</code> keyword, a relational operator, (one of <code>=</code>, <code>&gt;</code>, <code>&gt;=</code>, <code>&lt;</code>, and <code>&lt;=</code>), 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 <i>on</i>.</p><p><i>Note: The greater-than and less-than operators (<code>&gt;</code> and <code>&lt;</code>) result in key ranges that are inclusive of the terms. There is no supported notion of &#8220;strictly&#8221; greater-than or less-than; these operators are merely supported as aliases to <code>&gt;=</code> and <code>&lt;=</code>.</i> </p><h3 id="Limits">Limits</h3><pre><code>SELECT ... WHERE &lt;CLAUSE&gt; [LIMIT N] ...
</code></pre><p>Limiting the number of rows returned can be achieved by adding the <code>LIMIT</code> option to a <code>SELECT</code> expression. <code>LIMIT</code> defaults to 10,000 when left unset.</p><h2 id="UPDATE">UPDATE</h2><p><em>Synopsis:</em></p><pre><code>UPDATE &lt;COLUMN FAMILY&gt; [USING CONSISTENCY &lt;CL&gt;]
SET name1 = value1, name2 = value2 WHERE KEY = keyname;
</code></pre><p>An <code>UPDATE</code> is used to write one or more columns to a record in a Cassandra column family. No results are returned.</p><h3 id="ColumnFamily2">Column Family</h3><pre><code>UPDATE &lt;COLUMN FAMILY&gt; ...
</code></pre><p>Statements begin with the <code>UPDATE</code> keyword followed by a Cassandra column family name.</p><h3 id="ConsistencyLevel2">Consistency Level</h3><pre><code>UPDATE ... [USING &lt;CONSISTENCY&gt;] ...
</code></pre><p>Following the column family identifier is an optional <a href="#consistency">consistency level specification</a>.</p><h3 id="SpecifyingColumnsandRow">Specifying Columns and Row</h3><pre><code>UPDATE ... SET name1 = value1, name2 = value2 WHERE KEY = keyname;
</code></pre><p>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 <code>KEY</code> keyword.</p><p>Additionally, it is also possible to send multiple UPDATES to a node at once using a batch syntax:</p><pre><code>BEGIN BATCH [USING &lt;CONSISTENCY&gt;]
UPDATE CF1 SET name1 = value1, name2 = value2 WHERE KEY = keyname1;
UPDATE CF1 SET name3 = value3 WHERE KEY = keyname2;
UPDATE CF2 SET name4 = value4, name5 = value5 WHERE KEY = keyname3;
APPLY BATCH
</code></pre><p>When batching UPDATEs, a single consistency level is used for the entire batch, it appears after the <code>BEGIN BATCH</code> statement, and uses the standard <a href="#consistency">consistency level specification</a>. Batch UPDATEs default to <code>CONSISTENCY.ONE</code> when left unspecified.</p><p><em>NOTE: While there are no isolation guarantees, <code>UPDATE</code> queries are atomic within a give record.</em></p><h2 id="DELETE">DELETE</h2><p><em>Synopsis:</em></p><pre><code>DELETE [COLUMNS] FROM &lt;COLUMN FAMILY&gt; [USING &lt;CONSISTENCY&gt;] WHERE KEY = keyname1
DELETE [COLUMNS] FROM &lt;COLUMN FAMILY&gt; [USING &lt;CONSISTENCY&gt;] WHERE KEY IN (keyname1, keyname2);
</code></pre><p>A <code>DELETE</code> is used to perform the removal of one or more columns from one or more rows.</p><h3 id="SpecifyingColumns2">Specifying Columns</h3><pre><code>DELETE [COLUMNS] ...
</code></pre><p>Following the <code>DELETE</code> 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 <a href="#deleterows">WHERE clause</a></p><h3 id="ColumnFamily3">Column Family</h3><pre><code>DELETE ... FROM &lt;COLUMN FAMILY&gt; ...
</code></pre><p>The column family name follows the list of column names.</p><h3 id="ConsistencyLevel3">Consistency Level</h3><pre><code>UPDATE ... [USING &lt;CONSISTENCY&gt;] ...
</code></pre><p>Following the column family identifier is an optional <a href="#consistency">consistency level specification</a>.</p><h3 id="deleterows">Specifying Rows</h3><pre><code>UPDATE ... WHERE KEY = keyname1
UPDATE ... WHERE KEY IN (keyname1, keyname2)
</code></pre><p>The <code>WHERE</code> clause is used to determine which row(s) a <code>DELETE</code> applies to. The first form allows the specification of a single keyname using the <code>KEY</code> keyword and the <code>=</code> operator. The second form allows a list of keyname terms to be specified using the <code>IN</code> notation and a parenthesized list of comma-delimited keyname terms.</p><h2 id="TRUNCATE">TRUNCATE</h2><p><em>Synopsis:</em></p><pre><code>TRUNCATE &lt;COLUMN FAMILY&gt;
</code></pre><p>Accepts a single argument for the column family name, and permanently removes all data from said column family.</p><h2 id="CREATEKEYSPACE">CREATE KEYSPACE</h2><p><em>Synopsis:</em></p><pre><code>CREATE KEYSPACE &lt;NAME&gt; WITH replication_factor = &lt;NUM&gt; AND strategy_class = &lt;STRATEGY&gt;
[AND strategy_options.&lt;OPTION&gt; = &lt;VALUE&gt; [AND strategy_options.&lt;OPTION&gt; = &lt;VALUE&gt;]];
</code></pre><p>The <code>CREATE KEYSPACE</code> statement creates a new top-level namespace (aka &#8220;keyspace&#8221;). 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:</p><table><tr><th>keyword</th><th>required</th><th>description</th></tr><tr><td>replication_factor</td><td>yes</td><td>Numeric argument that specifies the number of replicas for this keyspace.</td></tr><tr><td>strategy_class</td><td>yes</td><td>Class name to use for managing replica placement. Any of the shipped strategies can be used by specifying the class name relative to org.apache.cassandra.locator, others will need to be fully-qualified and located on the classpath.</td></tr><tr><td>strategy_options</td><td>no</td><td>Some strategies require additional arguments which can be supplied by appending the option name to the <code>strategy_options</code> keyword, separated by a colon (<code>:</code>). For example, a strategy option of &#8220;DC1&#8221; with a value of &#8220;1&#8221; would be specified as <code>strategy_options:DC1 = 1</code>.</td></tr></table><h2 id="CREATECOLUMNFAMILY">CREATE COLUMNFAMILY</h2><p><em>Synopsis:</em></p><pre><code>CREATE COLUMNFAMILY &lt;COLUMN FAMILY&gt; (KEY &lt;type&gt; PRIMARY KEY [, name1 type, name2 type, ...]);
CREATE COLUMNFAMILY &lt;COLUMN FAMILY&gt; (KEY &lt;type&gt; PRIMARY KEY [, name1 type, name2 type, ...])
[WITH keyword1 = arg1 [AND keyword2 = arg2 [AND ...]]];
</code></pre><p><code>CREATE COLUMNFAMILY</code> 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.</p><h3 id="keytypes">Specifying Key Type</h3><pre><code>CREATE ... (KEY &lt;type&gt; PRIMARY KEY) ...
</code></pre><p>When creating a new column family, you must specify key type. The list of possible key types is identical to column comparators/validators, (see <a href="columntypes">Specifying Column Type</a>). It&#8217;s important to note that the key type must be compatible with the partitioner in use, for example <code>OrderPreservingPartitioner</code> and <code>CollatingOrderPreservingPartitioner</code> both require UTF-8 keys.</p><h3 id="columntypes">Specifying Column Type (optional)</h3><pre><code>CREATE ... (KEY &lt;type&gt; PRIMARY KEY, name1 type, name2 type) ...
</code></pre><p>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:</p><table><tr><th>type</th><th>description</th></tr><tr><td>bytea</td><td>Arbitrary bytes (no validation)</td></tr><tr><td>ascii</td><td>ASCII character string</td></tr><tr><td>text</td><td>UTF8 encoded string</td></tr><tr><td>varchar</td><td>UTF8 encoded string</td></tr><tr><td>uuid</td><td>Type 1, or type 4 UUID</td></tr><tr><td>varint</td><td>4-byte integer</td></tr><tr><td>bigint</td><td>8-byte long</td></tr></table><p><em>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 <code>AbstractType</code>), either fully qualified, or relative to the <code>org.apache.cassandra.db.marshal</code> package.</em></p><h3 id="ColumnFamilyOptionsoptional">Column Family Options (optional)</h3><pre><code>CREATE COLUMNFAMILY ... WITH keyword1 = arg1 AND keyword2 = arg2;
</code></pre><p>A number of optional keyword arguments can be supplied to control the configuration of a new column family.</p><table><tr><th>keyword</th><th>default</th><th>description</th></tr><tr><td>comparator</td><td>text</td><td>Determines sorting and validation of column names. Valid values are identical to the types listed in <a href="#columntypes">Specifying Column Type</a> above.</td></tr><tr><td>comment</td><td>none</td><td>A free-form, human-readable comment.</td></tr><tr><td>row_cache_size</td><td>0</td><td>Number of rows whose entire contents to cache in memory.</td></tr><tr><td>key_cache_size</td><td>200000</td><td>Number of keys per SSTable whose locations are kept in memory in &#8220;mostly LRU&#8221; order.</td></tr><tr><td>read_repair_chance</td><td>1.0</td><td>The probability with which read repairs should be invoked on non-quorum reads.</td></tr><tr><td>gc_grace_seconds</td><td>864000</td><td>Time to wait before garbage collecting tombstones (deletion markers).</td></tr><tr><td>default_validation</td><td>text</td><td>Determines validation of column values. Valid values are identical to the types listed in <a href="#columntypes">Specifying Column Type</a> above.</td></tr><tr><td>min_compaction_threshold</td><td>4</td><td>Minimum number of SSTables needed to start a minor compaction.</td></tr><tr><td>max_compaction_threshold</td><td>32</td><td>Maximum number of SSTables allowed before a minor compaction is forced.</td></tr><tr><td>row_cache_save_period_in_seconds</td><td>0</td><td>Number of seconds between saving row caches.</td></tr><tr><td>key_cache_save_period_in_seconds</td><td>14400</td><td>Number of seconds between saving key caches.</td></tr><tr><td>memtable_flush_after_mins</td><td>60</td><td>Maximum time to leave a dirty table unflushed.</td></tr><tr><td>memtable_throughput_in_mb</td><td>dynamic</td><td>Maximum size of the memtable before it is flushed.</td></tr><tr><td>memtable_operations_in_millions</td><td>dynamic</td><td>Number of operations in millions before the memtable is flushed.</td></tr><tr><td>replicate_on_write</td><td>false</td><td></td></tr></table><h2 id="CREATEINDEX">CREATE INDEX</h2><p><em>Synopsis:</em></p><pre><code>CREATE INDEX [index_name] ON &lt;column_family&gt; (column_name);
</code></pre><p>A <code>CREATE INDEX</code> statement is used to create a new, automatic secondary index for the named column.</p><h2 id="DROP">DROP</h2><p><em>Synopsis:</em></p><pre><code>DROP &lt;KEYSPACE|COLUMNFAMILY&gt; namespace;
</code></pre><p><code>DROP</code> statements result in the immediate, irreversible removal of keyspace and column family namespaces.</p><h2 id="CommonIdioms">Common Idioms</h2><h3 id="consistency">Specifying Consistency</h3><pre><code>... USING &lt;CONSISTENCY&gt; ...
</code></pre><p>Consistency level specifications are made up the keyword <code>USING</code>, followed by a consistency level identifier. Valid consistency levels are as follows:</p><ul><li><code>CONSISTENCY ZERO</code></li><li><code>CONSISTENCY ONE</code> (default)</li><li><code>CONSISTENCY QUORUM</code></li><li><code>CONSISTENCY ALL</code></li><li><code>CONSISTENCY DCQUORUM</code></li><li><code>CONSISTENCY DCQUORUMSYNC</code></li></ul><h3 id="terms">Term specification</h3><p>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:</p><ul><li>Any single quoted string literal (example: <code>'apple'</code>).</li><li>Unquoted alpha-numeric strings that begin with a letter (example: <code>carrot</code>).</li><li>Unquoted numeric literals (example: <code>100</code>).</li><li>UUID strings in hyphen-delimited hex notation (example: <code>1438fc5c-4ff6-11e0-b97f-0026c650d722</code>). </li></ul><p>Terms which do not conform to these rules result in an exception.</p><p>How column name/value terms are interpreted is determined by the configured type.</p><table><tr><th>type</th><th>term</th></tr><tr><td>ascii</td><td>Any string which can be decoded using ASCII charset</td></tr><tr><td>text / varchar</td><td>Any string which can be decoded using UTF8 charset</td></tr><tr><td>uuid</td><td>Standard UUID string format (hyphen-delimited hex notation)</td></tr><tr><td>uuid</td><td>Standard UUID string format (hyphen-delimited hex notation)</td></tr><tr><td>uuid</td><td>The string <code>now</code>, to represent a type-1 (time-based) UUID with a date-time component based on the current time</td></tr><tr><td>uuid</td><td>Numeric value representing milliseconds since epoch</td></tr><tr><td>uuid</td><td>An <a href="http://en.wikipedia.org/wiki/8601">iso8601 timestamp</a></td></tr><tr><td>bigint</td><td>Numeric value capable of fitting in 8 bytes</td></tr><tr><td>varint</td><td>Numeric value of arbitrary size</td></tr><tr><td>bytea</td><td>Hex-encoded strings (converted directly to the corresponding bytes)</td></tr></table><h1 id="Versioning">Versioning</h1><p>Versioning of the CQL language adheres to the <a href="http://semver.org">Semantic Versioning</a> 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.</p><table><tr><th>version</th><th>description</th></tr><tr><td>Patch</td><td>The patch version is incremented when bugs are fixed.</td></tr><tr><td>Minor</td><td>Minor version increments occur when new, but backward compatible, functionality is introduced.</td></tr><tr><td>Major</td><td>The major version <em>must</em> be bumped when backward incompatible changes are introduced. This should rarely (if ever) occur.</td></tr></table><h1 id="Changes">Changes</h1><pre>Tue, 22 Mar 2011 18:10:28 -0700 - Eric Evans &lt;eevans@rackspace.com&gt;
* Initial version, 1.0.0
</pre></body></html>