Skip to content
Evan Prothro edited this page Nov 19, 2015 · 1 revision

There are two ways to use the cassandra interface provided by this gem

1. Accessing through query helpers

# selects all posts
CassandraMigrations::Cassandra.select(:posts)

# more complex select query
CassandraMigrations::Cassandra.select(:posts,
  :projection => 'title, created_at',
  :selection => 'id > 1234',
  :order_by => 'created_at DESC',
  :limit => 10
)

# selects single row by uuid
CassandraMigrations::Cassandra.select(:posts,
  :projection => 'title, created_at',
  :selection => 'id = 6bc939c2-838e-11e3-9706-4f2824f98172',
  :allow_filtering => true  # needed for potentially expensive queries
)

# secondary options
If using gem version 0.2.3+, you can also select based on secondary options listed [here](http://datastax.github.io/ruby-driver/api/session/#execute_async-instance_method).

For instance, for the above query you might want your results to be paginated with 50 results on each page with a timeout of 200 seconds:
CassandraMigrations::Cassandra.select(:posts,
  :projection => 'title, created_at',
  :selection => 'id > 1234',
  :order_by => 'created_at DESC',
  :limit => 10,
  :page_size => 50,
  :timeout => 200
)

All listed options in the linked page above are supported though you can also pass in any secondary options using a "secondary_options" hash as shown below:
CassandraMigrations::Cassandra.select(:posts,
  :projection => 'title, created_at',
  :selection => 'id > 1234',
  :order_by => 'created_at DESC',
  :limit => 10,
  {:secondary_options =>
    {:page_size => 50,
    {:timeout => 200}}
)



# adding a new post
CassandraMigrations::Cassandra.write!(:posts, {
  :id => 9999,
  :created_at => Time.current,
  :title => 'My new post',
  :text => 'lorem ipsum dolor sit amet.'
})

# adding a new post with TTL
CassandraMigrations::Cassandra.write!(:posts,
  {
    :id => 9999,
    :created_at => Time.current,
    :title => 'My new post',
    :text => 'lorem ipsum dolor sit amet.'
  },
  :ttl => 3600
)

# updating a post
CassandraMigrations::Cassandra.update!(:posts, 'id = 9999',
  :title => 'Updated title'
)

# updating a post with TTL
CassandraMigrations::Cassandra.update!(:posts, 'id = 9999',
  { :title => 'Updated title' },
  :ttl => 3600
)

# deleting a post
CassandraMigrations::Cassandra.delete!(:posts, 'id = 1234')

# deleting a post title
CassandraMigrations::Cassandra.delete!(:posts, 'id = 1234',
  :projection => 'title'
)

# deleting all posts
CassandraMigrations::Cassandra.truncate!(:posts)

4. Manipulating Collections

Given a migration that generates a set type column as shown next:

class CreatePeople < CassandraMigrations::Migration
  def up
    create_table :people, :primary_keys => :id do |t|
      t.uuid :id
      t.string :ssn
      ...
      t.set :emails, :type => :string
    end
  end

  ...
end

You can add new emails to the existing collection:

CassandraMigrations::Cassandra.update!(:people, "ssn = '867530900'",
                                       {emails: ['[email protected]', '[email protected]']},
                                       {operations: {emails: :+}})

You can remove emails from the collection:

CassandraMigrations::Cassandra.update!(:people, "ssn = '867530900'",
                                       {emails: ['[email protected]']},
                                       {operations: {emails: :-}})

Or, completely replace the existing values in the collection:

CassandraMigrations::Cassandra.update!(:people, "ssn = '867530900'",
                                       {emails: ['[email protected]', '[email protected]']})

The same operations (addition :+ and subtraction :-) are supported by all collection types.

Read more about C* collections at http://cassandra.apache.org/doc/cql3/CQL.html#collections

3. Using raw CQL3

CassandraMigrations::Cassandra.execute('SELECT * FROM posts')

Reading query results

Select queries will return an enumerable object over which you can iterate. All other query types return nil.

CassandraMigrations::Cassandra.select(:posts).each |post_attributes|
  puts post_attributes
end

# => {'id' => 9999, 'created_at' => 2013-05-20 18:43:23 -0300, 'title' => 'My new post', 'text' => 'lorem ipsum dolor sit amet.'}

If your want some info about the table metadata just call it on a query result:

CassandraMigrations::Cassandra.select(:posts).metadata

# => {'id' => :integer, 'created_at' => :timestamp, 'title' => :varchar, 'text' => :varchar}

Using uuid data type

Please refer to the wiki: Using uuid data type

Clone this wiki locally