Apache Thrift - Interface Definition Language



The Interface Definition Language (IDL) of Apache Thrift is a declarative language used to define the structure of data and services in a way that is independent of any specific programming language.

It enables you to describe data types, service methods, and their interactions in a simple, human-readable format. The Thrift compiler then uses this IDL to generate code in multiple languages, which can be used to implement and interact with the defined services.

Structure of Thrift IDL

Thrift IDL files use a .thrift file extension and follow a simple syntax. The basic structure of a Thrift IDL file includes definitions for data types, constants, enums, structs, and services.

Here is a simple breakdown of the structure of Thrift IDL :

Namespaces

Namespaces help organize your IDL definitions and prevent naming conflicts. You can define namespaces for different programming languages using the namespace keyword. Each namespace directive specifies the target language and the corresponding namespace :

namespace java com.example.thrift
namespace py example.thrift

In this example :

  • namespace java com.example.thrift defines the namespace for Java code generated from the IDL file.
  • namespace py example.thrift defines the namespace for Python code generated from the IDL file.

Data Types

Thrift supports several basic data types that you can use to define the structure of your data. Some of the basic types include :

  • bool: Boolean values (true or false).
  • byte: 8-bit integer.
  • i16: 16-bit integer.
  • i32: 32-bit integer.
  • i64: 64-bit integer.
  • double: Double-precision floating-point number.
  • string: A sequence of characters.
  • binary: A sequence of bytes (used for raw data).

Structures

Structs are used to define complex data types with named fields. Each field in a struct is assigned a unique identifier (ID) and has a specific data type. Fields can be marked as optional or required. Here is an example :

struct User {
  1: i32 id
  2: string name
  3: bool is_active
}

In this User structure :

  • 1, 2, and 3 are field IDs (unique integers) used for serialization.
  • i32, string, and bool are the data types of the fields.
  • id, name, and is_active are the field names.

Enums

Enums (short for enumerations) are used to define a set of named constants. Each constant in an enum is assigned an integer value, starting from 0 by default. You can specify custom values for the constants if needed. Following is an example :

enum Status {
  ACTIVE = 1
  INACTIVE = 2
  PENDING = 3
}

In this "Status" enum :

  • ACTIVE, INACTIVE, and PENDING are possible values.
  • Each value is associated with an integer.

Unions

In Apache Thrift IDL, a union is a special type of data structure that can hold one of several possible fields at a time.

Unlike structures, which can hold multiple fields simultaneously, a union can only hold one field at a time. Following is an example :

union Result {
  1: string message
  2: i32 errorCode
}

In this example :

  • "Result" is the name of the union.
  • It can either have a "string" field named "message" or an "i32" field named "errorCode", but not both at the same time.

Defining Services

Services define the operations that can be performed and the methods that are exposed. Each service contains a list of methods, each of which specifies the parameters and return type. Here is an example :

Syntax

Following is the basic syntax of defining services in Apache Thrift :

service ServiceName {
  <returnType> <methodName>(<parameterList>) throws (<exceptionList>)
}

Here, the service keyword is followed by the name of the service. Inside the curly braces, each method is defined with its return type, method name, list of parameters, and any exceptions it might throw.

Example

In the following example, "UserService" is a service with two methods. The "getUserById" takes an i32 ID and returns a "User" structure. It might throw a "UserNotFoundException". The "updateUser" takes a "User" structure and returns nothing (void).:

  • getUserById takes an i32 ID and returns a User structure.
  • updateUser takes a User structure and returns nothing (void).
service UserService {
  User getUserById(1: i32 id) throws (1: UserNotFoundException e)
  void updateUser(1: User user)
}

Defining Exceptions

Exceptions are used to handle errors that occur during service method calls. You define them like structures but with the exception keyword :

Syntax

Following is the basic syntax of defining exceptions in Apache Thrift :

exception ExceptionName {
  1: <type> <fieldName>
}

Here, the exception keyword is followed by the name of the exception. Inside the curly braces, each field of the exception is defined with a unique integer ID, a data type, and a field name.

Example

In the following example, "UserNotFoundException" is an exception with one field and "message" is a string with ID 1 that holds the error message :

exception UserNotFoundException {
  1: string message
}

Containers in Apache Thrift

In Apache Thrift IDL, containers are used to group multiple values together. They come in three types: list, set, and map. Each type serves a different purpose and has its own characteristics :

  • List: An ordered collection of elements where duplicates are allowed. Following is the syntax example −
list<string> names

This defines a list named "names" where each element is a "string".

  • Set: An unordered collection of unique elements where duplicates are not allowed. Following is the syntax example −
  • set<i32> numbers
    

    This defines a set named "numbers" where each element is a 32-bit integer (i32).

  • Map: A collection of key-value pairs where each key is unique. The keys and values can be of different types. Following is the syntax example −
  • map<string, i32> ageMap
    

    This defines a map named "ageMap" where each key is a "string" (e.g., a person's name) and each value is an "i32" (e.g., their age).

    Advertisements