thrift protobuf

12
GOOGLE PROTOCOL BUFFERS VS APACHE THRIFT

Upload: sajeevanie-mariyan-fernando

Post on 24-Oct-2014

134 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Thrift Protobuf

GOOGLE PROTOCOL BUFFERS VS

APACHE THRIFT

Page 2: Thrift Protobuf

What are we looking for?

Lightweight /Scalable cross-language services

Why not JSON or XML? XML is verbose by design

Since XML is a text format and it uses tags to delimit the data, XML files are nearly always larger than comparable binary formats

XML and JSON use UTF-8 encoding which are larger than binary when sent across the wire. Xml <?xml version="1.0" encoding="UTF-8" ?

> JSON "version": "1.0", "encoding": "UTF-8",

Page 3: Thrift Protobuf

Basic Information

Open sourced by Google Maintained by Google Serialization API Write a proto file to

define message and then use compiler to generate classes for target language

Supports Binary serialization

Open sourced by Facebook Maintained by Apache

Incubator Serialization and RPC API Write a thrift file to define

message and then use compiler to generate classes for target language

Supports Binary and JSON serialization

Protocol Buffers Thrift

Page 4: Thrift Protobuf

Language Support

C++ Java Python

Community Supported C# Ruby Perl Objective C Etc.

C++ Java Python PHP Ruby Erlang Perl Haskell C# Cocoa Smalltalk OCaml

Protocol Buffers Thrift

Page 5: Thrift Protobuf

Primitive Types Supported

Double Float Unsigned int32/64 Signed int32/64 Bool String Bytes Repeated properties (lists) Enumerations

Bool Byte Signed int32/64 Double (64-bit floating point) String List<type> Set<type> Map<type1, type2> Enumerations Exceptions Constants

Protocol Buffers Thrift

Page 6: Thrift Protobuf

Message Definition

package tutorial;

option java_package = com.example.tutorial";

option java_outer_classname = AddressBookProtos";

message Person {

required string name = 1; required int32 id = 2;

optional string email = 3;

enum PhoneType {

MOBILE = 0; HOME = 1; WORK = 2;

}

message PhoneNumber {

required string number = 1; optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

message AddressBook {

repeated Person person = 1;

}

Protocol Buffers

Page 7: Thrift Protobuf

namespace java tutorial namespace csharp Tutorialenum PhoneType {

MOBILE = 1, HOME = 2, WORK = 3}struct PhoneNumber {

1: string number, 2: PhoneType type = 2}struct Person {

1: string name, 2: i32 id, 3: string email, 4: set<PhoneNumber> phone

}struct AddressBook {

1: list<Person> person}

ThriftMessage Definition

Page 8: Thrift Protobuf

RPC Implementation

Not Applicable

ZeroMQ is a possible RPC implementation that has cross language support

Cross language RPC built into the APIs

RPC interfaces reference Thrift types

Json and Binary Protocols

service Calculator extendsshared.SharedService {

void ping(),

i32 add(1:i32 num1, 2:i32 num2)

throws (1:InvalidOperation ouch)

}

Protocol Buffers Thrift

Page 9: Thrift Protobuf

Create .thrift file and gen Java code Write java code

struct dns_record { 1: string key, 2: string value, 3: string type = 'A', 4: i32 ttl =

86400, 5: string first, 6: string last } Service TestDns { dns_record test(1:string q); }

<thrift_root>/bin/thrift –gen java tim.thrift code will be generated in gen-java/*.java

// new object dns_record dr = new dns_record(key, value, type, ttl, first, last) // serialize TSerializer serializer = new TSerializer(new

TBinaryProtocol.Factory()); TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory()); byte[] bytes = serializer.serialize(dr);

Page 10: Thrift Protobuf

Create .proto file and gen Java code Write Java code

package dns;

message DnsRecord {

required string key = 1; required string value = 2; required string first = 3; required string last = 4; optional string type = 5 [default = "A"]; optional int32  ttl = 6 [default = 86400]; }

message DnsResponse { repeated DnsRecord records = 1; }

bin/protoc –java_out . tim.proto

// protocol buffer need a builder to create object

Dns.DnsRecord.Builder b = Dns.DnsRecord.newBuilder(); b.setKey("key"); b.setValue("value...");

b.builder();

byte[] bytes = dr.toByteArray();

Dns.DnsRecord dr2 = Dns.DnsRecord.parseFrom(bytes);

Page 11: Thrift Protobuf

Performance in Java

Get object     : 14,394msec Serdes thrift  : 37,671msec Objs per second: 265,456 Total bytes    : 1,130,000,000

Get object     : 8,170 msec Serdes protobuf: 33,054

msec Objs per second: 302,535 Total bytes    :

829,997,866

Protocol Buffers Thrift

From the result, Protocol Buffers is 1.1 times faster than Thrift!

Page 12: Thrift Protobuf

Thank You

Group Members

2010/mcs/051 Saman Perera2010/mcs/037 Kamal Mettananda2010/mcs/075 Hansaka Wickramasinghe2010/mcs/007 Yasindu Dambadeniya2010/mcs/046 B S Dilshan Perera2010/mcs/056 L Prashanthan2010/mcs/068 Gayanath Senanayake2010/mcs/072 W Sriwathsan2010/mcs/076 Darshana Wijegunarathna