Google Protocol Buffers Summary (b)

Source: Internet
Author: User

ProtobufWhat is it

protobuf, full name Google protocol buffer,Google 's internal mixed-language data standard for RPC Systems and continuous data storage systems

It is a lightweight and efficient structured data storage format, can be used for structured data serialization, or serialization, suitable for data storage or RPC Data Interchange Format, can be used in communication protocols, data storage and other fields of language-independent, platform-Independent, Extensible serialization Structure data format . Currently available in C + +,Java,Python three languages API

Why to use protobuf

Referring to Protobuf, the general will be compared with XML ,protobuf Advantages:

style= Font-family:calibri ">xml" Style= "Font-family:calibri" >protobuf smaller, faster, simpler , you can customize the data structure, Generate readable code with code generator, plus protobuf supports backward compatible , does not have to break the old data format of the program, you can upgrade, do not worry about the message structure changes caused by large-scale code refactoring or migration problems; protobuf semantics are clearer , there is nothing like xml parser, protobuf data for serialization and deserialization operations; protobuf No need to learn complex Document object models, protobuf easy to learn , with good documentation and examples to get started quickly.

protobuf Disadvantages:

XML compared to protobuf to mature, has become the industry standard,protobuf is just Google internal use of tools, poor versatility

protobuf The binary message is generated after serialization, unless there is a . Proto definition, the content of the protobuf cannot be read directly , and the XML with some degree of self-explanatory, can be directly read by the editor;Protobuf does not apply text-based markup document (HTML) modeling

How to use protobuf ProtobufSimple example

Figure 1

         Part:messageKey WordsSimilarJavaIn theclasskeywords;HellowworldMessage NameSimilarJavaThe class name in the;required/optional/repeatedis a qualifier,Requiredbefore serialization or deserialization, the field cannot be empty, amessagemust have at least oneRequiredQualifying Fields;OptionalAndrepeatedThere is no such limit, whereOptionalindicates optional, you can set the default value, amessageallowed in0one or moreOptionalQualifying Fields(See figure2If not set, the serialization is if the field contents are empty, then depending on the type, if the type isInt32/int64Zemer that0, if the type isstringThe default is an empty string, if the type isBooleanZemer thatfalse;repeatedused to represent arrays,repeatedthe defined fields allow0one or more data;int32/int64/stringand other types areProtobufthe basic data type in

Figure 2

Protobuf Supports message nesting, supports enumeration, supports a message as another message the field Properties ,3

Figure 3

How to implement a different. ProtoFile,messageWhat about the public? Can be asJavaGenerally, the introductionImportkeywords, such asA.protoFile inProPath, and there is a name ofCommonOfmessage,B.protoFile to quoteCommon, you need the file's Package(Protobufthe package name, after serialization, generates the correspondingJavathe package name of the file,C + +of the filenamespaceUndermessagePrevious references:import "Pro/a.proto"

protobuf Basic data types with Java and C + + comparison 4

Figure 4

ProtobufOfOptions

Protocol buffer allows us to define some common options in the . Proto file, which instructs the Protocol buffer compiler to help us generate a more matching target language code. Protocol Buffer The built-in options are divided into the following three levels:
1. file level, such options will affect all messages and enumerations defined in the current file.

2. message level, such options affect only a message and all fields it contains.

3. field level, such that the option responds only to fields related to it.

The file-level options are:

      Java_package - lets you generate java the package name for this option value by specifying this option, for example [optionjava_package= "Com.hik.web.proto"] , the build java file package is under com.hik.web.proto , if this option is not specified, the package name for java is package The name specified by the keyword, this option is valid only for the build java code

    Java_outer_classname - This option specifies the generated java class name, for example: [optionjava_outer_classname=" Hpp "] , file is named proto file, and the name is converted to camel mode, such as .proto file is named my_proto.proto , The resulting java file is named myproto.java , This option is only valid for java code

optimize_for-protocol Buffer defines three levels of optimization speed/code_size/lite_runtime. Speed is the default . speed indicates that the generated code runs efficiently, but the generated code consumes more space after compiling;code_size , in contrast to speed, generates code that runs less efficiently, but generates less space after compilation, For platforms with limited resources, such as mobile,lite_runtime generated code is highly efficient and generates less space after compilation, which is done by sacrificing Protobuf Provided by the reflection function implemented, using this option,Java or C + + requires Protobuf-lite related packages or connections

The field-level options are:

packed=true-- for numeric repeated fields, such as int32,Int64 , etc., are not well optimized for coding, however in recent versions of the Protocol buffer , you can notify Protocol buffer to be more efficient when encoding a message object of that type by adding the [packed=true] field option (The Option is only valid for protobuf2.3.0 or later)

Default=default_value- This is used primarily for the optional field to set default values, which are described above

ProtobufCommand line

Figure 5

Protoc-- Command-line compilation tool

--proto_path is equivalent to-I, which specifies the proto file directory to compile, which can specify multiple

--java_out is compiled into a Java file and specifies the directory in which Java files are generated, as well as cpp_out generate C + + files,python _out generating python files

Path/file.proto represents the proto file to be compiled

You can write a protoc.bat file with content 6:

Google Protocol Buffers Summary (b)

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.