Java Getting started with protocol buffers four steps

Source: Internet
Author: User

Protocol buffers (abbreviated as PROTOBUF) is a Google technology. Used to serialize, deserialize, and format structured data. Often used for network transmission.

The goods are actually similar to XML generation and parsing. But Protobuf is more efficient than XML, but PROTOBUF generates bytecode, which is less readable than XML. Similar to JSON, Java serializable and so on.

PROTOBUF supports a variety of languages. This article takes Java as an example. Introduction Protobuf how to use.

Other language usages are similar.

First you need to download:

http://download.csdn.net/download/xiao__gui/7586617

After decompression there are two files: Protobuf-java-2.5.0.jar and Protoc.exe.


Protobuf-java-2.5.0.jar is the jar package required by PROTOBUF. Suppose that the file can be ignored by Maven;

Protoc.exe is the PROTOBUF code generation tool.

First step: Define the data structure

First, define the data structure of the protobuf, and write a. proto file here. This file is a bit similar to defining a class.

For example, define a person. Save the file Personmsg.proto (note that the file name is not the same as the message name inside).


Message person {//ID (required) required Int32 id = 1;//name (required) Required String name = 2;//email (optional) Optional String email = 3;// Friend (collection) repeated string friends = 4;}

The above 1, 2, 3, 4 is unique numbered tag, is a unique identifier.


The example above defines a very easy data structure and, of course, the ability to define more complex structures, which are no longer discussed and can be viewed in detail in official documents.


Step two: Protoc.exe generate Java code

Use the file Protoc.exe,cmd command line to execute:

Protoc.exe--java_out=e:\java Personmsg.proto

The input file is Personmsg.proto. This is the file that defines the data structure, the output directory is E:\java, and the Java file is generated in E:\java. Personmsg.java is generated when the command is executed successfully:


Create a project in eclipse to copy the Java files into your project. The Protobuf-java-2.5.0.jar package needs to be introduced into the project.

The assumption is that the MAVEN project adds:

<dependency><groupid>com.google.protobuf</groupid><artifactid>protobuf-java</ Artifactid><version>2.5.0</version></dependency>

Step Three: Serialization
Fourth step: Deserialization

Generally speaking. Serialization and deserialization are separate. For example, network transmission, by one side of the data is serialized and sent to another party to receive and parse. Serialization of send and receive deserialization is not together. But the following is a simple example of writing both in the same program for the sample.

Import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.util.list;public class Main {public static void main (string[] args) throws IOException {//According to the defined data structure, create a Personperso NMsg.Person.Builder Personbuilder = PersonMsg.Person.newBuilder ();p Ersonbuilder.setid (1);p ersonbuilder.setname (" Fork brother ");p Ersonbuilder.setemail (" [email protected] ");p ersonbuilder.addfriends (" Friend A "); Personbuilder.addfriends ("Friend B"); Personmsg.person xxg = Personbuilder.build ()//writes data to an output stream, such as a network output stream, where Bytearrayoutputstream is used instead of Bytearrayoutputstream Output = new Bytearrayoutputstream (), Xxg.writeto (output),//--------------Cut line: Above is the sender, the data is serialized and sent---------------byte[ ] ByteArray = Output.tobytearray ();//--------------Cutting line: The following is the receiver, the data received after the deserialization---------------//received stream and read, such as the network input stream, Here the Bytearrayinputstream is used instead of bytearrayinputstream input = new Bytearrayinputstream (byteArray);//Deserialization Personmsg.person Xxg2 = PersonMsg.Person.parseFrom (input); System.out.println ("ID:" + Xxg2.getid()); System.out.println ("Name:" + xxg2.getname ()); System.out.println ("Email:" + xxg2.getemail ()); System.out.println ("friend:"); List<string> friends = Xxg2.getfriendslist (); for (String friend:friends) {System.out.println (Friend);}}}


Fork Brother reproduced please indicate the source: http://blog.csdn.net/xiao__gui/article/details/36643949



Java Getting started with protocol buffers four steps

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.