Rhino, a Ruby HBase ORM

Rhino is a Ruby HBase ORM with support for has_many and belongs_to relationships, constraints/validations, and the HBase Thrift API. It can replace ActiveRecord in Rails (or Merb or any other Ruby framework).

git clone git://github.com/sqs/rhino.git

The following is a feature walkthrough (last updated Dec 17, 2008). You can also read the full Rhino RDoc documentation or browse the code on Github.

Has_many and belongs_to, HBase-style

Rhino’s has_many and belongs_to define relationships within a single table, unlike in ORMs for other databases where they define relationships between tables. The following code shows how to set up a situation where your pages(title:, contents:, links:, meta:, images:) table has_many links and images.

 

class Link < Rhino::Cell

  belongs_to :page

end

class Image < Rhino::Cell

  belongs_to :page

end

class Page < Rhino::Model

  column_family :title

  column_family :contents

  column_family :links

  column_family :meta

  column_family :images

  has_many :links, Link

  has_many :images, Image

end

Once you’ve done that, you can do things like:

 

nytimes_page = Page.get('com.nytimes')

nytimes_page.links.each do |link|

  puts link.key

end

nytimes_page.links.create('com.apple/itunes', 'iTunes')

nytimes_page.links.get('com.google')

You can define methods on the Link class to make common operations simpler, like converting from com.apple/iTunes to apple.com/iTunes:

 

class Link < Rhino::Cell

  belongs_to :page

  def url

    url_parts = key.split('/')

    backwards_host = url_parts.shift

    path = url_parts.join('/')

    host = backwards_host.split('.').reverse.join('.')

    "http://#{host}/#{path}"

  end

end

Constraints (like ActiveRecord’s validations)

In your table classes, you can specify conditions that must exist before the record can be saved. Here’s an example.

 

class Page < Rhino::Model

  column_family :title

  # ...other column_family descriptions here...

  constraint(:title_required) { |page| page.title and !page.title.empty? }

end

If you try to save a page without a title, here’s what you’ll get.

 

page_without_title = Page.new('com.example', :contents=>'Welcome to my page')

page_without_title.save # => raises Rhino::ConstraintViolation: Page failed constraint title_required

Rails integration

Rhino provides all of the basic functionality required for Rails models. It’s not a drop-in replacement for ActiveRecord, but with some tweaking and thinking, it works well. I’ve provided some sample code for Rails integration taken directly from a Rails app that uses Rhino.

HBase’s Thrift API

Rhino uses HBase’s Thrift API.

How to install Rhino

It’s not packaged yet, so you have to clone the Git repository and then require ‘PATH_TO_RHINO/lib/rhino.rb’ to use Rhino.

git clone git://github.com/sqs/rhino.git

Email me if you have any questions.

Links

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>