Sie sind auf Seite 1von 2

CQL Quick Reference

CQL is a query language for Apache Cassandra, similar to SQL, that makes application development a snap. A growing ecosystem of drivers makes access from many languages/frameworks possible, but for quick tests and exploration, the interactive command interpreter that ships with the Python driver is a great place to start.
$ easy_install cql ... $ cqlsh [-C/--color] localhost 9160

Creating a new keyspace that uses SimpleStrategy and three replicas


CREATE KEYSPACE application WITH strategy_class = SimpleStrategy AND strategy_options:replication_factor = 3;

Sessions are bound to a keyspace, to operate on one you must "USE" it rst
USE application;

Creating a new column family with text (utf8) key and column values
CREATE COLUMNFAMILY users (username text PRIMARY KEY, firstname text, middle text, surname text, birthdate text);

Inserting (or updating) a record


INSERT INTO users (username, firstname, surname) VALUES (cdickens, Charles, Dickens);

Reading columns from a row


SELECT firstname, surname FROM users WHERE username = cdickens;

Deleting columns and rows


DELETE middle FROM users WHERE username = cdickens; DELETE FROM users WHERE username = smeyer;

Executing batch operations


BEGIN BATCH USING CONSISTENCY ALL INSERT INTO users (username, surname) VALUES (dadams, Adams); INSERT INTO users (username, surname) VALUES (mshelley, Mary); UPDATE users SET birthdate = -139518000000 WHERE username = jrowling; DELETE FROM users WHERE username = sking; DELETE FROM users WHERE username = dbrown; APPLY BATCH

Modifying column family schema


ALTER COLUMNFAMILY users ADD birthplace text; ALTER COLUMNFAMILY users ALTER birthdate TYPE int; ALTER COLUMNFAMILY users DROP middle;

Creating an index
CREATE INDEX birthplace ON users (birthplace);

Specifying consistency level


SELECT surname, birthdate FROM users USING CONSISTENCY QUORUM WHERE birthplace = 'Portsmouth'; Specifying column timestamps. UPDATE users USING TIMESTAMP 1319129012000 SET birthdate = -4982839764 WHERE username = cdickens;

Specifying column TTL values


UPDATE users USING TTL 86400 SET us_tax_lien = 1 WHERE username = cdickens;

Select a range of column values


SELECT '2011-10-23 00:00'..'2011-10-23 23:59' FROM sessions WHERE username = cdickens;

Select the rst ten values from a range of columns


SELECT FIRST 10 '2011-10-23 00:00'..'2011-10-23 23:59' FROM sessions WHERE username = cdickens;

Select the last ten values in reverse order, from a range of columns
SELECT FIRST 10 REVERSED '2011-10-23 23:59'..'2011-10-23 00:00' FROM sessions WHERE username = cdickens;

Select the rst 10 rows from a range of keys


SELECT surname FROM users WHERE username > eabbot AND username < teliot LIMIT 10

CQL Datatypes
ascii bigint blob boolean counter decimal double US ASCII encoded string 8-byte long Arbitrary bytes (hex encoded) One of true or false Counter column (an 8-byte long) Decimal value of arbitrary precision 8-byte oating point oat int text timestamp uuid varchar varint 4-byte oating point 4-byte integer UTF-8 encoded string Date and time encoded as an 8-byte long Type 1, or type 4 UUID UTF-8 endoded string Integer value of arbitrary precision

CREATE KEYSPACE keyword arguments


argument strategy_class strategy_options required yes no Name of the replication strategy class. Additional arguments for the strategy. For example, to specify the replication factor for SimpleStrategy: strategy_options:replication_factor = N description

CREATE COLUMNFAMILY keyword options


option comparator comment read_repair_chance default_validation replicate_on_write text none 0.1 text TRUE default description Controls sorting and validation of column names. See "CQL Data Types" for valid arguments Free-form human-readable description The probability that read repairs will be invoked for non-quorum reads Determines validation of column values. See "CQL Data Types" for valid arguments Force immediate replication of counter columns when writing

Acunu Reflex: the high performance database powered by Apache Cassandra

www.acunu.com
Apache Cassandra is a trademark of the Apache Software Foundation

Das könnte Ihnen auch gefallen