Railsプロジェクトでcassandra gemを少しだけ使いやすくする

cassandra-rb/cassandra · GitHub

Railsプロジェクトでcassandra gemを少しだけ使いやすくする。

本当に少しだけ...。

# lib/cassandra_ext.rb

class CassandraExt < Cassandra
  include Singleton

  def initialize
    config = YAML.load_file(Rails.root.join('config', 'cassandra.yml'))[Rails.env]
    hosts = config['hosts'].map { |host| "#{host}:#{config['port']}" }
    super(config['keyspace'], hosts, config['thrift_client_options'] || {})
  end
end
# config/cassandra.yml

development:
  hosts:
    - '127.0.0.1'
  port: 9160
  keyspace: 'demo_development'
  thrift_client_options:

test:
  hosts:
    - '127.0.0.1'
  port: 9160
  keyspace: 'demo_test'
  thrift_client_options:

production:
  hosts:
    - 'cassandra1.example.com'
    - 'cassandra2.example.com'
  port: 9160
  keyspace: 'demo_production'
  thrift_client_options:
$ rails c
> CassandraExt.instance
=> #<Cassandra:70262775931740, @keyspace="demo_development", @schema={}, @servers=["127.0.0.1:9160"]>

> CassandraExt.instance.keyspaces
=> ["demo_development", "system", "system_traces"]

CQLなら以下のGemがいいですね。

iconara/cql-rb · GitHub

cequel/cequel · GitHub