cassandra 2.1 boot camp, protocol, queries, cql
DESCRIPTION
Cassandra Summit Boot Camp, 2014 Protocol, Queries, and Cell Names Tyler Hobbs presenterTRANSCRIPT
![Page 1: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/1.jpg)
©2014 DataStax
Tyler Hobbs
Protocol, Queries, and Cell Names
1
[email protected] @tylhobbs
Friday, September 12, 14
![Page 2: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/2.jpg)
Native Protocol•Driver <=> Cassandra Communications•doc/native_protocol_v3.spec
Friday, September 12, 14
![Page 3: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/3.jpg)
Native Protocol•Handshake•Set protocol version, compression, auth, etc
Friday, September 12, 14
![Page 4: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/4.jpg)
Native Protocol•Multiplexed operations•Unique request ID per-operation per-connection•Responses can come out of order•Pushed notifications from the server
driver c*123
213
Friday, September 12, 14
![Page 5: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/5.jpg)
Native Protocol•PREPARE•EXECUTE•QUERY•BATCH
Friday, September 12, 14
![Page 6: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/6.jpg)
Statement Preparation•Driver sends PREPARE with query•C* replies with:• Statement ID• Query parameter metadata• Result set metadata
•Driver prepares against all nodes it will use
Friday, September 12, 14
![Page 7: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/7.jpg)
Lexing and Parsing•ANTLR•Cql.g is the grammar•Minimal checking of statement validity•Creates “Raw” statements and terms• No type checking yet
Friday, September 12, 14
![Page 8: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/8.jpg)
Raw -> Prepared•Term.Raw -> Term.(Non)Terminal• Terminal is fully processed, nothing to do after preparing
• String literals, int literals, etc• Pure function calls (e.g. intAsBlob(42))
• NonTerminal has bind markers or non-pure function calls• Example: now()
•Type checking performed where possible•Term.Raw.prepare()
Friday, September 12, 14
![Page 9: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/9.jpg)
Raw -> Prepared•Statement.Raw -> ParsedStatement.Prepared• Is the query valid and supported?• Organize restrictions on partitioning, clustering and
normal columns• Check validity of restrictions, secondary indexes,
ordering, filtering, etc.• SelectStatement.Raw.prepare()
Friday, September 12, 14
![Page 10: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/10.jpg)
Raw -> Prepared•ParsedStatement.Prepared is cached, ID returned
to driver•QueryProcessor
Friday, September 12, 14
![Page 11: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/11.jpg)
Statement Execution•EXECUTE with ID and binary parameters
Friday, September 12, 14
![Page 12: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/12.jpg)
Statement Execution•Check permissions• QueryProcessor.processStatement -> Statement.checkAccess()
•Check Statement Validity•Bind parameters• Term.NonTerminal -> Term.Terminal• Evaluate functions• Check parameter types
Friday, September 12, 14
![Page 13: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/13.jpg)
Statement Execution•Build slice ends for reads• Pick start, end based on reversal• Are we getting multiple slices?• SelectStatement.makeFilter()
Friday, September 12, 14
![Page 14: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/14.jpg)
Statement Execution•Set up paging• Use last seen primary key as starting point• Paging state held by driver
Friday, September 12, 14
![Page 15: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/15.jpg)
Statement Execution•Pass command to StorageProxy• StorageProxy.read(), mutate(), cas()
• Dispatched to replicas• Low-level cells returned
Friday, September 12, 14
![Page 16: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/16.jpg)
Statement Execution•Process Results• Order columns correctly• Filter empty rows• Order rows• Trim excess rows to match LIMIT• Aggregate (count(), for now)
Friday, September 12, 14
![Page 17: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/17.jpg)
Statement Classes•SchemaAlteringStatement• CREATE/ALTER/DROP KEYSPACE, etc
•ModificationStatement• INSERT, UPDATE, DELETE
•SelectStatement
Friday, September 12, 14
![Page 18: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/18.jpg)
SchemaAlteringStatement•Update local schema•Broadcast to all live nodes•Notify drivers with pushed notification
Friday, September 12, 14
![Page 19: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/19.jpg)
ModificationStatement•When doing LWT/CAS• Prepare -> Read -> Propose -> Commit• Build slice ends for read, execute read• Check conditions against read row• If CAS fails, return existing row as result
Friday, September 12, 14
![Page 20: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/20.jpg)
Cells•Subject to change for 3.0•Name/value pair
• Sorted by name
Friday, September 12, 14
![Page 21: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/21.jpg)
CellsCREATE TABLE versioned_docs (id uuid,
version timeuuid,
fieldname text,
fieldvalue blob,
PRIMARY KEY (id, version, fieldname))
•id is the partitioning key•version and fieldname are clustering columns•Within a partition, rows are ordered by clustering
columns
Friday, September 12, 14
![Page 22: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/22.jpg)
Sparse Format•CQL3 Format•Cell name format:
• (clustering_1, ..., clustering_n, column_name)
Friday, September 12, 14
![Page 23: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/23.jpg)
Sparse FormatCREATE TABLE versioned_docs (id uuid,
version timeuuid,
fieldname text,
fieldvalue blob,
PRIMARY KEY (id, version, fieldname))
Friday, September 12, 14
![Page 24: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/24.jpg)
Sparse FormatCREATE TABLE versioned_docs (id uuid,
version timeuuid,
fieldname text,
fieldvalue blob,
PRIMARY KEY (id, version, fieldname))
INSERT INTO ... VALUES( D5B...0A5, 3AF4...D93, ‘title’, ‘Monthly Report’)
(3AF4..D93, 'title', 'fieldvalue')
Friday, September 12, 14
![Page 25: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/25.jpg)
Sparse Format•Add one extra component for collections:
• (clustering_1, ..., column_name, collection_element)
Friday, September 12, 14
![Page 26: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/26.jpg)
Sparse FormatCREATE TABLE versioned_docs (id uuid,
version timeuuid,
fields map<text, blob>,
PRIMARY KEY (id, version))
Friday, September 12, 14
![Page 27: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/27.jpg)
Sparse FormatCREATE TABLE versioned_docs (id uuid,
version timeuuid,
fields map<text, blob>,
PRIMARY KEY (id, version))
INSERT INTO ... VALUES ( D5B...0A5, 3AF4...D93, {‘title’: ‘Monthly Report’})
(3AF4..D93, 'fields', 'title')
Friday, September 12, 14
![Page 28: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/28.jpg)
Sparse Format• If there are no clustering columns, the cell name
is just the column name
CREATE TABLE users ( id uuid PRIMARY KEY, name text, age int)
•Cell names:(‘name’) and (‘age’)
Friday, September 12, 14
![Page 29: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/29.jpg)
Sparse Format• If COMPACT STORAGE is used, we drop the
composite
CREATE TABLE users ( id uuid PRIMARY KEY, name text, age int)WITH COMPACT STORAGE
•Cell names: ‘name’ and ‘age’
Friday, September 12, 14
![Page 30: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/30.jpg)
Dense Format•Used for COMPACT STORAGE tables with
clustering columns
CREATE TABLE sensor_readings ( id uuid, time timestamp, value float, PRIMARY KEY (id, time))WITH COMPACT STORAGE
Friday, September 12, 14
![Page 31: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/31.jpg)
Dense Format•Don’t store column name in cell name
CREATE TABLE sensor_readings ( id uuid, time timestamp, value float, PRIMARY KEY (id, time))WITH COMPACT STORAGE
INSERT INTO ... VALUES ( D5B...0A5, 1410279227535, 0.64)
1410279227535
Friday, September 12, 14
![Page 32: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/32.jpg)
Dense Format•Multiple clustering keys result in composite cell
nameCREATE TABLE sensor_readings ( id uuid, time timestamp,
attribute text, value float, PRIMARY KEY (id, time, attribute))WITH COMPACT STORAGE
•Cell name: (1410279227535, ‘temperature’)
Friday, September 12, 14
![Page 33: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/33.jpg)
Formats•Sparse Composite
• CQL3 tables
•Sparse Simple• “Static” compact storage, no clustering columns• e.g. “users” table
•Dense Simple• compact storage w/ one clustering column• e.g. sensor readings
•Dense Composite• compact storage w/ multiple clustering columns• e.g. sensor readings w/ multiple fields
Friday, September 12, 14
![Page 34: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/34.jpg)
Internal Classes• CellName and CellNameType
• Sparse/Dense
• Simple/Composite
Friday, September 12, 14
![Page 35: Cassandra 2.1 boot camp, Protocol, Queries, CQL](https://reader031.vdocument.in/reader031/viewer/2022020718/5590f0241a28abe2378b45ff/html5/thumbnails/35.jpg)
Questions?
@tylhobbs
Friday, September 12, 14