Wednesday, October 20, 2010

Thrift Installation

Thrift


Thrift is a framework for cross-language services development. It has less documentation and the documentation you will find is for implementing the services. Data is transferred over http in binary form. We can use the serialization it does for caching. So instead of passing it over the wire we will put the serialized data in our cachepage.
How Thrift works is :
Create thrift definition files.
Compile the files to get client and server codes.
Compiled code will have methods to get or set the serialized object.

Languages Supported


* C++
* C#
* Cocoa
* Erlang
* Haskell
* Java
* OCaml
* Perl
* PHP
* Python
* Ruby
* Smalltalk

Compare this with ProtocolBuffers, which supports : C++, Java, Python

You will find interesting comparison with protoBufs at this page : http://stuartsierra.com/2008/07/10/thrift-vs-protocol-buffers


Serialization


Can serialize from and to binary or JSON . http://wiki.apache.org/thrift/ThriftUsageJava

Binary

TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
byte[] bytes = serializer.serialize(work);

TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
Work moreWork = new Work();
deserializer.deserialize(moreWork, bytes);


JSON

TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
String json = serializer.toString(work);


Thrift Definition File


Objects are defined as Structures.
Structure can contain other structures. - same as in Protocol Buffers
Can contain methods that act on the members
* Can import files.
* supports inheritance
For the above to work, compile using special option.
Here it the [http://wiki.apache.org/thrift/Tutorial tutorial]
You can find more examples in the test folder of downloaded tar.

Services


Thrift definition files can have services. The compiler generates client and server code that provides RPC.
This is how Thrift can be used with [http://cassandra.apache.org/ cassandra] and [http://www.lexemetech.com/2008/07/rpc-and-serialization-with-hadoop.html Haddop]

Development Support


* IDE :
Eclipse and IntelliJ plugins have been out. But not sure if they work. Here is the link for [http://sourceforge.net/projects/thrift4eclipse/ Eclipse] and [http://incubator.apache.org/thrift/version_control.html IntelliJ]
* Maven :
out of the box Thrift is supported by ant.
But [http://github.com/dtrott/maven-thrift-plugin maven] plugin is availabile

References


* [http://incubator.apache.org/thrift/ Main Page]
* [http://incubator.apache.org/thrift/static/thrift-20070401.pdf WhitePage]

Installation




Ubuntu
sudo apt-get install libboost-dev automake libtool flex bison pkg-config g++

1. Download Thrift tar ball
http://incubator.apache.org/thrift/download/


2. unzip to /url/local/thrift
unzip /home/asulgaonkar/download/thrift-0.5.0.tar.gz

3. go to the root and read README
follow the instructions and do :

./configure


4. go to lib/java

ant


This will create thrift jar.

5. create thrift compiler

cd compile/cpp
make


How to run the thrift compiler ?

6. Make sure thrift is compiled, both the compiler and the Java library. You should
be able to verify the following:

thrift/tutorial/java$ file ../../lib/java/libthrift.jar
../../lib/java/libthrift.jar: Zip archive data, at least v1.0 to extract


if not do step 4.


thrift/tutorial/java$ file ../../compiler/cpp/thrift
../../compiler/cpp/thrift: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

if not do step 5.


thrift/tutorial/java$ ls ../../lib/java/build/ivy/lib/
commons-lang-2.5.jar junit-4.4.jar servlet-api-2.5.jar slf4j-api-1.5.8.jar slf4j-simple-1.5.8.jar


7. Generate code for java:

thrift/tutorial/java$ cd ..
thrift/tutorial$ thrift -r -gen java tutorial.thrift


>> This will create gen-java
and folders inside it - shared and tutorial



8. Compile example

thrift/tutorial/java$ ant


This will compile the files that were generated


9. Run example:

thrift/tutorial/java$ ./JavaServer &
thrift/tutorial/java$ ./JavaClient




To prove that the serialization and de-serialization works
I modified client code to include following lines :


TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
byte[] bytes = serializer.serialize(work);
System.out.println("bytes : " + String.valueOf(bytes));
System.out.println("Serialization Works!!");

TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
deserializer.deserialize(moreWork, bytes);
System.out.println("De - Serialization Works!!" + moreWork.op + " on " + moreWork.num1 + " and " + moreWork.num2);

TSerializer jsonSerializer = new TSerializer(new TSimpleJSONProtocol.Factory());
String json = jsonSerializer.toString(moreWork);
System.out.println("JSON " + json);