The serialization of Google's protocol buffers object for Java

Source: Internet
Author: User

Objective:

Protobuf is really many times faster than JSON, see the figure below.


Environment:

Win7 x64

Eclipse 4.3

protoc-2.5.0

Install package Download:

Https://code.google.com/p/protobuf/downloads/list

The 2.6+ version has been transferred to GitHub and is:

Https://github.com/google/protobuf/releases

1. Build the JAR Package

Download the PROTOBUF package and generate the Protobuf-2.5.0.jar file. Download Protoc-2.5.0-win32.zip and Protobuf-2.5.0.zip, two files are uncompressed, copy protoc.exe files from Protoc-2.5.0-win32 to protobuf-2.5.0\src directory , then go to Protobuf-2.5.0\java, execute mvn install, and the Protobuf-2.5.0.jar file can be found in the Protobuf-2.5.0\java\target directory after the compilation is complete.

2. Building the Eclipse Project

A. Protoc.exe placed under the project's root directory

B. Protobuf-java-2.5.0.jar is placed under Lib

C. Create a new folder Proto store proto file

D. Create a new file Msg.proto under the Proto folder and write the following:

Option Java_package = "Com.jamesfen.protobuf"; Option Java_outer_classname = "Personprobuf"; 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 Countryinfo {          Required String name = 1;          Required String code = 2;          Optional int32 number = 3;  }} Message AddressBook {   repeated person person = 1;}

3. Generate Protobuf Java class

Go to CD e:/git/myhadoop2.x/myhadoop2.x folder

Perform:

Protoc--java_out=./src/main/java./proto/msg.proto

in packages package com.jamesfen.protobuf; The following will find the class Personprobuf

4. Serialization of the deserialization test:

Package Com.jamesfen.protobuf;import Java.util.list;import com.google.protobuf.InvalidProtocolBufferException; Import Com.jamesfen.protobuf.personprobuf;import Com.jamesfen.protobuf.personprobuf.person;import Com.jamesfen.protobuf.personprobuf.person.phonenumber;public class Testprotobuf {/*** @param args*/public static void Main (string[] args) {//TODO auto-generated method StubPersonProbuf.Person.Builder Builder = PersonProbuf.Person.newBuilder (); Builder.setemail ("[email protected]"); Builder.setid (1); Builder.setname (" TestName "); Builder.addphone (PersonProbuf.Person.PhoneNumber.newBuilder (). Setnumber (" 131111111 "). SetType ( PersonProbuf.Person.PhoneType.MOBILE)); Builder.addphone (PersonProbuf.Person.PhoneNumber.newBuilder (). Setnumber ("011111"). SetType (PersonProbuf.Person.PhoneType.HOME)); Person person = Builder.build (), byte[] buf = Person.tobytearray (); try {person person2 = PersonProbuf.Person.parseFrom ( BUF); System.out.println (Person2.getname () + "," + Person2.getemail ()); List<phonenumber> lstphones = Person2.getphonelist (); for (PhoneNumber Phonenumber:lstphones) {System.out.println ( Phonenumber.getnumber ());}} catch (Invalidprotocolbufferexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println (BUF);}}

5. Output:

testname, [email protected]
131111111
011111
[[Email protected]

6.demo Source Download

https://github.com/Bellonor/myhadoop2.x

The serialization of Google's protocol buffers object for Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.