Structured data serialization mechanism --Protocol Buffers

 

First, what is pb agreement?

ProtoBuff is a structured data serialization and deserialization process. Its role is similar to XML, its main advantage for small data in the serialized data analysis and faster speed.

Suitable for the volume and the speed of the data sequence demanding scenarios, such as in the field of instant messaging, interact with the mobile client and the server interaction with the server desktop ratio pb more suitable protocol.

Two, pb compared with other sequences mechanism

The same serialization mechanism, pb compared to the more common XML data user interaction, json so what each characteristic?

1, json: a lightweight data-interchange format. Using the key on the way encapsulating the data . Simple to use, high readability, but can not express complex data types. General web projects, the most popular mainly json. Because the browser for json data support is very good, there are a lot of built-in functions support. 

2, xml: using a closed tag data encapsulation . In webservice most widely used, but compared to json, it's even more data redundancy because of the closure label in pairs. json using a key way to not only a certain amount of compressed data space, but also readable. 

3, protobuf: rising star, Google is the open source data format, for high-performance, data transmission scenarios required response speed. Because profobuf is binary data format and require encoding and decoding, encoding and decoding both parties must have a common proto file. The data itself is not readable. Thus obtained only after the real-readable data deserialization.

therefore:
In a scenario requires a large amount of data transmission, if a large amount of data, the selection protobuf can significantly reduce the amount of data, reducing the IO network, reducing the network transmission time consumed.

---------------------
above with reference to: https: //blog.csdn.net/u014043213/article/details/80336805

If you have decided to use pb, pb then how to use it?

Third, how to use pb?

Basic sequence of steps:

Proto defined File -> File compiled proto -> compiled file after import -> copy of each field in the message -> sequence (i.e., call SeriotoString () obtained binary string)

Deserialize basic steps:

Get in touch with a consistent serialization process of proto file -> 

Below serialization in python example to introduce the language:

1, in accordance with the requirements of the preparation of proto file format pb;

A simple message format:

{The Message Name 
    Field Name Field Type Field Rule = assigned identification numbers; 
} 
Field Rules: required (to be provided), optional (there may be 0 or 1), repeated (there may be 0 or more) 
field types: may be a standard types, enumerated types, custom message type 
assignment identification name: 1,2,3 ......

2, using pb proto compiler to compile the file:

protoc -I = $ SRC_DIR --python_out = $ DST_DIR $ SRC_DIR / your file name .proto 
You need to specify the source directory (application source directory - if you do not provide this directory, the default is the current directory), target directory (the directory generated after compiling your application code; usually $ SRC_DIR), 
as well as .proto file directory path. in this case the corresponding .py file will be generated in the specified target directory. because the framework for the preparation of our case for python, we want to generate Python classes, so use -python_out option, also have similar options to support other languages, generating C ++ classes, for example, with the option -cpp_out

3, pb importing class files compiled in the project:

4, create a class object, and the object of each field assignment

5, call SerializeToString () method of serialization.

--------------------- 
Reference:

https://developers.google.cn/protocol-buffers/docs/overview

https://www.sohu.com/a/168549441_216613

https://www.cnblogs.com/tohxyblog/p/8974763.html

Fourth, the use of a pit pb

repeated modifier modified field in a note to use point of time. At work, a situation is encountered, message rules in the field for a field is repeated, field type is a custom message type. In this scenario, when the assignment of the field, it is necessary to call custom message type fields add method initializes a new instance; then a new instance of each element assignment. E.g:

message format -

Field assignment process -

 

Guess you like

Origin www.cnblogs.com/Jing-Wang/p/10958672.html