Archive

Posts Tagged ‘technology’

Thrift Tutorial on Ubuntu 11.10

February 10, 2012 Leave a comment

What is Thrift and how can it help you and your team build a highly scalable platform for web services? According to its project page [1], “Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml.” Although Facebook open sourced it in April 2007, its documentation could be improved to make it easier to try out and start building prototypes. Even the project’s tutorial page is still a work-in-progress. So, here’s my version of such, specifically for Java on Ubuntu 11.10. I hope you find it useful.

Get Thrift

First, make sure your system meets the requirements specified at [2]. Here’s how you can install the required packages on Ubuntu 11.10.

$ sudo apt-get update
$ sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

Then, install the Java OpenJDK and ant.

$ sudo apt-get install openjdk-6-jdk ant

Now you can get Thrift. I chose to checkout Thrift from the Apache SVN repository.

$ cd ~/svn
$ svn co http://svn.apache.org/repos/asf/thrift/trunk thrift

You can also simply download the stable, snapshot or archived releases of Thrift from [3].

Build and Install

Now, you’re ready to build and install Thrift on your system.

$ cd thrift
$ ./bootstrap.sh
$ ./configure
$ make
$ sudo make install
$ thrift -version

Your First Thrift File

The tutorial directory, thrift/tutorial, contains the files tutorial.thrift and shared.thrift. Cleverly, the tutorial.thrift file teaches you Thrift in a .thrift file! Read those files and start learning.

Build for Java

At this point, you’re probably itching to see something running. For this tutorial, I chose the Java example. The tutorial directory, thrift/tutorial, contains examples under each language’s directory. But, before you can run the Java example, you have to compile the Java library first.

$ cd ~/svn/thrift/lib/java
$ ant

Now, you get to see the code generation engine at work.

Using The Thrift Compiler

$ cd ~/svn/thrift/tutorial

The following command tells Thrift to generate Java code. The -r option tells it to also generate included files.

$ thrift -r –gen java tutorial.thrift

After the command runs, you should see a directory called gen-java. If you look under that directory, you should see two directories: shared and tutorial.

Compile The Example

Now that the code has been generated, you can compile the Java example.

$ cd ~/svn/thrift/tutorial/java
$ ant

Running A Thrift Server

Now, for what you’ve been waiting for, a working example of a client/server application working through Thrift!

$ ./JavaServer &
Starting the simple server…
Starting the secure server…

Running A Thrift Client

$ ./JavaClient simple

If all is well, you should see something like the following:

ping()
1+1=2
Whoa we can divide by 0
15-10=5
Check log: 5

Other Languages

Make sure to try out the other examples for the other languages under each respective directory. You may need to install additional packages to support other languages. Please refer to [5].

References:

  1. http://thrift.apache.org/
  2. http://wiki.apache.org/thrift/ThriftRequirements
  3. http://thrift.apache.org/download/
  4. http://wiki.apache.org/thrift/ThriftInstallation
  5. http://wiki.apache.org/thrift/GettingUbuntuPackages
Design a site like this with WordPress.com
Get started