Ruby Driver 2.1 release

By Bulat Shakirzyanov -  February 23, 2015 | 0 Comments

Finally, the long-anticipated Ruby Driver 2.1 with full support of Apache Cassandra 2.1 features is released.

User-defined types and tuples

The first major new feature that I would like to bring to your attention is the availability of new data types in Apache Cassandra 2.1, specifically – tuples and user-defined types.

A tuple is a fixed-length set of elements, with each element having its own type. An example is worth a thousand words:

session.execute(<<-CQL)
  CREATE TABLE collect_things (
    k bigint PRIMARY KEY,
    v frozen <tuple<bigint, text, float>>
  )
CQL

session.execute("INSERT INTO collect_things (k, v) VALUES(?, ?)", arguments: [
  0, Cassandra::Tuple.new(3, 'bar', 2.1)
])

p session.execute("SELECT * FROM collect_things")
# => #<Cassandra::Result:0x3fe3ce1a1584 @rows=[{"k"=>0, "v"=>#<Cassandra::Tuple:0x3fe3ce1a1610 (3, bar, 2.1)>}] @last_page=true>

A user-defined type is a collection of named and typed elements. Once defined, it can be used just like any of the built-in types:

session.execute(<<-CQL)
  CREATE TYPE address (
    street varchar,
    city varchar,
    zip varchar,
    phones set<varchar>
  )
CQL
session.execute(<<-CQL)
  CREATE TABLE people (
    id bigint PRIMARY KEY,
    name varchar,
    address frozen<address>
  )
CQL

session.execute("INSERT INTO people (id, name, address) VALUES (?, ?, ?)", arguments: [
  0, 'Jane Smith',
  Cassandra::UDT.new(
    street: '123 Main St.',
    city: 'Anyville',
    zip: '99999',
    phones: Set['888-888-8888', '555-555-5555']
  )
])

p session.execute("SELECT * FROM people")
# => #<Cassandra::Result:0x3fc11c8051d4 @rows=[{"id"=>0, "address"=>#<Cassandra::UDT:0x3fc11c805918 { street: "123 Main St.", city: "Anyville", zip: "99999", phones: #<Set: {"888-888-8888", "555-555-5555"}> }>, "name"=>"Jane Smith"}] @last_page=true>

User defined types can be accessed using schema metadata. Given the schema from the example above, we can inspect the user-defined type representing the address:

puts cluster.keyspace('simplex').type('address').to_cql
# => CREATE TYPE simplex.address (
# =>   street varchar,
# =>   city varchar,
# =>   zip varchar,
# =>   phones set<varchar>
# => );

Public types api

Until today, the data types API has been strictly private and not exposed anywhere. However, with the introduction of user-defined types and tuples it becomes increasingly important to allow the users to inspect types of columns and fields in tables and user-defined types.

The Cassandra::Types set of apis has been created to allow just that:

# include Cassandra::Types for brevity
include Cassandra::Types

p cluster.keyspace('simplex').type('address').field('street').type == varchar      # => true
p cluster.keyspace('simplex').type('address').field('phones').type == set(varchar) # => true
p cluster.keyspace('simplex').table('people').column('address').type == udt('simplex', 'address', 'street', varchar, 'city', varchar, 'zip', varchar, 'phones', set(varchar)) # => true

Named arguments

The 2.1 release introduces the ability to use named arguments in prepared statements. Apache Cassandra 2.1 introduces a special CQL syntax for those:

p session.execute("SELECT * FROM people WHERE id = :user_id", arguments: {user_id: 0})
# => #<Cassandra::Result:0x3fe4728bb474 @rows=[{"id"=>0, "address"=>#<Cassandra::UDT:0x3fe4728bb5f0 { street: "123 Main St.", city: "Anyville", zip: "99999", phones: #<Set: {"888-888-8888", "555-555-5555"}> }>, "name"=>"Jane Smith"}] @last_page=true>

Increased number of stream ids

In addition to the user-facing changes above, Apache Cassandra 2.1 introduces a change in the native protocol. The greatly increased number of stream ids allows more requests to be executed in parallel over a single connection. As a result, the Ruby Driver uses a single TCP connection to each node in the cluster, where previously it would have used two.

Overall, this is a huge release that introduces many long anticipated features. I hope you’ll enjoy using it as much as I’ve enjoyed implementing it.



Comments

Your email address will not be published. Required fields are marked *




Subscribe for newsletter:

© 2017 DataStax, All rights reserved. Tel. +1 (408) 933-3120 sales@datastax.com Offices

DataStax is a registered trademark of DataStax, Inc. and its subsidiaries in the United States and/or other countries.
Apache Cassandra, Apache, Tomcat, Lucene, Solr, Hadoop, Spark, TinkerPop, and Cassandra are trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries.