Google Protocol Buffers

hedgebornabaloneSoftware and s/w Development

Dec 2, 2013 (3 years and 4 months ago)

64 views

Google Protocol Buffers

and

Apache Thrift

What are we looking for?


Scalable cross
-
language services


Why not JSON or XML?


XML is verbose


XML and JSON use UTF
-
8 encoding which are
larger than binary when sent across the wire.

Basic Information

Protocol Buffers


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

Thrift


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


Language Support

Protocol Buffers


C++


Java


Python



Community Supported


C#


Ruby


Perl


Objective C


Etc.

Thrift


C++


Java


Python


PHP


Ruby


Erlang


Perl


Haskell


C#


Cocoa


Smalltalk


OCaml

Primitive Types Supported

Protocol Buffers


Double


Float


Unsigned int32/64


Signed int32/64


Bool


String


Bytes


Repeated properties (lists)


Enumerations

Thrift


Bool


Byte


Signed int32/64


Double (64
-
bit floating point)


String


List<type>


Set<type>


Map<type1, type2>


Enumerations


Exceptions


Constants

Performance in Java

Protocol Buffers


Obj. Creation Time: 528ns


Serialize Time: 5227ns


Deserialize

Time:
2725
s


Serialized Size: 239 bytes

Thrift


Obj. Creation Time: 432ns


Serialize Time: 5644ns


Deserialize

Time: 2857ns


Serialized Size
: 349 bytes


Message Definition

Protocol Buffers

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;

}

Thrift

namespace java tutorial

namespace
csharp

Tutorial


enum

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

}


RPC Implementation

Protocol Buffers



Not Applicable



ZeroMQ

is a possible RPC
implementation that has
cross language support

Thrift


Cross language RPC built
into the APIs


RPC interfaces reference
Thrift types


Json

and Binary Protocols


service Calculator
extends

shared.SharedService

{

void
ping(),

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


throws
(1:InvalidOperation ouch
)

}


Object Creation


C#

Protocol Buffers

var

phoneNumberBuilder

=
PhoneNumber.CreateBuilder
();

phoneNumberBuilder.SetNumber
(number);

phoneNumberBuilder.Type

=
PhoneType.MOBILE
;

var

phoneNumber

=
phoneNumberBuilder.Build
();


Person.Builder

personBuilder

=
Person.CreateBuilder
();

personBuilder.Id

= 1;

personBuilder.Name

= "Matthew Kubicina";

personBuilder.Email

= "mkubicina@book.com";

personBuilder.AddPhone
(
phoneNumber
);

var

person =
personBuilder.Build
();


var

addressBookBuilder

=
AddressBook.CreateBuilder
();

addressBookBuilder.AddPerson
(person);

var

addressBook

=
addressBookBuilder.Build
();

Thrift

Thrift Client API


C#

Thrift Server API


C#

TServerSocket

serverTransport

= new
TServerSocket
(9090);

AddressBookService.Processor

processor = new


AddressBookService.Processor
(new
AddressBookServiceImpl
());

Args

serverArgs

= new
Args
(
serverTransport
);

TServer

server = new
TSimpleServer
(
serverArgs.processor
(processor
));

server.serve
();