email iconemail phone iconcall

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:

  CREATE TABLE collect_things (
    k bigint PRIMARY KEY,
    v frozen <tuple<bigint, text, float>>

session.execute("INSERT INTO collect_things (k, v) VALUES(?, ?)", arguments: [
  0,, '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:

  CREATE TYPE address (
    street varchar,
    city varchar,
    zip varchar,
    phones set<varchar>
  CREATE TABLE people (
    id bigint PRIMARY KEY,
    name varchar,
    address frozen<address>

session.execute("INSERT INTO people (id, name, address) VALUES (?, ?, ?)", arguments: [
  0, 'Jane Smith',
    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.

DataStax has many ways for you to advance in your career and knowledge.

You can take free classes, get certified, or read one of our many white papers.

register for classes

get certified

DBA's Guide to NoSQL


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

Subscribe for newsletter: