Learning Apache Thrift
()
About this ebook
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.
Related to Learning Apache Thrift
Related ebooks
Advanced C++ Programming Cookbook: Become an expert C++ programmer by mastering concepts like templates, concurrency, and type deduction Rating: 0 out of 5 stars0 ratingsPractical C Programming: Solutions for modern C developers to create efficient and well-structured programs Rating: 0 out of 5 stars0 ratingsApache Mahout Clustering Designs Rating: 0 out of 5 stars0 ratingsReactive Programming for .NET Developers Rating: 0 out of 5 stars0 ratingsBare-Metal Embedded C Programming: Develop high-performance embedded systems with C for Arm microcontrollers Rating: 0 out of 5 stars0 ratingsDocker A Complete Guide - 2020 Edition Rating: 0 out of 5 stars0 ratingsLearning BeagleBone Python Programming Rating: 0 out of 5 stars0 ratingsProgrammer's Guide to Apache Thrift Rating: 0 out of 5 stars0 ratingsGetting Data Science Done: Managing Projects From Ideas to Products Rating: 0 out of 5 stars0 ratingsMastering Android Development with Kotlin: Deep dive into the world of Android to create robust applications with Kotlin Rating: 0 out of 5 stars0 ratingsUsing Yocto Project with BeagleBone Black Rating: 0 out of 5 stars0 ratingsGetting Started with Hazelcast - Second Edition Rating: 0 out of 5 stars0 ratingsOpa Application Development Rating: 0 out of 5 stars0 ratingsSignalR Realtime Application Cookbook Rating: 0 out of 5 stars0 ratings.NET 7 for Jobseekers: Elevate your coding journey with .NET 7 (English Edition) Rating: 0 out of 5 stars0 ratingsHow Computers Make Books: From graphics rendering, search algorithms, and functional programming to indexing and typesetting Rating: 0 out of 5 stars0 ratingsBuilding Microservices with Node.js: Explore microservices applications and migrate from a monolith architecture to microservices Rating: 0 out of 5 stars0 ratingsProjects with IOTA Rating: 0 out of 5 stars0 ratingsDeep Learning Essentials: Your hands-on guide to the fundamentals of deep learning and neural network modeling Rating: 0 out of 5 stars0 ratingsAjax Bible Rating: 3 out of 5 stars3/5Storm Applied: Strategies for real-time event processing Rating: 0 out of 5 stars0 ratingsDomain Driven Design A Complete Guide - 2020 Edition Rating: 0 out of 5 stars0 ratingsRuby On Rails A Complete Guide - 2020 Edition Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook Rating: 0 out of 5 stars0 ratingsCacti 0.8 Network Monitoring Rating: 0 out of 5 stars0 ratingsIntroduction to Python Programming Rating: 0 out of 5 stars0 ratings
Applications & Software For You
Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Logic Pro X For Dummies Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Sound Design for Filmmakers: Film School Sound Rating: 5 out of 5 stars5/580 Ways to Use ChatGPT in the Classroom Rating: 5 out of 5 stars5/5Adobe Premiere Pro: A Complete Course and Compendium of Features Rating: 5 out of 5 stars5/5The Basics of User Experience Design by Interaction Design Foundation Rating: 4 out of 5 stars4/5Animation for Beginners: Getting Started with Animation Filmmaking Rating: 3 out of 5 stars3/5Blender 3D Basics Beginner's Guide Second Edition Rating: 5 out of 5 stars5/5Data Visualization: A Practical Introduction Rating: 5 out of 5 stars5/5The Designer’s Guide to Figma: Master Prototyping, Collaboration, Handoff, and Workflow Rating: 0 out of 5 stars0 ratingsBlender 3D By Example Rating: 4 out of 5 stars4/5Hands-On Motion Graphics with Adobe After Effects CC: Develop your skills as a visual effects and motion graphics artist Rating: 0 out of 5 stars0 ratingsAdobe Illustrator: A Complete Course and Compendium of Features Rating: 0 out of 5 stars0 ratingsBlender 3D for Jobseekers: Learn professional 3D creation skills using Blender 3D (English Edition) Rating: 0 out of 5 stars0 ratingsLogseq for Students: Super Powered Outliner Notebook for Learning with Confidence Rating: 5 out of 5 stars5/5Experts' Guide to Obsidian Rating: 3 out of 5 stars3/5Adobe Photoshop Lightroom Classic For Dummies Rating: 0 out of 5 stars0 ratingsOneNote Recipes: Elegant Techniques for Problem Solving and Visual Thinking Rating: 5 out of 5 stars5/52022 Adobe® Premiere Pro Guide For Filmmakers and YouTubers Rating: 5 out of 5 stars5/5Trend Following: Learn to Make a Fortune in Both Bull and Bear Markets Rating: 5 out of 5 stars5/5Experts' Guide to Snagit 2021 Rating: 5 out of 5 stars5/52024 – 2025 Newbies Guide to UI/UX Design Using Figma Rating: 0 out of 5 stars0 ratingsStatistical Analysis with R For Dummies Rating: 0 out of 5 stars0 ratingsYouTube Channels For Dummies Rating: 3 out of 5 stars3/5Mastering Python for Finance Rating: 5 out of 5 stars5/5Visualizing Financial Data Rating: 0 out of 5 stars0 ratings
Reviews for Learning Apache Thrift
0 ratings0 reviews
Book preview
Learning Apache Thrift - Rakowski Krzysztof
Table of Contents
Learning Apache Thrift
Credits
About the Author
About the Reviewer
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introducing Apache Thrift
Distributed systems and their services
Service-oriented architecture
Distributed systems
Maintainability
Scalability
Testability
An introduction to Apache Thrift
Supported programming languages
Data types
Transports
Protocols
Versioning
Security
Interface description language
Apache Thrift and others
Custom protocols
XML-RPC and JSON-RPC
SOAP and WSDL
RESTful APIs
CORBA
Apache Avro
Protocol Buffers
When to choose Apache Thrift
Summary
2. Installing and Running Apache Thrift
Installing Apache Thrift on Linux
Installation requirements
Installing dependencies
Installing dependencies on CentOS
Installing dependencies on Debian and Ubuntu
Installing Apache Thrift
Installing Apache Thrift on Mac OS X
Installing Apache Thrift
Installing Apache Thrift on Windows
Testing the installation
Summary
3. Running Your First Apache Thrift Service and Client
Creating necessary project files
Creating a local copy of the Apache Thrift libraries
Defining our first service and generating files
The service code in PHP
The client code in Python
Running the code
What really happened?
Analyzing the code
The service description – IDL
The server script – PHP
The client script – Python
Summary
4. Understanding How Apache Thrift Works
Prepare your tools
Apache Thrift's architecture
Going about using the tool
Designing the services
Preparing the interface description
Generating service and client libraries
Implementing services and clients
Running server and clients
The network stack
Transport
Protocol
Processor
Server and client
Example
Apache Thrift's type system
Basic types
Special types
Structs
Unions
Containers
list
set
map
Usage of containers
Enums
Exceptions
Services
IDL syntax
Comments
Document
Headers
Thrift include
C++ include
Namespace
Definitions
const
typedef
Summary
5. Generating and Running Code in Different Languages
PHP
Generating the code
Examining the code
Transports
Protocols
Servers
Implementing and running the service
Implementing and running the client
Java
Generating the code
Examining the code
Transports
Protocols
Servers
Implementing and running the service
Implementing and running the client
Python
Generating the code
Examining the code
Transports
Protocols
Servers
Building the libraries
Implementing and running the service
Implementing and running the client
JavaScript
Generating the code
Examining the code
Transport, protocol, and servers
Implementing and running the client
Ruby
Generating the code
Examining the code
Transports
Protocols
Servers
Implementing and running the service
Implementing and running the client
C++
Generating the code
Examining the code
Transports
Protocols
Servers
Implementing and running the service
Implementing and running the client
Summary
6. Handling Errors in Apache Thrift
What are the type of errors that can occur?
Syntax errors
Runtime errors
Logic errors
What are exceptions and how to handle them?
Handling exceptions in Apache Thrift
An example code
Implementing the divide method
Running the application without error handling
Adding error handling to the server
Adding error handling to the client
Advanced error handling
Summary
7. An Example Client-Server Application
Our example application
Planning out your work
Getting a general idea of the example application
A technical overview of the application
get_distance
find_occurences
save_to_log
The server
Clients
Preparing the Apache Thrift document
The basic toolbox – base.thrift
The MyToolbox service – mytoolbox.thrift
Compiling the IDL files
Implementing the server
Imports
Displaying errors on the console (logger)
Implementing service methods
Creating the server
Running the server
Implementing and running clients
Creating a client in PHP
Creating a client in Ruby
Further testing and other exercises
Summary
8. Advanced Usage of Apache Thrift
Apache Thrift in production
Code version control systems
Code deployment
Apache Thrift versioning
Apache Thrift performance
Comparing Java servers
Comparing C++ servers
Service multiplexing
Security issues
General security tips
Transport Layer Security/Secure Sockets Layer
Generating keystores
Using keystores in the Java code
Real-world examples of the usage of Apache Thrift
FBThrift in Facebook
Apache Thrift in Evernote
Apache Thrift in Twitter
Apache Thrift in other companies
Summary
Index
Learning Apache Thrift
Learning Apache Thrift
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: December 2015
Production reference: 1181215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-274-6
www.packtpub.com
Credits
Author
Krzysztof Rakowski
Reviewer
Faisal Rahman
Commissioning Editor
Dipika Gaonkar
Acquisition Editor
Rahul Nair
Content Development Editor
Mehvash Fatima
Technical Editor
Ankita Thakur
Copy Editor
Sonia Cheema
Project Coordinator
Milton Dsouza
Proofreader
Safis Editing
Indexer
Hemangini Bari
Graphics
Jason Monteiro
Production Coordinator
Nilesh Mohite
Cover Work
Nilesh Mohite
About the Author
Krzysztof Rakowski has 13 years of professional experience in IT as a team leader, software developer and architect, and agile project manager. During the course of his career, he has helped major global brands establish their online presence using scalable, fault-tolerant, and high-performance systems. His broad experience comes from various industries, including interactive advertising, banking, retail, and e-commerce. He is a recognized expert, Zend Certified Engineer, and a Professional Scrum Master.
Currently, Krzysztof works for the largest online shop in central and eastern Europe—where he is responsible for supervising teams of software engineers and project managers who pair the smartest IT solutions with the best customer experience.
He enjoys sharing his knowledge through articles and presentations. He occasionally writes about his side projects on his website at www.rakowski.pro.
In his free time, Krzysztof likes to travel around the world with his wife, go snowboarding, or read a good book.
I would like to thank my wife, Anna, for her constant support, encouragement, and patience. I also want to thank my parents, parents-in-law, and brother for inspiring me to reach my goals.
This book wouldn't be possible without the generous support of the friendly people at Packt Publishing.
About the Reviewer
Faisal Rahman is a developer, writer, mentor, and tech enthusiast. His passion extends from architecting secure, scalable, and maintainable software to finding optimal algorithms and data structures for the smallest problems in a system. His research on optimization algorithms for known mathematical problems has been published in reputed journals. He is currently working as a software engineer at Microsoft.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
I dedicate my work on this book to my son, Ignacy, who will be born as this book goes into print.
Preface
In 2007, Facebook's engineers needed to integrate the various applications powering their website. As their engineering culture encouraged selecting the best tools for a task without imposing strict rules regarding the choice of technology, their applications were written in a wide spectrum of different programming languages, which were considered the best for the given task.
Looking for the best solution to fulfill their needs, the engineers reviewed lots of different frameworks that were already available on the market. None of them was deemed sufficient in terms of performance or flexibility. They made a decision to develop their own solution, which became a standard to integrate all the services on Facebook.
As they considered their solution to be exceeding the current standards of the market, they released their code to the open source community, passing the task of maintaining their work on the project to the Apache Software Foundation. Since then, Apache Thrift has been developed by a large group of volunteers.
Now you can use Apache Thrift as a tool to expose your own services that are written in different languages and make your applications communicate with each other. Regardless of whether you intend to work on a small-scale application or huge enterprise, Apache Thrift may be one of the best tools for you.
In Learning Apache Thrift, you will find an introduction to various concepts of the services around you and some service-oriented architecture (SOA). Then you will learn how to use Apache Thrift in various projects. We will discuss advanced concepts too to see how the giants of the industry use this framework, and you will get some solid advice and much needed inspiration.
What this book covers
Chapter 1, Introducing Apache Thrift, gives you basic information about the environment where services are needed. You will learn about the history of Apache Thrift and its position in the market. This chapter provides some solid understanding of the context in which Apache Thrift exists.
Chapter 2, Installing and Running Apache Thrift, provides you with a quick tutorial that will allow you to have Apache Thrift up and running on your machine in no time. Instructions for Linux (Debian and CentOS), Windows, and Mac OS X are included.
Chapter 3, Running Your First Apache Thrift Service and Client, gives you the ability to see Apache Thrift in action. Simple instructions will get you through the process of setting up a server and client that run in two different programming languages (PHP and Python) and communicate with each other.
Chapter 4, Understanding How Apache Thrift Works, provides you with real knowledge of the framework's internals. You will learn about its components, network stacks, data types, interface description language (IDL), and the programming languages that are supported. You will also find out about its limitations and how to deal with them. This chapter is essential to understand the concept of under the hood
, and how to design your own Apache Thrift-supported services.
Chapter 5, Generating and Running Code in Different Languages, provides you with a toolbox of essential information about different popular programming languages and how you can use them with Apache Thrift. You may read it from the beginning to the end or just focus on those languages that interest you. The same example is used for every language, so you can easily compare the server's and client's implementation for each of them.
Chapter 6, Handling Errors in Apache Thrift, gives you information on how to deal with undesirable situations that may occur when you run your service or client. Handling errors is an important part of any programming project, and is especially essential when dealing with cross-platform applications where errors occur frequently due to the nature of the distributed architecture.
Chapter 7, An Example Client-Server Application, gathers knowledge from the whole book into one example client-server application. You will build the code step by step. The example touches every aspect of Apache Thrift and is a bit more complicated than what you have done until now. Three different languages will be used (PHP, Python, and Ruby).
Chapter 8, Advanced Usage of Apache Thrift, inspires you to further expand your Apache Thrift skills. You will learn how big companies use this framework, how to run your applications in production, and how to address security, performance, and scalability issues. You will be also be given access to other interesting Apache Thrift-related projects.
What you need for this book
To run the examples in this book, you will need any modern computer running Linux—CentOS or Debian (preferred)—Mac OS X, or Windows. You will also need some text editor to edit your code.
You will also need an Internet connection to download Apache Thrift and other required software on your computer.
Who this book is for
If you have some experience of developing applications in one or more languages that are 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.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: It initializes the variable x, which is an integer.
A block of code is set as follows:
int main()
{
int x = 42;
// this line will produce compilation error
x = forty two
;
return 0;
}
Any command-line input or output is written as follows:
sudo apt-get update
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: It varies from version to version, but in most cases may be found if you right-click on My Computer and choose Properties, then look for the Advanced panel and the Environment Variables… tab.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of