Skip to content

End-to-end example of using Apache Thrift for data serialization between services

Notifications You must be signed in to change notification settings

dharmeshkakadia/thrift-data-serialization-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

thrift-data-serialization-example

Schema and example of how to use Apache Thrift for data serialization between different components, without using RPC/services.

TL;DR

Using maven locally

To compile:

mvn compile

To run:

mvn exec:java

Using Docker

Build it :

docker build -f Dockerfile -t thrift-test . 

Run it:

docker run thrift-test

Code structure

Here is the code structure in the repo:

.
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ README.md
β”œβ”€β”€ pom.xml
└── src
    └── main
        β”œβ”€β”€ java
        β”‚Β Β  └── org
        β”‚Β Β      └── example
        β”‚Β Β          └── EventLogger.java
        └── resources
            └── thrift
                └── event.thrift

Here, mainly two files are important:

  • event.thrift defines an example event schema.
  • EventLogger.java is an example client which will use the schema and create a JSON out of it and print it. Here is where you can do anything with the generated object (post this event to other service etc.), but for simplicity I am just printing the json on console.

Infrastructure files:

  • Dockerfile is uses mvn image and installs thrift into it. Dockerfile is very simple right now. It currently is not optimized and downloads maven dependencies each time, for example.
  • pom.xml is for mvn and defines code dependencies, how to compile the project etc.

This is the final generated and compiled code structure.

.
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ README.md
β”œβ”€β”€ pom.xml
β”œβ”€β”€ src
β”‚Β Β  └── main
β”‚Β Β      β”œβ”€β”€ java
β”‚Β Β      β”‚Β Β  └── org
β”‚Β Β      β”‚Β Β      └── example
β”‚Β Β      β”‚Β Β          └── EventLogger.java
β”‚Β Β      └── resources
β”‚Β Β          └── thrift
β”‚Β Β              └── event.thrift
└── target
    β”œβ”€β”€ classes
    β”‚Β Β  β”œβ”€β”€ event.thrift
    β”‚Β Β  β”œβ”€β”€ org
    β”‚Β Β  β”‚Β Β  └── example
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Event$1.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Event$EventStandardScheme.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Event$EventStandardSchemeFactory.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Event$EventTupleScheme.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Event$EventTupleSchemeFactory.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Event$_Fields.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Event.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ EventLogger.class
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ EventName.class
    β”‚Β Β  β”‚Β Β      └── State.class
    β”‚Β Β  └── thrift
    β”‚Β Β      └── event.thrift
    β”œβ”€β”€ generated-sources
    β”‚Β Β  └── thrift
    β”‚Β Β      └── org
    β”‚Β Β          └── example
    β”‚Β Β              β”œβ”€β”€ Event.java
    β”‚Β Β              β”œβ”€β”€ EventName.java
    β”‚Β Β              └── State.java
    └── maven-status
        └── maven-compiler-plugin
            └── compile
                └── default-compile
                    β”œβ”€β”€ createdFiles.lst
                    └── inputFiles.lst

About

End-to-end example of using Apache Thrift for data serialization between services

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors