hector.rb: the pleasant JRuby Cassandra client (wraps Hector)

hectorHector is a Java Cassandra client. It’s a nice abstraction over making raw Thrift calls. Hector’s features include:

  • an object-oriented way to interface with Cassandra
  • serialization helpers
  • failover support
  • connection pooling
  • jmx support

There is already a Ruby cassandra gem, but it uses the Ruby Thrift bindings which do not work well for JRuby. In any case, I want to be able to seamlessly serialize Java objects and store them in Cassandra.

Thus hector.rb was born. hector.rb is a JRuby Cassandra client that wraps Hector. The interface is based on clj-hector. Eventually, I’d like the interface to be API compatible with the cassandra gem.

Example Usage

require 'java'
require 'hector'
cluster = Hector.cluster("Hector", "")
ks_name = java.util.UUID.randomUUID.to_s.gsub("-","")
client = Hector.new(nil, cluster, :retries => 2, :exception_classes => [])
column_families = [{:name =>"a"}, {:name => "b", :type => :super}]
client.add_keyspace({:name => ks_name, :strategy => :local, :replication => 1, :column_families => column_families}) 
client.keyspace = ks_name
sopts = {:n_serializer => :string, :v_serializer => :string, :s_serializer => :string}
client.put_row("a", "row-key", {"k" => "v"})
client.get_rows("a", ["row-key"], sopts) # => {"row-key" => {'k' => 'v'}}
client.get_columns("a", "row-key", ["k"], sopts) # => {'k' => 'v'}
client.put_row("b", "row-key", 
               { "SuperCol"  => {"k" => "v", "k2" => "v2"},
                 "SuperCol2" => {"k" => "v", "k2" => "v2"} })
client.get_super_columns("b", "row-key", "SuperCol", ["k2"], sopts) # => {"k2" => "v2"}

For more examples, see the tests.


gem install --source http://gems.xcombinator.com hector
Get the source here.

Future Work

Now that we can easily deal with [super]rows and columns in Cassandra, the next step is to put an ActiveModel abstraction over it. I’m currently working on modifying cassandra_object to work on JRuby with hector.rb. If you’d like to follow development the branch is here.

  • del.icio.us
  • Reddit
  • Technorati
  • Twitter
  • Facebook
  • Google Bookmarks
  • HackerNews
  • PDF
  • RSS
This entry was posted in big-data. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.