From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Cassandra EU 2012 - CQL: Then, Now and When by Eric Evans
1. CQL: Then, Now, and
When
Eric Evans
eric@acunu.com
Cassandra Europe
March 28, 2012
Wednesday, March 28, 12
2. Cassandra Query Language
(aka CQL)
• Query language for Apache Cassandra
• SQL for the most part
• An alternative query interface
• Available since Cassandra 0.8.0
Wednesday, March 28, 12
6. Not a troll at all,
actually.
Wednesday, March 28, 12
7. Status Quo
• RPC-based query interface
• Low level; very little abstraction
• Implemented in Thrift
• Compact binary serialization
• Loads of supported languages
• Generated language code
Wednesday, March 28, 12
8. Unstable
$@#*!
Relax
NOT AGAIN!
It’ll take you 5 minutes to
update your code.
Wednesday, March 28, 12
10. This cannot be unseen!
// Your column
Column col = new Column(ByteBuffer.wrap(“name”.getBytes()));
col.setValue(ByteBuffer.wrap(“value”.getBytes()));
col.setTimestamp(System.currentTimeMillis());
// Don’t ask
ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
cosc.setColumn(cosc);
// Hang on, here we go...
Mutation mutation = new Mutation();
mutation.setColumnOrSuperColumn(cosc);
List<Mutation> mutations = new ArrayList<Mutation>();
mutations.add(mutation);
Map mutations_map = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
Map cf_map = new Map<String, List<Mutation>>();
cf_map.set(“Standard1”, mutations);
mutations_map.put(ByteBuffer.wrap(“key”.getBytes()), cf_map);
cassandra.batch_mutate(mutations_map, consistency_level);
Wednesday, March 28, 12
11. This cannot be unseen!
// Your column
Column col = new Column(ByteBuffer.wrap(“name”.getBytes()));
col.setValue(ByteBuffer.wrap(“value”.getBytes()));
col.setTimestamp(System.currentTimeMillis());
// Don’t ask
ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
cosc.setColumn(cosc);
// Hang on, here we go...
Mutation mutation = new Mutation();
mutation.setColumnOrSuperColumn(cosc);
List<Mutation> mutations = new ArrayList<Mutation>();
mutations.add(mutation);
Map mutations_map = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
Map cf_map = new Map<String, List<Mutation>>();
cf_map.set(“Standard1”, mutations);
mutations_map.put(ByteBuffer.wrap(“key”.getBytes()), cf_map);
cassandra.batch_mutate(mutations_map, consistency_level);
Wednesday, March 28, 12
12. A query interface
should be...
Simple
Intuitive
Invisible
Performant(?)
Wednesday, March 28, 12
19. Hello...
-- Create or update
INSERT INTO users (id, given, surname)
VALUES (jericevans, Eric, Evans);
-- Create or update
UPDATE users SET given = Eric,
surname = Evans WHERE id = jericevans;
SELECT surname, given FROM users
WHERE id = jericevans;
Wednesday, March 28, 12
20. ...is it me you’re looking
for?
-- Adding an index
CREATE INDEX surnameidx ON users (surname);
SELECT id, given FROM users
WHERE surname = Evans;
-- Limiting the number of rows
SELECT id, given FROM users
WHERE surname = Evans LIMIT 1000;
Wednesday, March 28, 12
21. Querying column ranges
-- From column, to column
SELECT ‘2012-01-01’..’2012-03-28’ FROM News
WHERE topic = cassandra
-- Last N columns
SELECT FIRST 10 REVERSED * FROM News
WHERE topic = cassandra
Wednesday, March 28, 12
22. Counting
-- Get your count on
UPDATE inventory SET apples = apples + 1
WHERE id = fruit;
UPDATE inventory SET carrots = carrots - 1
WHERE id = vegetable;
Wednesday, March 28, 12
23. Batching writes
BEGIN BATCH
INSERT INTO msgs (owner, subject, body)
VALUES(jericevans, ‘Hi’, ‘Howdy’);
UPDATE subjects SET subject = now
WHERE owner = jericevans
APPLY BATCH
Wednesday, March 28, 12
24. A query interface
should be...
Simple
Intuitive
Invisible
Performant(?)
Wednesday, March 28, 12
25. Drivers
• Not a replacement for high-level, idiomatic
libraries
• Avoids duplicating efforts, (error handling,
pooling, etc)
• Consistently scoped, JDBC, etc
• Consistently hosted, licensed
• Discoverable
• More work needed...
Wednesday, March 28, 12
35. -- Tweets in Scotty’s timeline, by date
SELECT * FROM timeline WHERE username =
scotty AND posted_at > ‘2012-03-22’;
Wednesday, March 28, 12
36. Is it a row, or a table?
Yes.
username posted_at body posted_by
scotty 23/03 14:36 stupid klingons... jtkirk
scotty 23/03 16:12 @jtkirk green? spock
scotty 23/03 17:42 @spock yes, green jtkirk
scotty 25/03 08:14 get off my lawn! bones
Wednesday, March 28, 12
37. Also...
• Column names are strictly UTF-8
• Column names are case-insensitive (unless
quoted)
• Old slice notation is gone (<start>..<end>)
• Static column families are actually static
(schema-enforced)
Wednesday, March 28, 12