SlideShare a Scribd company logo
Apache Thrift:
RPC service cross languages
          2012/11/04
           Jimmy Lai
    r97922028 [at] ntu.edu.tw
Thrift Network Stack




     Thrift http://thrift.apache.org/   2
Languages Supported
•   C++                              •     Java
•   C#                               •     OCaml
•   Cocoa                            •     Perl
•   D                                •     PHP
•   Delphi                           •     Python
•   Erlang                           •     Ruby
•   Haskell                          •     Smalltalk



                   Thrift http://thrift.apache.org/    3
Interface Description Language (IDL)
  Data structure
    'bool' | 'byte' | 'i16' | 'i32' | 'i64' | 'double' |
    'string' | 'binary' | 'slist'
    map
    set
    list
• http://thrift.apache.org/docs/idl/


                      Thrift http://thrift.apache.org/     4
Usage
1. Define data structure and service interface
   using IDL(interface description language)
2. Generate language binding library:
      thrift -gen py my.thrift
3. Write Server and Client using the generated
   library. You may refer the template file in
   thrift/tutorial directory.


                  Thrift http://thrift.apache.org/   5
Example
• Scenario:
• We have a prepared machine learning model,
  and we'd like to load the model in advance as
  a server to provide prediction service.




                  Thrift http://thrift.apache.org/   6
Step 1. Define data structure and
        service: classifier.thrift
typedef list<double> Feature

service Classifier{
  i64 predict(1: Feature feature);
}




                   Thrift http://thrift.apache.org/   7
Step 2. Generate binding library
• thrift --gen py classifier.thrift
• Code binding library in gen-py dir




                  Thrift http://thrift.apache.org/   8
Step 3. Write Service and Client
• Server
  class ClassifierHandler(object):
    def __init__(self):
       self.model = pkl.load(open('svm_model.pkl', 'rb'))
       print 'loading model...'

    def predict(self, feature):
      return self.model.predict(feature)[0]




                       Thrift http://thrift.apache.org/     9
Client (1/2)
# Make socket
transport = TSocket.TSocket('localhost', 9090)

# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)

# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)

# Create a client to use the protocol encoder
client = Classifier.Client(protocol)

# Connect!
transport.open()
                     Thrift http://thrift.apache.org/   10
Client (2/2)
digits = datasets.load_digits()
data = zip(digits.data, digits.target)
random.shuffle(data)

for i in range(10):
  result = client.predict(data[i][0])
  answer = data[i][1]
  if result == answer:
      print 'Correct!'
  else:
      print 'Wrong: answer is %d, predicted as %d' % (answer,
result)

# Close!
transport.close()        Thrift http://thrift.apache.org/       11
Live Demo
• See thrift_demo directory in
  https://bitbucket.org/noahsark/slideshare/
 noahsark@localhost:~/workspace/thrift/gen-py$ ./PythonServer.py
 loading model...
 Starting the server...
 noahsark@localhost:~/workspace/thrift/gen-py$ ./PythonClient.py
 Correct!
 Correct!
 Correct!
 Correct!
 Correct!
 Correct!
 Correct!
 Wrong: answer is 8, predicted as 1
 Correct!
 Correct!                   Thrift http://thrift.apache.org/       12

More Related Content

ODP
An introduction to Apache Thrift
PDF
Apache Thrift
PDF
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
PPT
Introduction to Thrift
PPTX
Apache Thrift, a brief introduction
PDF
Apache Thrift : One Stop Solution for Cross Language Communication
PDF
3 apache-avro
PPTX
Thrift vs Protocol Buffers vs Avro - Biased Comparison
An introduction to Apache Thrift
Apache Thrift
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
Introduction to Thrift
Apache Thrift, a brief introduction
Apache Thrift : One Stop Solution for Cross Language Communication
3 apache-avro
Thrift vs Protocol Buffers vs Avro - Biased Comparison

What's hot (20)

PPTX
Facebook thrift
PPTX
Php’s guts
PDF
Fluentd meetup dive into fluent plugin (outdated)
PDF
Multiplexing in Thrift: Enhancing thrift to meet Enterprise expectations- Imp...
PPTX
Hack and HHVM
PPTX
Hack Programming Language
PDF
Rest style web services (google protocol buffers) prasad nirantar
PDF
PHPCR e API Platform: cosa significa davvero sviluppare un CMF con Symfony
PDF
DEF CON 27 - workshop - HUGO TROVAO and RUSHIKESH NADEDKAR - scapy dojo v1
PDF
Apache AVRO (Boston HUG, Jan 19, 2010)
PDF
Optimizing LAMPhp Applications
PDF
Dive into Fluentd plugin v0.12
PDF
Fluentd meetup in japan
PDF
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
PDF
JRuby with Java Code in Data Processing World
PDF
NativeBoost
PDF
Fluentd introduction at ipros
PDF
Fluentd v0.14 Plugin API Details
PDF
Fluentd and WebHDFS
PDF
Php a dynamic web scripting language
Facebook thrift
Php’s guts
Fluentd meetup dive into fluent plugin (outdated)
Multiplexing in Thrift: Enhancing thrift to meet Enterprise expectations- Imp...
Hack and HHVM
Hack Programming Language
Rest style web services (google protocol buffers) prasad nirantar
PHPCR e API Platform: cosa significa davvero sviluppare un CMF con Symfony
DEF CON 27 - workshop - HUGO TROVAO and RUSHIKESH NADEDKAR - scapy dojo v1
Apache AVRO (Boston HUG, Jan 19, 2010)
Optimizing LAMPhp Applications
Dive into Fluentd plugin v0.12
Fluentd meetup in japan
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
JRuby with Java Code in Data Processing World
NativeBoost
Fluentd introduction at ipros
Fluentd v0.14 Plugin API Details
Fluentd and WebHDFS
Php a dynamic web scripting language
Ad

Viewers also liked (20)

PPTX
Facebook thrift
PDF
Fast data mining flow prototyping using IPython Notebook
PDF
[LDSP] Solr Usage
PDF
Data Analyst Nanodegree
PDF
[LDSP] Search Engine Back End API Solution for Fast Prototyping
PDF
Illustration of TextSecure's Protocol Buffer usage
PDF
Nltk natural language toolkit overview and application @ PyCon.tw 2012
PDF
Documentation with sphinx @ PyHug
PDF
Software development practices in python
PDF
When big data meet python @ COSCUP 2012
PPTX
HBase Advanced Schema Design - Berlin Buzzwords - June 2012
PDF
Build a Searchable Knowledge Base
PPTX
Mongo DB로 진행하는 CRUD
PDF
Big data analysis in python @ PyCon.tw 2013
PPTX
Nltk natural language toolkit overview and application @ PyHug
PDF
NetworkX - python graph analysis and visualization @ PyHug
PDF
Text classification in scikit-learn
PDF
Facebook architecture
PDF
Text Classification in Python – using Pandas, scikit-learn, IPython Notebook ...
PPTX
OVERVIEW OF FACEBOOK SCALABLE ARCHITECTURE.
Facebook thrift
Fast data mining flow prototyping using IPython Notebook
[LDSP] Solr Usage
Data Analyst Nanodegree
[LDSP] Search Engine Back End API Solution for Fast Prototyping
Illustration of TextSecure's Protocol Buffer usage
Nltk natural language toolkit overview and application @ PyCon.tw 2012
Documentation with sphinx @ PyHug
Software development practices in python
When big data meet python @ COSCUP 2012
HBase Advanced Schema Design - Berlin Buzzwords - June 2012
Build a Searchable Knowledge Base
Mongo DB로 진행하는 CRUD
Big data analysis in python @ PyCon.tw 2013
Nltk natural language toolkit overview and application @ PyHug
NetworkX - python graph analysis and visualization @ PyHug
Text classification in scikit-learn
Facebook architecture
Text Classification in Python – using Pandas, scikit-learn, IPython Notebook ...
OVERVIEW OF FACEBOOK SCALABLE ARCHITECTURE.
Ad

Similar to Apache thrift-RPC service cross languages (20)

PDF
Distributed Ruby and Rails
PDF
Hands on with CoAP and Californium
PPTX
Data processing at the speed of 100 Gbps@Apache Crail (Incubating)
PPT
Building scalable and language-independent Java services using Apache Thrift ...
PPT
Building scalable and language independent java services using apache thrift
PDF
PDF
Intro to-puppet
PDF
Big Data, Data Lake, Fast Data - Dataserialiation-Formats
PPTX
CocoaConf: The Language of Mobile Software is APIs
PDF
Consuming RESTful Web services in PHP
PDF
Consuming RESTful services in PHP
PDF
(phpconftw2012) PHP as a Middleware in Embedded Systems
PDF
Web services tutorial
PDF
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
PDF
Web Services Tutorial
PPT
Apache web server installation/configuration, Virtual Hosting
PPTX
Introduction into Docker Containers, the Oracle Platform and the Oracle (Nati...
KEY
Rails web api 开发
PPTX
Introduction to Laravel Framework (5.2)
PPTX
Chicago Hashicorp User Group - Terraform Public Module Registry
Distributed Ruby and Rails
Hands on with CoAP and Californium
Data processing at the speed of 100 Gbps@Apache Crail (Incubating)
Building scalable and language-independent Java services using Apache Thrift ...
Building scalable and language independent java services using apache thrift
Intro to-puppet
Big Data, Data Lake, Fast Data - Dataserialiation-Formats
CocoaConf: The Language of Mobile Software is APIs
Consuming RESTful Web services in PHP
Consuming RESTful services in PHP
(phpconftw2012) PHP as a Middleware in Embedded Systems
Web services tutorial
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Web Services Tutorial
Apache web server installation/configuration, Virtual Hosting
Introduction into Docker Containers, the Oracle Platform and the Oracle (Nati...
Rails web api 开发
Introduction to Laravel Framework (5.2)
Chicago Hashicorp User Group - Terraform Public Module Registry

More from Jimmy Lai (9)

PDF
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
PDF
PyCon JP 2024 Streamlining Testing in a Large Python Codebase .pdf
PDF
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
PDF
Python Linters at Scale.pdf
PDF
EuroPython 2022 - Automated Refactoring Large Python Codebases
PDF
Annotate types in large codebase with automated refactoring
PDF
The journey of asyncio adoption in instagram
PDF
Distributed system coordination by zookeeper and introduction to kazoo python...
PDF
Continuous Delivery: automated testing, continuous integration and continuous...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
PyCon JP 2024 Streamlining Testing in a Large Python Codebase .pdf
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
Python Linters at Scale.pdf
EuroPython 2022 - Automated Refactoring Large Python Codebases
Annotate types in large codebase with automated refactoring
The journey of asyncio adoption in instagram
Distributed system coordination by zookeeper and introduction to kazoo python...
Continuous Delivery: automated testing, continuous integration and continuous...

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Cloud computing and distributed systems.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Cloud computing and distributed systems.
Per capita expenditure prediction using model stacking based on satellite ima...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Review of recent advances in non-invasive hemoglobin estimation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
“AI and Expert System Decision Support & Business Intelligence Systems”
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Apache thrift-RPC service cross languages

  • 1. Apache Thrift: RPC service cross languages 2012/11/04 Jimmy Lai r97922028 [at] ntu.edu.tw
  • 2. Thrift Network Stack Thrift http://thrift.apache.org/ 2
  • 3. Languages Supported • C++ • Java • C# • OCaml • Cocoa • Perl • D • PHP • Delphi • Python • Erlang • Ruby • Haskell • Smalltalk Thrift http://thrift.apache.org/ 3
  • 4. Interface Description Language (IDL) Data structure 'bool' | 'byte' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist' map set list • http://thrift.apache.org/docs/idl/ Thrift http://thrift.apache.org/ 4
  • 5. Usage 1. Define data structure and service interface using IDL(interface description language) 2. Generate language binding library: thrift -gen py my.thrift 3. Write Server and Client using the generated library. You may refer the template file in thrift/tutorial directory. Thrift http://thrift.apache.org/ 5
  • 6. Example • Scenario: • We have a prepared machine learning model, and we'd like to load the model in advance as a server to provide prediction service. Thrift http://thrift.apache.org/ 6
  • 7. Step 1. Define data structure and service: classifier.thrift typedef list<double> Feature service Classifier{ i64 predict(1: Feature feature); } Thrift http://thrift.apache.org/ 7
  • 8. Step 2. Generate binding library • thrift --gen py classifier.thrift • Code binding library in gen-py dir Thrift http://thrift.apache.org/ 8
  • 9. Step 3. Write Service and Client • Server class ClassifierHandler(object): def __init__(self): self.model = pkl.load(open('svm_model.pkl', 'rb')) print 'loading model...' def predict(self, feature): return self.model.predict(feature)[0] Thrift http://thrift.apache.org/ 9
  • 10. Client (1/2) # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Classifier.Client(protocol) # Connect! transport.open() Thrift http://thrift.apache.org/ 10
  • 11. Client (2/2) digits = datasets.load_digits() data = zip(digits.data, digits.target) random.shuffle(data) for i in range(10): result = client.predict(data[i][0]) answer = data[i][1] if result == answer: print 'Correct!' else: print 'Wrong: answer is %d, predicted as %d' % (answer, result) # Close! transport.close() Thrift http://thrift.apache.org/ 11
  • 12. Live Demo • See thrift_demo directory in https://bitbucket.org/noahsark/slideshare/ noahsark@localhost:~/workspace/thrift/gen-py$ ./PythonServer.py loading model... Starting the server... noahsark@localhost:~/workspace/thrift/gen-py$ ./PythonClient.py Correct! Correct! Correct! Correct! Correct! Correct! Correct! Wrong: answer is 8, predicted as 1 Correct! Correct! Thrift http://thrift.apache.org/ 12