Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to serialize using Protobuf

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you how to use Protobuf serialization, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The serialization mechanism that comes with java is too inefficient and has many disadvantages. As a result, many excellent serialization frameworks have emerged, such as protobuf, protostuff, thrift, hession, kryo, avro, fst, msgpack and so on. In this article, let's take a look at protobuf and give a simple example to see how it is implemented.

Note: if you still have doubts about the concept and basic use of serialization, you can read my previous article, or some basic concepts and functions of Baidu.

First, why use protobuf?

The reason for using protobuf must be to solve some problems in development, so what's wrong with using other serialization mechanisms?

(1) java default serialization mechanism: extremely inefficient, but also can share data between languages.

(2) XML is often used to transfer or share data with other projects, but encoding and decoding will cause great performance loss.

(3) the json format is also common, but it is very time-consuming in json parsing, and the json structure takes up a lot of memory.

But our protobuf is a flexible, efficient and automated serialization mechanism that can effectively solve the above problems.

Second, how to use protobuf

Protobuf is so good to use is also very simple, we can give its three main steps, and then use a basic case to implement.

(1) define the .proto file: we describe our serialized information in this file, similar to the bean class.

(2) generate the corresponding class file according to .proto. The above proto is like a template, and now we need to create a java class based on this template.

(3) serialization.

With this basic step, let's implement it in detail:

1. Download and install protobuf

Step 1: download and decompress

My computer is windows10, so here is the use under windows, and the use under linux will be introduced in a later article. Let's first download protobuf (search protobuf on github, there are a variety of languages to choose from). Select protoc-3.9.0-win64.zip. When the download is complete, just unzip it to the D:/protobuf directory.

Step 2: configure environment variables

That is, D:\ protobuf\ protoc-3.9.0-win64\ bin is configured into the path environment variable.

Step 3: verify that the installation is successful

Finally, we can type protoc-- version into cmd to verify whether it is successful.

OK, we have installed it here.

2. Write proto files

We said earlier that the proto file is very similar to java's bean. Here we create a new Person.proto file (and proto.exe) in the bin directory. Enter the following:

Syntax= "proto3"; option java_package = "com.fdd.protobuf"; option java_outer_classname = "PersonProto"; message Person {string name = 1; int32 age = 2;}

Let's explain some of what we wrote.

(1) there is a proto3 on the first line, which represents the grammatical version of protobuf, similar to the concepts of jdk1.7 and jdk1.8. It needs to be specified on the first line. Proto2 is used by default.

(2) java_package: indicates the java package and uses package if not specified. The generated classes are placed under the package. This means to store the generated class under the com.fdd.protobuf package.

(3) java_outer_classname: we said that in the .proto file, to generate the corresponding class, this parameter specifies which class name to output. This means that the generated class name is PersonProto.

(4) message is used for data format definition.

Multiple message can be defined in a .proto file

The fields defined in message support string, byte, bool, map, enum, numeric types and user-defined message

You need to specify a unique identification number after the definition field, which is used to identify the fields in the binary format message. Once you start using this message, the identification number cannot be changed.

If you need to define List, you can add repeated to the field.

If you have already used the classes generated by the message, you can simply add fields if you want. When the class of the new field parses the old data, the new field is set to the default value. New fields are ignored when the old class parses the new data.

For example, here we define a complex proto file:

Syntax = "proto3"; option java_package = "com.fdd.protobuf"; option java_outer_classname = "Persons"; message Staff {int32 id = 1; string name = 2; int32 age = 3; / / enumerated example enum PhoneType {MOBILE = 0; TELEPHONE = 1;} / nested example message PhoneNumber {string number = 1; PhoneType type = 2 } / / list sample repeated PhoneNumber phone = 4; message Map {string key = 1; int32 value = 2;} / / map sample Map map = 5;}

Since this is only a basic case, other data types can be defined according to their own needs.

3. Generate class class files according to proto files

It's easy to compile. Under the protobuf we downloaded, there is a bin directory with a proto.exe in it. Let's just use this to compile the person.proto file.

That is, execute proto.exe-- java_out = / Person.proto and you will compile successfully.

4. Use class class files

This means that we are ready for serialization, and then we can use this class directly.

(1) step 1: introduce the generated PersonProto class into our IDEA or eclipse.

(2) step 2: add protobuf dependencies to idea or eclipse.

Com.google.protobuf protobuf-java 3.9.0

(3) step 3: use

First, let's take a look at how to serialize:

/ / 1. Create Builder PersonProto.Person.Builder builder = PersonProto.Person.newBuilder (); / / 2, set the properties of Person builder.setAge (20); builder.setName ("architect's technology stack of java"); / / 3, create Person PersonProto.Person person = builder.build (); / / 4, serialize byte [] data = person.toByteArray (); / / 5, save data locally or transfer it to the network

And then deserialize.

Try {/ / one line of code implements deserialization. Data can be local data or network data PersonProto.Person person = PersonProto.Person.parseFrom (data); System.out.println (person.getAge ()); System.out.println (person.getName ());} catch (InvalidProtocolBufferException e) {e.printStackTrace () } on how to use Protobuf serialization to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report