Serializing Data with Protocol Buffers. Vinicius Vielmo Cogo Smalltalks, DI, FC/UL. February 12, 2014.



Similar documents
CS 378 Big Data Programming. Lecture 9 Complex Writable Types

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. AVRO Tutorial

MarkLogic Server. Java Application Developer s Guide. MarkLogic 8 February, Copyright 2015 MarkLogic Corporation. All rights reserved.

SSC - Communication and Networking Java Socket Programming (II)

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Monitoring Android Apps using the logcat and iperf tools. 22 May 2015

Java Web Services Training

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

Getting Started with the Internet Communications Engine

AVRO - SERIALIZATION

Network-based Applications. Pavani Diwanji David Brown JavaSoft

This documentation is made available before final release and is subject to change without notice and comes with no warranty express or implied.

Flash and Python. Dynamic Object oriented Rapid development. Flash and Python. Dave Thompson

WEB SERVICES. Revised 9/29/2015

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Socket Programming in Java

MongoDB. Or how I learned to stop worrying and love the database. Mathias Stearn. N*SQL Berlin October 22th, gen

Socket Programming. Announcement. Lectures moved to

Intranet, Extranet, Firewall

N CYCLES software solutions. XML White Paper. Where XML Fits in Enterprise Applications. May 2001

Web. Services. Web Technologies. Today. Web. Technologies. Internet WWW. Protocols TCP/IP HTTP. Apache. Next Time. Lecture # Apache.

PYTHON IN A NUTSHELL. O'REILLY Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo. Alex Martelli. Second Edition

MD Link Integration MDI Solutions Limited

How to develop your own app

XML Processing and Web Services. Chapter 17

Apache Thrift and Ruby

Network Communication

JAXB: Binding between XML Schema and Java Classes

Everything is Terrible

Assignment 4 Solutions

Web Pages. Static Web Pages SHTML

DESIGN AND IMPLEMENTATION OF A FILE SHARING APPLICATION FOR ANDROID

High Performance XML Data Retrieval

Developing Java Web Services

Python 2 and 3 compatibility testing via optional run-time type checking

2013 Ruby on Rails Exploits. CS 558 Allan Wirth

How To Use Query Console

XMLVend Protocol Message Validation Suite

JVA-561. Developing SOAP Web Services in Java

Grandstream Networks, Inc.

HP Operations Orchestration Software

The BSN Hardware and Software Platform: Enabling Easy Development of Body Sensor Network Applications

Network Programming using sockets

Programming IoT Gateways With macchina.io

Systems Integration: Co C mp m onent- t bas a e s d s o s ftw ft a w r a e r e ngin i eeri r n i g

Develop PHP mobile apps with Zend Framework

Data Communication & Networks G

Login with Amazon. Getting Started Guide for Websites. Version 1.0

Application Note AN0008. Data Logging Extension. For. Venus 8 GPS Receiver

ModScan A SCADA MODBUS Network Scanner. Mark Bristow mark.bristow@gmail.com

Introduction to Synoptic

Leveraging the Eclipse TPTP* Agent Infrastructure

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Log Management with Open-Source Tools. Risto Vaarandi SEB Estonia

SURF HMP getting started guide

LabVIEW Internet Toolkit User Guide

Smooks Dev Tools Reference Guide. Version: GA

Library ModbusRTUlib Modbus RTU master communication. TXV rd Issue February 2010 All rights reserved

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Introducing Apache Pivot. Greg Brown, Todd Volkert 6/10/2010

INPUT AND OUTPUT STREAMS

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Affdex SDK for Windows!

Caml Virtual Machine File & data formats Document version: 1.4

PROFESSIONAL. Node.js BUILDING JAVASCRIPT-BASED SCALABLE SOFTWARE. Pedro Teixeira WILEY. John Wiley & Sons, Inc.

Network Programming. Writing network and internet applications.

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

XML & Databases. Tutorial. 2. Parsing XML. Universität Konstanz. Database & Information Systems Group Prof. Marc H. Scholl

SSL VPN. Virtual Private Networks based on Secure Socket Layer. Mario Baldi. Politecnico di Torino. Dipartimento di Automatica e Informatica

pyownet Documentation

Accessing Data with ADOBE FLEX 4.6

Client-server Sockets

ITS. Java WebService. ITS Data-Solutions Pvt Ltd BENEFITS OF ATTENDANCE:

Understanding Evolution's Architecture A Technical Overview

UNIX Sockets. COS 461 Precept 1

Onset Computer Corporation

ICE Trade Vault. Public User & Technology Guide June 6, 2014

Attacking MongoDB. Firstov Mihail

Java Network. Slides prepared by : Farzana Rahman

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

Web development... the server side (of the force)

JobScheduler Web Services Executing JobScheduler commands

Beyond The Web Drupal Meets The Desktop (And Mobile) Justin Miller Code Sorcery Workshop, LLC

Log Management with Open-Source Tools. Risto Vaarandi rvaarandi 4T Y4H00 D0T C0M

JAVA - FILES AND I/O

CSE 403. Performance Profiling Marty Stepp

My First TTCN-3 Project with TTworkbench

ASM 4.0 A Java bytecode engineering library. Eric Bruneton

The Java Sound Internet Phone

Internationalization & Pseudo Localization

PTSv2 in pills: The Best First for Beginners who want to become Penetration Testers. Self-paced, online, flexible access

Virtual Office. Technical Requirements. Version 3.1. Revision 1.0

WEB APPLICATION DEVELOPMENT. UNIT I J2EE Platform 9

Transcription:

Serializing Data with Protocol Buffers Vinicius Vielmo Cogo Smalltalks, DI, FC/UL. February 12, 2014.

Problem statement App2 App1 Storage 2 / 19

Problem statement App2 App1 Correct object Efficient (time and space) Storage Language interoperable Easy to use 2 / 19

Context Data serialization (in Java): 1. Java built-in serialization Ex.: writeobject/readobject(aobject) Easy to use, but inefficient in terms of space (extra fields) No language interoperability 2. Manual binary encoding Ex.: 4 ints as - and aobject.getbytes() Space efficient, but time efficiency depends on parsing methods Difficult for complex objects Language interoperable 3. Human-readable formats Ex.: Using XML, JSON, DOM, SAX, STAX, JAXB, JAXP, etc. Inefficient (space and time w/ human readable format) Language interoperable 3 / 19

Protocol Buffers Protocol Buffers (Protobuf) is a solution to data serialization: A description language A compiler A library Easy-to-use, efficient automatic binary encoding Created by and in production at Google Inc. Publicly launched in 2008. Language interoperable: Officially: Java, C++, and Python Unofficially: C, C#, Erlang, Perl, PHP, Ruby, etc. Download and install the Protocol Buffers Available at: https://developers.google.com/protocol-buffers/ 4 / 19

Generic workload Designing objects 1 Describing objects 2 Compiling the description 3 Obtaining the generated source-code 4 Importing objects into your project 5 Instantiating objects 6 Using objects 7 5 / 19

1 Designing objects Person: Id Name Age Email Phone(s) Protocol Message: ClientId Sequence Operation Signature Sentence: Id Language Subject Predicate Verb(s) Pronoun(s) Object Protein: Id Organism Function(s) Sequence 6 / 19

2 Describing objects Person: required int32 id required string name optional string email repeated string phone Protocol Message: required int32 clientid required int32 sequence optional string operation optional string signature Sentence: required int32 id optional string language optional string subject optional string predicate repeated string verbs repeated string pronouns optional string object Protein: required int32 id optional string organism repeated string function optional string sequence Slide E1 7 / 19

2 Describing objects addressbook.proto 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; Slide E2 8 / 19

2 Describing objects addressbook.proto package tutorial; option java_package = "com.example.tutorial"; option java_outer_classname = "AddressBookProtos"; Initial configuration 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; 8 / 19

2 Describing objects addressbook.proto 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 = object message AddressBook { repeated Person person = 1; 8 / 19

2 Describing objects addressbook.proto 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; Basic fields message AddressBook { repeated Person person = 1; 8 / 19

2 Describing objects addressbook.proto 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; Enumerations message AddressBook { repeated Person person = 1; 8 / 19

2 Describing objects addressbook.proto 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; Internal objects message AddressBook { repeated Person person = 1; 8 / 19

2 Describing objects addressbook.proto 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; Default values message AddressBook { repeated Person person = 1; 8 / 19

2 Describing objects addressbook.proto 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; List of objects 8 / 19

3 Compiling the description Terminal $ protoc --java_out=$dst_dir addressbook.proto Output folder.proto fiile 9 / 19

4 Obtaining the generated source-code Terminal $ cat $DST_DIR/com/example/tutorial/AddressBookProtos.java 2478 java.lang.string[] descriptordata = { 2479 "\n\021addressbook.proto\022\010tutorial\"\332\001\n\006person" + 2480 "\022\014\n\004name\030\001 \002(\t\022\n\n\002id\030\002 \002(\005\022\r\n\005email\030\003 \001(" + 2481 "\t\022+\n\005phone\030\004 \003(\0132\034.tutorial.Person.Phone" + 2482 "Number\032M\n\013PhoneNumber\022\016\n\006number\030\001 \002(\t\022.\n" + 2483 "\004type\030\002 \001(\0162\032.tutorial.Person.PhoneType:" + 2484 "\004HOME\"+\n\tPhoneType\022\n\n\006MOBILE\020\000\022\010\n\004HOME\020\001" + 2485 "\022\010\n\004WORK\020\002\"/\n\013AddressBook\022 \n\006person\030\001 \003(" + 2486 "\0132\020.tutorial.PersonB)\n\024com.example.tutor" + 2487 "ialb\021addressbookprotos" 2488 ; 2489 com.google.protobuf.descriptors.filedescriptor.internaldescriptorassigner assigner = 2490 new com.google.protobuf.descriptors.filedescriptor.internaldescriptorassigner() { 2491 public com.google.protobuf.extensionregistry assigndescriptors( 2492 com.google.protobuf.descriptors.filedescriptor root) { 2493 descriptor = root; 2494 internal_static_tutorial_person_descriptor = 2495 getdescriptor().getmessagetypes().get(0); 2496 internal_static_tutorial_person_fieldaccessortable = new 2497 com.google.protobuf.generatedmessage.fieldaccessortable( 2498 internal_static_tutorial_person_descriptor, 2499 new java.lang.string[] { "Name", "Id", "Email", "Phone", ); 2500 internal_static_tutorial_person_phonenumber_descriptor = 2501 internal_static_tutorial_person_descriptor.getnestedtypes().get(0); 2502 internal_static_tutorial_person_phonenumber_fieldaccessortable = new 2503 com.google.protobuf.generatedmessage.fieldaccessortable( 2504 internal_static_tutorial_person_phonenumber_descriptor, 2505 new java.lang.string[] { "Number", "Type", ); 2506 internal_static_tutorial_addressbook_descriptor = 2507 getdescriptor().getmessagetypes().get(1); 2508 internal_static_tutorial_addressbook_fieldaccessortable = new 2509 com.google.protobuf.generatedmessage.fieldaccessortable( 2510 internal_static_tutorial_addressbook_descriptor, 2511 new java.lang.string[] { "Person", ); 2512 return null; 2513 2514 ; 2515 com.google.protobuf.descriptors.filedescriptor 2516.internalBuildGeneratedFileFrom(descriptorData, 2517 new com.google.protobuf.descriptors.filedescriptor[] { 2518, assigner); 2519 2520 2521 // @@protoc_insertion_point(outer_class_scope) 2522 10 / 19

5 Importing objects into your project Terminal $ cp $DST_DIR/com/example/tutorial/AddressBookProtos.java ~/workspace/tutorial/src/com/example/tutorial/ tutorial src com.example.tutorial AddressBookProtos.java MyExampleClass.java MyExampleClass.java package com.example.tutorial; import com.example.tutorial.addressbookprotos.addressbook; import com.example.tutorial.addressbookprotos.person; 11 / 19

6 Instantiating objects MyExampleClass.java Person john = Person.newBuilder().setId(12345).setName.setEmail.addPhone(Person.PhoneNumber.newBuilder().setNumber("+351 999 999 999").setType(Person.PhoneType.HOME).build()).build(); 12 / 19

7 Using objects: Storage MyStorageExample.java // Writing data to a file FileOutputStream aoutput = new FileOutputStream thefilename Person aperson = Person.newBuilder a Person aperson.writeto(aoutput); aoutput.close(); // Reading data from a file Person aperson = Person.parseFrom(new FileInputStream thefilename // Do something with the received Person 13 / 19

7 Using objects: TCP Communication MyTCPCommunicationExample.java // Server-side ServerSocket aserversocket = new ServerSocket(10000); Socket aconnection = aserversocket.accept(); Person aperson = Person.parseDelimitedFrom(aConnection.getInputStream()); // Do something with the received Person aperson.writedelimitedto(aconnection.getoutputstream()); // Client-side Person aperson = Person.newBuilder a Person Socket asocket = new Socket("127.0.0.1", 10000); aperson.writedelimitedto(asocket.getoutputstream()); Person aperson = Person.parseDelimitedFrom(aConnection. getinputstream()); // Do something with the received Person Slide E3 14 / 19

7 Using objects: UDP Communication MyUDPCommunicationExample.java // Receive a packet DatagramSocket aserversocket = new DatagramSocket(10000); byte[] areceivedata = new byte[1024]; DatagramPacket areceivepacket = new DatagramPacket(aReceiveData, areceivedata.length); aserversocket.receive(areceivepacket); ByteArrayInputStream ainput = new ByteArrayInputStream(aReceiveData); Person aperson = Person.parseDelimitedFrom(aInput); // Do something with the received Person // Send a packet DatagramSocket aclientsocket = new DatagramSocket(10001); ByteArrayOutputStream aoutput = new ByteArrayOutputStream(1024); Person aperson = Person.newBuilder a Person aperson.writedelimitedto(aoutput); byte asenddata[] = aoutput.tobytearray(); InetAddress aip = InetAddress.getLocalHost(); // or areceivepacket.getaddress(); DatagramPacket asendpacket = new DatagramPacket(aSendData, asenddata.length, aip, 10001); aclientsocket.send(asendpacket); Slide E4 15 / 19

Performance: Size Slide E5 16 / 19

Performance: Time Slide E5 17 / 19

Final remarks Protobuf focuses on: Efficiency (space and time) Language interoperability Usability It is a good alternative for built-in serialization But there are other available solutions: Avro (http://avro.apache.org/) Thrift (http://thrift.apache.org/) Kryo (https://github.com/esotericsoftware/kryo) Slide E6 18 / 19

Thank You! Vinicius Vielmo Cogo Smalltalks, DI, FC/UL. February 12, 2013.

Extra 1:.proto types See the Protobuf language guide: https://developers.google.com/protocol-buffers/docs/proto E1

Extra 2: Polymorphism message BaseType { // Reserve field numbers 100 to 199 for extensions. extensions 100 to 199; // All other field numbers are available for use here. required string name = 1; optional uint32 quantity = 2; extend BaseType { // This extension can only use numbers 100 to 199. optional float price = 100; message Cat { optional bool declawed = 1; message Dog { optional uint32 bones_buried = 1; message Animal { required float weight = 1; optional Dog dog = 2; optional Cat cat = 3; See the tutorial about Protocol Buffers Polymorphism: http://www.indelible.org/ink/protobuf-polymorphism/ E2

Extra 3: Netty and Protocol Buffers See the following Netty packages and examples: io.netty.handler.codec.protobuf io.netty.example.worldclock E3

Extra 4: To delimit or not? writeto(outputstream o) writedelimitedto(outputstream o) Serializes the message Writes the message to output No flush() No close() Writes the size of the message Serializes the message Writes the message to output parsefrom(inputstream i) parsedelimitedfrom(inputstream i) Reads the InputStream until EOF No close() Reads the size of message Reads the message data E4

Extra 5: Performance record Image = { uri: String title: String? width: Int32 height: Int32 size: Size enum Size = { SMALL, LARGE, record Media = { uri: String title: String? width: Int32 height: Int32 format: String duration: Int64 size: Int64 bitrate: Int32? persons: List<String> player: Player copyright: String? enum Player = { JAVA, FLASH, record MediaContent = { images: List<Image> media: Media See the following links: http://code.google.com/p/thrift-protobuf-compare/wiki/benchmarkingv2 https://groups.google.com/forum/#!forum/java-serialization-benchmarking E5

Extra 6: General Tips Updating a.proto file can be compatible with previous versions Prefer optional fields than required (required is forever) optional specific messages fields is a good solution for complex protocols Same object with different structure are supported through optional fields E6