140 likes | 323 Views
CQL – Cassandra Query Language. Courtney Robinson – crlog.info @ Eric Evans (Python tests and CQL Spec). Thrift or Avro?. External dependency Community activity dissipates Too generic Many user reported problems caused by Thrift or the misuse thereof. Its thrift! Its Avro! Its CQL.
E N D
CQL – Cassandra Query Language Courtney Robinson – crlog.info@ Eric Evans (Python tests and CQL Spec)
Thrift or Avro? • External dependency • Community activity dissipates • Too generic • Many user reported problems caused by Thrift or the misuse thereof... Its thrift! Its Avro! Its CQL....
What is it? • Effectively a structured query language • Replacement for clients? Not really... • Attempting to push as much server-side as possible • Familiar syntax • User friendly API for Cassandra new comers
CQL V1.0.0 - Keywords That’s right!No INSERT...? • USE • SELECT • UPDATE • DELETE • TRUNCATE • DROP • BATCH • CREATE KEYSPACE • CREATE COLUMNFAMILY • CREATE INDEX Special statements
Specifying Consistency • ... USING <CONSISTENCY> ... • Made up of the keyword USING, followed by a consistency level identifier. • Valid consistency levels are: • CONSISTENCY ZERO • CONSISTENCY ONE (default) • CONSISTENCY QUORUM • CONSISTENCY ALL • CONSISTENCY DCQUORUM • CONSISTENCY DCQUORUMSYNC • E.gBEGIN BATCH USING CONSISTENCY ONE
Create keyspce • CREATE KEYSPACE WITH replication_factor = AND strategy_class = [AND strategy_options. = [AND strategy_options. = ]]; • E.gCREATE KEYSPACE TestKeyspaceWITH strategy_options:DC1 = '1‘ AND strategy_class = 'NetworkTopologyStrategy‘ • CREATE COLUMNFAMILY [(name1 type, name2 type, ...)] [WITH keyword1 = arg1 [AND keyword2 = arg2 [AND ...]]]; Set column type: • CREATE COLUMNFAMILY (name1 type, name2 type) ...; • CREATE INDEX [index_name] ON <column_family> (column_name); • Used to create a new, automatic secondary index for the named column. Create Column Family Create INDEX
USE • USE <KEYSPACE>; • Use keyword followed by a valid Keyspace name • Set working Keyspace per-connection • DROP <KEYSPACE|COLUMNFAMILY>; • DROP KEYSPACE KSName; • DROP COLUMNFAMILYCFName; Immediate, irreversible removal of keyspace and column family namespaces. • TRUNCATE <COLUMN FAMILY> • Accepts a single argument (CF) name • permanently removes all data from said column family. DROP TRUNCATE
UPDATE (values) • UPDATE [USING CONSISTENCY ] SET name1 = value1, name2 = value2 WHERE KEY = keyname; • UPDATE is used to write one or more columns to a record in a Cassandra column family. • No results are returned. • Creates or updates rows • UPDATE CFName SET ‘name' = 10 WHERE KEY = ‘1234’ • UPDATE <COLUMN FAMILY> ... • Begin with the UPDATE keyword followed by CF name. • Followed by an optional consistency level specification.
BATCH UPDATES • Where’s my batch mutate gone? • Similar to code blocks/structs • Has begining (Begin Batch) and end (Apply Batch) BEGIN BATCH [USING ] 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 • BEGIN BATCH USING CONSISTENCY QUORUM UPDATE CFnameSET 1='1', 2='2', 3='3', 4='4' WHERE KEY='aa‘ UPDATE CFname2 SET 5='5', 6='6', 7='8', 9='9' WHERE KEY='ab' UPDATE CFnameSET 9='9', 8='8', 7='7', 6='6' WHERE KEY='ac' UPDATE CFname3 SET 5='5', 4='4', 3='3', 2='2' WHERE KEY='ad' UPDATE CFnameSET 1='1', 2='2', 3='3', 4='4' WHERE KEY='ae' • APPLY BATCH
DELETE • DELETE [COLUMNS] FROM [USING ] WHERE KEY = keyname1 • DELETE [COLUMNS] FROM [USING ] WHERE KEY IN (keyname1, keyname2); • Remove one or more columns from one or more rows. • Optional comma-delimited list of column names following DELETE • Removes entire row(s) if no columns are specified • DELETE ‘col1', 'col2' FROM CF WHERE KEY = 'key‘ • DELETE ‘col1', 'col2' FROM CF WHERE KEY = ('key‘, ‘key2’, ‘key3’)
SELECT • SELECT [FIRST N] [REVERSED] <SELECT EXPR> FROM <COLUMN FAMILY> [USING <CONSISTENCY>] [WHERE <CLAUSE>] [LIMIT N]; • FIRST = number of columns, N • REVERSED = Reverses sort order • Return a result set, key + columns per row Specify columns: • SELECT [FIRST N] [REVERSED] name1, name2, name3 FROM ... Request a range of columns: • SELECT [FIRST N] [REVERSED] name1..nameN FROM ... • “..” Notation specifies range • Inclusive of start and end columns • E.gSELECT FIRST 2 REVERSED 3..1 FROM CF WHERE KEY = 'aa'
Courtney Robinson @zcourts Thank you for listening. Questions?Links: http://crlog.info/2011/03/29/cassandra-query-language-aka-cql-syntax/ https://svn.apache.org/viewvc/cassandra/trunk/doc/cql/CQL.html?view=co https://issues.apache.org/jira/browse/CASSANDRA-1703