You are previewing Learning Apache Thrift.
O'Reilly logo
Learning Apache Thrift

Book Description

Make applications cross-communicate using Apache Thrift!

About This Book

  • Leverage Apache Thrift to enable applications written in different programming languages (Java, C++, Python, PHP, Ruby, and so on) to cross-communicate.

  • Learn to make your services ready for real-world applications by using stepwise examples and modifying code from Industry giants.

  • Be a crackerjack at solving Apache Thrift-related issues.

  • Who This Book Is For

    If you have some experience of developing applications in one or more languages supported by Apache Thrift (C++, Java, PHP, Python, Ruby, and others) and want to broaden your knowledge and skills in building cross-platform, scalable applications, then this book is for you.

    What You Will Learn

  • Understand the need for cross-language services and the basics of Apache Thrift.

  • Learn how Apache Thrift works and what problems it solves.

  • Determine when to use Apache Thrift instead of other methods (REST API), and when not to use it.

  • Create and run an example application using Apache Thrift.

  • Use Apache Thrift in your applications written in different languages supported by Apache Thrift (PHP, Python, Ruby, Java, and C++).

  • Handle exceptions and deal with errors.

  • Modify code in different languages.

  • Use Apache Thrift in the production environments of big applications.

  • In Detail

    With modern software systems being increasingly complex, providing a scalable communication architecture for applications in different languages is tedious. The Apache Thrift framework is the solution to this problem! It helps build efficient and easy-to-maintain services and offers a plethora of options matching your application type by supporting several popular programming languages, including C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml, and Delphi.

    This book will help you set aside the basics of service-oriented systems through your first Apache Thrift-powered app. Then, progressing to more complex examples, it will provide you with tips for running large-scale applications in production environments.

    You will learn how to assess when Apache Thrift is the best tool to be used. To start with, you will run a simple example application, learning the framework's structure along the way; you will quickly advance to more complex systems that will help you solve various real-life problems. Moreover, you will be able to add a communication layer to every application written in one of the popular programming languages, with support for various data types and error handling. Further, you will learn how pre-eminent companies use Apache Thrift in their popular applications.

    This book is a great starting point if you want to use one of the best tools available to develop cross-language applications in service-oriented architectures.

    Style and approach

    A stepwise guide to learning Apache Thrift, with ready-to-run examples explained comprehensively. Advanced topics supply the inspiration for further work.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Learning Apache Thrift
      1. Table of Contents
      2. Learning Apache Thrift
      3. Credits
      4. About the Author
      5. About the Reviewer
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Introducing Apache Thrift
        1. Distributed systems and their services
          1. Service-oriented architecture
          2. Distributed systems
          3. Maintainability
          4. Scalability
          5. Testability
        2. An introduction to Apache Thrift
          1. Supported programming languages
          2. Data types
          3. Transports
          4. Protocols
          5. Versioning
          6. Security
          7. Interface description language
        3. Apache Thrift and others
          1. Custom protocols
          2. XML-RPC and JSON-RPC
          3. SOAP and WSDL
          4. RESTful APIs
          5. CORBA
          6. Apache Avro
          7. Protocol Buffers
        4. When to choose Apache Thrift
        5. Summary
      9. 2. Installing and Running Apache Thrift
        1. Installing Apache Thrift on Linux
          1. Installation requirements
          2. Installing dependencies
            1. Installing dependencies on CentOS
            2. Installing dependencies on Debian and Ubuntu
          3. Installing Apache Thrift
        2. Installing Apache Thrift on Mac OS X
          1. Installing Apache Thrift
        3. Installing Apache Thrift on Windows
        4. Testing the installation
        5. Summary
      10. 3. Running Your First Apache Thrift Service and Client
        1. Creating necessary project files
          1. Creating a local copy of the Apache Thrift libraries
          2. Defining our first service and generating files
          3. The service code in PHP
          4. The client code in Python
        2. Running the code
          1. What really happened?
        3. Analyzing the code
          1. The service description – IDL
          2. The server script – PHP
          3. The client script – Python
        4. Summary
      11. 4. Understanding How Apache Thrift Works
        1. Prepare your tools
        2. Apache Thrift's architecture
          1. Going about using the tool
            1. Designing the services
            2. Preparing the interface description
            3. Generating service and client libraries
            4. Implementing services and clients
            5. Running server and clients
          2. The network stack
          3. Transport
          4. Protocol
          5. Processor
          6. Server and client
          7. Example
        3. Apache Thrift's type system
          1. Basic types
          2. Special types
          3. Structs
          4. Unions
          5. Containers
            1. list
            2. set
            3. map
            4. Usage of containers
          6. Enums
          7. Exceptions
          8. Services
        4. IDL syntax
          2. Document
          3. Headers
            1. Thrift include
            2. C++ include
            3. Namespace
          4. Definitions
            1. const
            2. typedef
        5. Summary
      12. 5. Generating and Running Code in Different Languages
        1. PHP
          1. Generating the code
          2. Examining the code
          3. Transports
          4. Protocols
          5. Servers
          6. Implementing and running the service
          7. Implementing and running the client
        2. Java
          1. Generating the code
          2. Examining the code
          3. Transports
          4. Protocols
          5. Servers
          6. Implementing and running the service
          7. Implementing and running the client
        3. Python
          1. Generating the code
          2. Examining the code
          3. Transports
          4. Protocols
          5. Servers
          6. Building the libraries
          7. Implementing and running the service
          8. Implementing and running the client
        4. JavaScript
          1. Generating the code
          2. Examining the code
          3. Transport, protocol, and servers
          4. Implementing and running the client
        5. Ruby
          1. Generating the code
          2. Examining the code
          3. Transports
          4. Protocols
          5. Servers
          6. Implementing and running the service
          7. Implementing and running the client
        6. C++
          1. Generating the code
          2. Examining the code
          3. Transports
          4. Protocols
          5. Servers
          6. Implementing and running the service
          7. Implementing and running the client
        7. Summary
      13. 6. Handling Errors in Apache Thrift
        1. What are the type of errors that can occur?
          1. Syntax errors
          2. Runtime errors
          3. Logic errors
        2. What are exceptions and how to handle them?
        3. Handling exceptions in Apache Thrift
          1. An example code
          2. Implementing the divide method
          3. Running the application without error handling
          4. Adding error handling to the server
          5. Adding error handling to the client
          6. Advanced error handling
        4. Summary
      14. 7. An Example Client-Server Application
        1. Our example application
        2. Planning out your work
        3. Getting a general idea of the example application
        4. A technical overview of the application
          1. get_distance
          2. find_occurences
          3. save_to_log
          4. The server
          5. Clients
        5. Preparing the Apache Thrift document
          1. The basic toolbox – base.thrift
          2. The MyToolbox service – mytoolbox.thrift
          3. Compiling the IDL files
        6. Implementing the server
          1. Imports
          2. Displaying errors on the console (logger)
          3. Implementing service methods
          4. Creating the server
          5. Running the server
        7. Implementing and running clients
          1. Creating a client in PHP
          2. Creating a client in Ruby
        8. Further testing and other exercises
        9. Summary
      15. 8. Advanced Usage of Apache Thrift
        1. Apache Thrift in production
          1. Code version control systems
          2. Code deployment
          3. Apache Thrift versioning
          4. Apache Thrift performance
            1. Comparing Java servers
            2. Comparing C++ servers
          5. Service multiplexing
          6. Security issues
            1. General security tips
            2. Transport Layer Security/Secure Sockets Layer
              1. Generating keystores
              2. Using keystores in the Java code
        2. Real-world examples of the usage of Apache Thrift
          1. FBThrift in Facebook
          2. Apache Thrift in Evernote
          3. Apache Thrift in Twitter
          4. Apache Thrift in other companies
        3. Summary
      16. Index