Getting started with Apache Thrift

[Prereq: Windows, Visual Studio, Eclipse]
[Project link:]

How it started:
Recently I have been introduced with a very cool technology called Apache Thrift. It is originally developed by Facebook and later open-sourced in Apache Software Foundation. It is one of the core building block of the Facebook technology. The technical paper introducing Thrift can be found here.

What is it:
In layman’s terms, Thrift allows an application written in one language (e.g., Java) to exchange data with an application written another language (e.g., C#). Probably the most popular technology in this area is SOAP. Although SOAP is more general, but Thrift has less overhead. In technical terms, Thrift is a Remote Procedure Call (RPC) framework that can be used to develop scalable cross-language services. Thrift is an interface definition language, i.e., you can only write interfaces or pure abstract classes by Thrift. The Thrift compiler can generate corresponding classes and interfaces for any particular language (e.g., C#) from the Thrift interface. The server (e.g., C#) should implement these interfaces and start a service, and the client (e.g., Java) can call the functions of these interfaces by communicating with the service.

An example:
In this example, I am going to write some libraries in C# and call them in Java, i.e., C# application is the Server and Java application is the client. Let’s write a Math library that has the following functions: add, sub, mul, div, and mod.

Writing the Thrift file:
The Thrift definition file, i.e., math.thrift, will look like this:

The syntax for writing a .thrift can be found here and here. Notice the first 2 lines defines the namespaces for Java and C#.

Installing Thrift Compiler:
The install instruction can be found here. In Windows (which is being used for this example), you can just download the thrift-[version].exe from here.

Downloading Thrift Libraries:
We will also need Thrift libraries. Download and extract the tarball from the previous links.

Generating Corresponding Java and C# classes:
The name of my Thrift Compiler is thrift-0.9.1.exe (you may want to add the path of this exe to your PATH environment variable, so that you can call it directly). Open a command prompt at the location of your math.thrift file and run the following commands:

thrift-0.9.1.exe -gen java math.thrift
thrift-0.9.1.exe -gen csharp math.thrift

These commands should create 2 folders named gen-csharp and gen-java, which have the necessary classes to use Thrift.

Writing the C# server:
Let’s start by writing a normal C# math library. Open up Visual Studio and create a console application (I call it MathServer). Create a math library class called MathServer:

You should be able to use this class from C#. Now, we are going use this class as a Thrift server library. First add MathService.cs (generated by the Thrift compiler) file to your project. If you look at the file, you will see that there is an interface called Iface, which needs to be implemented by the MathServer class. So, change the definition of MathServer class as the following:

public class MathServer : Math.MathService.Iface{...}

At this point, you need to add the thrift library to your project. Open the Thrift C# library from the tarball (should be under /lib/csharp) and compile it. It should generate a Thrift.dll. Add this dll to the MathServer project as a reference. The final step is to change the Main function in the following way to run the program as a service.

Writing the Java client:
The client only has to call the library. Open up Eclipse create a project and a class with the main function (I call it Copy the code generated by the Thrift compiler to this project ( Copy the Thrift Java library from the tarball (should be under /lib/java) to this project. You will additionally need to add 2 jars namely, httpclient-[version].jar and httpcore-[version].java, both of them can be found in here. Now add this code to the main function:

The client is done. At this point, eclipse will show some more errors mostly because there are additional libraries required for it to run as a server and for logging. Either add these libraries or comment out the lines showing errors.

Go to Visual Studio and run the server. It should wait for client to connect. Go back to Eclipse and run the client. It should be able to use the Math library of the server and the server should print the functions that the client called.

The working copy of the projects can be downloaded from github.

Tagged: , , , , , , , , , , ,

5 thoughts on “Getting started with Apache Thrift

  1. Joseph Banks February 20, 2014 at 7:19 am Reply

    this is a great tutorial.

    Have a question. Is it possible to send a value to the server? Because so far, I can only find how client get values from the server.


    • Faisal R. February 20, 2014 at 7:23 am Reply

      Yes, of course. Rather than saying “client get values from the server”, lets say that “client can call a function of the server”. And in that function you can do whatever you want. You are sending values to the server by the parameters of the function.

  2. Joseph Banks February 20, 2014 at 7:18 pm Reply

    Hey.. OK, that makes sense.. thanks for the fast reply

  3. Padmika August 3, 2014 at 7:41 am Reply

    Nice tutorial

  4. Knox November 5, 2014 at 1:48 am Reply

    Great tutorial. Thx 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: