CQL In Cassandra 1.0 (and beyond)

       Big Data DC Meetup #5
          October 17, 2011

                Eric Evans
            eric@acunu.com
          @jericevans, @acunu
●   Overview, history, motivation
●   Changes in CQL 2.0 (Cassandra 1.0)
●   Coming soon (?)
●   Drivers status
What?
●   Cassandra Query Language
    ●   aka CQL
    ●   aka /ˈsēkwəl/
●   Exactly like SQL (except where it's not)
●   Introduced in Cassandra 0.8.0
●   Ready for production use
SQL? Almost.

–- Inserts or updates
INSERT INTO Standard1 (KEY, col0, col1)
VALUES (key, value0, value1)
                   vs.
–- Inserts or updates
UPDATE Standard1
SET col0=value0, col1=value1 WHERE KEY=key
SQL? Almost.
–- Get columns for a row
SELECT col0,col1 FROM Standard1 WHERE KEY=key

–- Range of columns for a row
SELECT col0..colN
    FROM Standard1 WHERE KEY=key

–- First 10 results from a range of columns
SELECT FIRST 10 col0..colN
    FROM Standard1 WHERE KEY=key

–- Invert the sorting of results
SELECT REVERSED col0..colN
    FROM Standard1 WHERE KEY=key
Why?
Interface Instability
(Un)ease of use
Column col = new Column(ByteBuffer.wrap(“name”.getBytes()));
col.setValue(ByteBuffer.wrap(“value”.getBytes()));
col.setTimestamp(System.currentTimeMillis());

ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
cosc.setColumn(col);
Mutation mutation = new Mutation();
Mutation.setColumnOrSuperColumn(cosc);
List mutations = new ArrayList<Mutation>();
mutations.add(mutation);
Map mutations_map = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
Map cf_map = new HashMap<String, List<Mutation>>();
cf_map.set(“Standard1”, mutations);
mutations.put(ByteBuffer.wrap(“key”.getBytes()), cf_map)
CQL
INSERT INTO Standard1 (KEY, col0)
    VALUES (key, value0)
Why? How about...
●   Better stability guarantees
●   Easier to use (you already know it)
●   Better code readability / maintainability
Why? How about...
●   Better stability guarantees
●   Easier to use (you already know it)
●   Better code readability / maintainability
●   Irritates the NoSQL purists
Why? How about...
●   Better stability guarantees
●   Easier to use (you already know it)
●   Better code readability / maintainability
●   Irritates the NoSQL purists
●   (Still )irritates the SQL purists
CQL 2.0
Wait, 2.0? You call that stable?
Yes yes, but...
 ●   We had a few FUBARs in the first pass
 ●   Not the norm, honest
 ●   It's a mulligan
 ●   Look, cats!
Changed in 2.0
●   SELECT count(...) FROM now returns row,
    not column count
●   Terms / Types
    ●   bytea became blob
    ●   int became 4-bytes (was arbitrary precision)
    ●   date became timestamp
Also new (but not breaking)
●   Named keys (virtual columns)
●   Counters
●   Complete DDL support
●   Timestamps and TTLs
Named Keys
–- An unnamed key uses the KEY keyword
CREATE TABLE Standard1
    (KEY text PRIMARY KEY)
–- But you can also name one like so...
CREATE TABLE Standard1
    (username text PRIMARY KEY)
–- The name will also be used in results!
SELECT email FROM Standard1
    WHERE username = 'jericevans'
Counters
–- Counter increment
UPDATE Standard1
SET acount = acount + 1 WHERE KEY = key
–- Counter decrement
UPDATE Standard1
SET acount = acount - 1 WHERE KEY = key
–- 1 not enough? Go nuts.
UPDATE Standard1
SET acount = acount + 9 WHERE KEY = key
Timestamps and TTLs

–- Inserts or updates
UPDATE CFam USING TIMESTAMP 131851901600
SET col0=value0, col1=value1 WHERE KEY=key
–- Inserts or updates
UPDATE CFam USING TTL 86400
SET col0=value0, col1=value1 WHERE KEY=key
Coming Soon(ish)
Roadmap
●   Prepared statements (CASSANDRA-2475)
●   Compound columns (CASSANDRA-2474)
●   Custom transport / protocol (CASSANDRA-2478)
●   Performance testing (CASSANDRA-2268)
●   Schema introspection (CASSANDRA-2477)
●   Multiget support (CASSANDRA-3069)
Drivers
Drivers
●   Hosted on Apache Extras (Google Code)
●   Tagged cassandra and cql
●   Licensed using Apache License 2.0
●   Conforming to a standard for database
    connectivity (if applicable)
●   Coming soon, automated testing and
    acceptance criteria
Drivers
Driver                           Platform                 Status
cassandra-jdbc                   Java                     Good
cassandra-dbapi2                 Python                   Good
cassandra-ruby                   Ruby                     New
cassandra-pdo                    PHP                      New
cassandra-node                   Node.js                  Good

http://code.google.com/a/apache-extras.org/hosting/search?q=label%3aCassandra
The End

CQL In Cassandra 1.0 (and beyond)

  • 1.
    CQL In Cassandra1.0 (and beyond) Big Data DC Meetup #5 October 17, 2011 Eric Evans eric@acunu.com @jericevans, @acunu
  • 2.
    Overview, history, motivation ● Changes in CQL 2.0 (Cassandra 1.0) ● Coming soon (?) ● Drivers status
  • 3.
    What? ● Cassandra Query Language ● aka CQL ● aka /ˈsēkwəl/ ● Exactly like SQL (except where it's not) ● Introduced in Cassandra 0.8.0 ● Ready for production use
  • 4.
    SQL? Almost. –- Insertsor updates INSERT INTO Standard1 (KEY, col0, col1) VALUES (key, value0, value1) vs. –- Inserts or updates UPDATE Standard1 SET col0=value0, col1=value1 WHERE KEY=key
  • 5.
    SQL? Almost. –- Getcolumns for a row SELECT col0,col1 FROM Standard1 WHERE KEY=key –- Range of columns for a row SELECT col0..colN FROM Standard1 WHERE KEY=key –- First 10 results from a range of columns SELECT FIRST 10 col0..colN FROM Standard1 WHERE KEY=key –- Invert the sorting of results SELECT REVERSED col0..colN FROM Standard1 WHERE KEY=key
  • 6.
  • 7.
  • 8.
    (Un)ease of use Columncol = new Column(ByteBuffer.wrap(“name”.getBytes())); col.setValue(ByteBuffer.wrap(“value”.getBytes())); col.setTimestamp(System.currentTimeMillis()); ColumnOrSuperColumn cosc = new ColumnOrSuperColumn(); cosc.setColumn(col); Mutation mutation = new Mutation(); Mutation.setColumnOrSuperColumn(cosc); List mutations = new ArrayList<Mutation>(); mutations.add(mutation); Map mutations_map = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); Map cf_map = new HashMap<String, List<Mutation>>(); cf_map.set(“Standard1”, mutations); mutations.put(ByteBuffer.wrap(“key”.getBytes()), cf_map)
  • 9.
    CQL INSERT INTO Standard1(KEY, col0) VALUES (key, value0)
  • 10.
    Why? How about... ● Better stability guarantees ● Easier to use (you already know it) ● Better code readability / maintainability
  • 11.
    Why? How about... ● Better stability guarantees ● Easier to use (you already know it) ● Better code readability / maintainability ● Irritates the NoSQL purists
  • 12.
    Why? How about... ● Better stability guarantees ● Easier to use (you already know it) ● Better code readability / maintainability ● Irritates the NoSQL purists ● (Still )irritates the SQL purists
  • 14.
  • 15.
    Wait, 2.0? Youcall that stable? Yes yes, but... ● We had a few FUBARs in the first pass ● Not the norm, honest ● It's a mulligan ● Look, cats!
  • 16.
    Changed in 2.0 ● SELECT count(...) FROM now returns row, not column count ● Terms / Types ● bytea became blob ● int became 4-bytes (was arbitrary precision) ● date became timestamp
  • 17.
    Also new (butnot breaking) ● Named keys (virtual columns) ● Counters ● Complete DDL support ● Timestamps and TTLs
  • 18.
    Named Keys –- Anunnamed key uses the KEY keyword CREATE TABLE Standard1 (KEY text PRIMARY KEY) –- But you can also name one like so... CREATE TABLE Standard1 (username text PRIMARY KEY) –- The name will also be used in results! SELECT email FROM Standard1 WHERE username = 'jericevans'
  • 19.
    Counters –- Counter increment UPDATEStandard1 SET acount = acount + 1 WHERE KEY = key –- Counter decrement UPDATE Standard1 SET acount = acount - 1 WHERE KEY = key –- 1 not enough? Go nuts. UPDATE Standard1 SET acount = acount + 9 WHERE KEY = key
  • 20.
    Timestamps and TTLs –-Inserts or updates UPDATE CFam USING TIMESTAMP 131851901600 SET col0=value0, col1=value1 WHERE KEY=key –- Inserts or updates UPDATE CFam USING TTL 86400 SET col0=value0, col1=value1 WHERE KEY=key
  • 21.
  • 22.
    Roadmap ● Prepared statements (CASSANDRA-2475) ● Compound columns (CASSANDRA-2474) ● Custom transport / protocol (CASSANDRA-2478) ● Performance testing (CASSANDRA-2268) ● Schema introspection (CASSANDRA-2477) ● Multiget support (CASSANDRA-3069)
  • 23.
  • 24.
    Drivers ● Hosted on Apache Extras (Google Code) ● Tagged cassandra and cql ● Licensed using Apache License 2.0 ● Conforming to a standard for database connectivity (if applicable) ● Coming soon, automated testing and acceptance criteria
  • 25.
    Drivers Driver Platform Status cassandra-jdbc Java Good cassandra-dbapi2 Python Good cassandra-ruby Ruby New cassandra-pdo PHP New cassandra-node Node.js Good http://code.google.com/a/apache-extras.org/hosting/search?q=label%3aCassandra
  • 26.