Yann’s Blog

July 19, 2008

Using Facebook’s Thrift with Python and HBase

Filed under: Python, Software, Tome — Tags: , , — Yann @ 1:25 am

Today I’m going to show you how to interface Python to Apache HBase using Facebook’s Thrift package. Hbase is a documented oriented database which is very similar to Google’s BigTable (in fact its more or less a clone of BigTable as seen in the BigTable paper). HBase has two primary interfaces - a REST API which is relatively slow, and a Thrift interface, which is recommended for high speed communication. For speed and other reasons, we’re going to be using the Thrift API.

Note that I am going to be touching on some Hbase jargon (such as column families). Its not essential to understand what those are if you are just trying to build a Python Thrift client. But if you’re trying to use HBase, I would consider that knowledge essential.

Getting Setup

First thing’s first, you need need to grab a copy of both HBase and Thrift. For this tutorial, I am using the Subversion copy of HBase (as of July 18th) and Thrift version 20080411p1. Thrift is shipped as a source package, you will need a compiler toolchain, as well as any Python development packages or header files your system may require (such as python-dev on Debian/Ubuntu). You’ll also need the Java JDK package (such as sun-java6-jdk on Ubuntu).

Thrift can be compiled using the standard routine:

./configure
make -j4
sudo make install

After installing thrift, you should have a system-wide ‘thrift’ command available, which should provide some usage information. Thrift uses a descriptor file for the communication layer, available as a .thrift file. I’m not going to describe how to create such a descriptor file here (perhaps in a later blog post), as we’ll be using the one provided by HBase (with one small tweak). You will need the HBase source package for this exercise.

Build a Thrift Client Package

Open up [hbasesrc]/src/java/org/apache/hadoop/hbase/thrift/Hbase.thrift in your favorite text editor. Search for lines containing ruby_namespace, and add the following line in the same region:

namespace py hbase

(Alert readers will wonder why we didn’t use py_namespace. The reason is simple, the xxx_namespace Thrift commands are deprecated, replaced with namespace xxx).

Next up, we’ll generate our Python HBase thrift interface. Fire up your shell to the same location, and run

thrift --gen py Hbase.thrift

Now we have generated a set of Python classes in the gen-py folder which will allow you to talk to the Hbase thrift server automatically. Lets setup our Python Thrift server now. I’ll grab the hbase folder inside of the gen-py folder, and move it to a project directory of your choosing.

Building a Client

Next up, we’ll need to work on the Python Thrift client application. I suggest starting with the Thrift server tutorial for a boilerplate template. Below is the file we’re going to use (lets just assume it is called client.py for this discussion):

#!/usr/bin/env python
import sys
 
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
 
from hbase import Hbase
from hbase.ttypes import *

This is general Thrift boilerplate. The application specific portions up to now are the last two lines. Hbase is the name of the service as described in the Hbase.thrift file.

Next up, we’re going to try to connect to our HBase instance. To do that, we will first create a TSocket, then add a TBufferedTransport over the raw socket, and then wrap that in a TBinaryProtocol. If someone has studied too much Java, it was the Thrift developers ;).

# Make socket
transport = TSocket.TSocket('localhost', 9090)
 
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)
 
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)

Now two application specific lines - we’re going to build a Hbase.Client() object, and then finally open up our transport.

client = Hbase.Client(protocol)
 
transport.open()

We can do a quick validation pass now, and start up Hbase (if you have a running Hbase server somewhere, you can omit this step of course). If you have a source checkout of Hbase, compiling is as simple as running the ant tool. Assuming you have the JDK installed, Hbase should be ready for action in under a minute. Start up a master Hbase instance by running bin/hbase master start &. Then, start up a thrift server for Hbase, by running bin/hbase thrift start.

Running our client script now should lead to no errors. If it does, stop, and try to figure out what is wrong (did you move the gen-py/hbase directory to where your client.py script is or set the python path appropriately?).

Using the Client

Lets call our first method: getTableNames(). Add this to the end of our script:

print client.getTableNames()

By default, it will simply print a blank list ([]), unless of course you have created tables. This is the simplest example of using Thrift with HBase and Python, where no special data structures are needed or passed around. But if we look at the HBase Thrift API (not up to date - for full details look at the Hbase.thrift file), we can see some methods will require parameters in the form of Thrift structs.

Lets try to create a table in Hbase. When we consult HBase.thrift, we can see it requires a list of ColumnDescriptors.

  /**
   * Create a table with the specified column families.  The name
   * field for each ColumnDescriptor must be set and must end in a
   * colon (:).  All other fields are optional and will get default
   * values if not explicitly specified.
   *
   * @param tableName name of table to create
   * @param columnFamilies list of column family descriptors
   *
   * @throws IllegalArgument if an input parameter is invalid
   * @throws AlreadyExists if the table name already exists
   */
  void createTable(1:Text tableName, 2:list columnFamilies)
    throws (1:IOError io, 2:IllegalArgument ia, 3:AlreadyExists exist)

Luckily, the thrift compiler has generated a Python class for this ColumnDescriptor (which we acquired by importing hbase.ttypes.*). Sadly, this isn’t the most Python of all classes, but will be quite serviceable for our needs. Lets build a ColumnDescriptor for a column-family called foo. For Hbase, we need to specify the column family in the name: format - so don’t forget that colon, or you will be faced with an IllegalArgumentException.

desc = ColumnDescriptor( { 'name' : 'foo:' } )

Note that there are many more fields you can use. Either consult the Hbase.thrift file or the hbase/ttypes.py file for details.

Now we’re ready to create our table!

client.createTable('our_table', [desc])
 
print client.getTableNames()

Running this script should yield a [] followed by ['our_table']. Now we have a table in Hbase! Congratulations!

Handling Errors

If you run the script again, you’ll notice that you get an exception since the table name is already in use. This is of course expected, but also highlights Thrift’s ability to propagate exceptions from the remote system.

Exceptions must be predefined in the .thrift interface file. For the case of the createTable method, there are three possible exceptions. Catching them is much like any other exception. Here is our program, changed to catch the AlreadyExists exception:

try:
    desc = ColumnDescriptor( d = { 'name' : 'foo:' } )
 
    client.createTable('our_table', [desc])
 
    print client.getTableNames()
 
except AlreadyExists, tx:
    print "Thrift exception"
    print '%s' % (tx.message)

Note specifically the presence of the message attribute. The Thrift compiler doesn’t generate a nice __str__ or __repr__ method for Python exceptions, so in many cases to determine the exact cause of the error, you need to grab the message attribute.

Wrapping Up

Before this turns into an exhaustive documentation of the HBase Thrift API, I’m going to put a close on this post :). I hope this short example will help you with using Hbase and Python, and combining Hbase and Thrift. In a future post, I will touch upon how to create a Python Thrift server, and define your own Thrift interface file.

July 15, 2008

Copious Project Starter - New Stuff

Filed under: Python, Software, StackFoundry, Tome — Yann @ 12:06 am

So, I’m a copious project and idea mill. Sometimes my ideas actually gain some traction, but often they languish on my hard drive. The useful ones I eventually get around to releasing though, which is what this blog post is all about.

If you’ll head over to StackFoundry.com you can see my recently updated Other section. Highlights here include:

  • quickmovie - a movie browser. I made this little program to keep a tally and easy to browse list of DVD images I keep on disk. It scans a directory (or set of), and grabs from IMDB relevant titles, summaries, and cover images. It has an AJAXy browser page which is served up by CherryPy allowing you to browse the list. Probably won’t get a lot of attention. The only feature addition I’m planning is to add a “Watch on date X” dialog, keeping track of when you last saw the movie (if ever).
  • qsgen - a quick static site generator. Most sites really don’t need a CMS system such as Django, Plone, or Pylons. There is a lot of dynamic heavy lifting for really little marginal gain. Yes, some dynamic features (such as RSS feeds of news, etc) are cool, but do sites really need that? I guess I’m not Web 2.0 compliant in that sense, but I’m proud of it ;). qsgen is designed to take a site built from Mako .html templates and compile the templates into real .html files, copying along supporting images, CSS, and JS files (amongst others). Its a very basic little script, and only accepts two parameters :) I’m using it to build stackfoundry.com and tomeapi.com
  • yyafl - Yann’s Yet Another Form Library. A re-implementation and adaptation of Django’s newforms library. It does use a few code pieces from the Django project (0.96.2), so it retains the same license as Django. It is designed to run stand-alone from the Django mega-infastructure though, allowing it to be used in projects such as Pylons and CherryPy as a form library. The API is not stable yet, as I’m overcoming some ugliness and rearranging some modules. Expect a release soon with a stableish API.

Beyond that, you can find the anyvcs and softwedge projects on the site. I’m looking for someone interested in anyvcs to help maintain a Mercurial port of it (and complete the git port ;)). I will be resuming work on anyvcs when I pick up wedge again, which is going to be after a first pass at tome.

As I said, I’m a copious project starter.

July 11, 2008

A Python API for Document Databases? Introducing tome

Filed under: Python, Software, Tome — Yann @ 11:24 pm

The relational database is dead. Long live the document database!

Ok, maybe that statement is far too gloomy and patently untrue. Relational databases have their place and their uses. But as a general purpose web application data storage system, they are not always the best tool for the job. There are many use cases where the RDBMS is not an effective storage engine.

Enter the ‘document database’. Or otherwise known as the big and fancy hash map. The concept is gaining momentum in the industry, particularly for online applications which are made up of structured data and documents: social sites, search engines, blogging applications, etc. It is an extension of data-de-normalization. By de-normalizing data, you can safely distribute and parallelize its storage and representation. You can do this with a RDBMS as well, but at its heart, you are still using an RDBMS.

Document databases are basic key/value storage systems. They are distinctly different from relational databases in that queries can only be performed (efficiently) on a key. Documents should also be de-normalized, minimizing external references so a complete view can be obtained without having to fall back to relational semantics (primarily JOINs).

Projects such as Hadoop/HBase, CouchDB, and even Google’s BigTable are great examples of emerging (and successful) document oriented databases. The problem is they all have non-standard access modules (if any), and are “bare bones” in the access model.

What Python needs is a specification and implementation of a document database access API. On top of this could be a layer similar in ideology to SQLAlchemy, in which Python classes could represent documents and any links amongst them.

I am working on an implementation of the lower-level access layers for HBase and CouchDB (leveraging the excellent CouchDB Python module). In addition, there will be a “DB-API” adapter and anydbm adapter for developer prototyping.

Its not specification worthy at this point in time, but will hopefully foster growth in this emerging field.

And since every project needs a nifty name, I dub thee tome.

Powered by WordPress