|
Google Protocol Buffers 学习笔记
什么是protobuf?
protobuf全称Google Protocol Buffers,是一种语言无关、平台无关的针对结构化数据的序列化工具。 作为Java开发者比较常用的是Java的序列化,但是这个序列化方式只能在Java语言中通信,而protobuf可以实现跨语言。 另外其实我们可以使用JSON或者XML方式,但是这两种结构导致数据比较大,而protobuf它更小、更快、更简单。
如何工作?
protobuf定义了自己的语言,用户需要根据要求指定自己的文件(.proto结尾),比如如下内容:
syntax = "proto3";
message Person {
optional string name = 1;
optional int32 id = 2;
optional string email = 3;
}上面的这些就是proto规定的文件,里面有一些类型等。具体什么意思,后面一一介绍与尝试。 定义好文件后,通过proto提供的编译器进行编译,编译完毕后,会根据你选择的编译器生成不同的文件 比如选择的是java,就会生成Java文件,使用者就可以使用这些文件了。
安装protobuf 编译器
github地址:https://github.com/protocolbuffers/protobuf#protocol-compiler-installation 我选择了releases中的mac系统的包(包含所支持的所有语言)https://github.com/protocolbuffers/protobuf/releases
image-20221020093058572
下载完毕后解压,看下结构
protoc-21.8-osx-x86_64 pwd
/Users/itlab/dev-tools/protoc-21.8-osx-x86_64
protoc-21.8-osx-x86_64 tree .
.
├── bin
│ └── protoc
├── include
│ └── google
│ └── protobuf
│ ├── any.proto
│ ├── api.proto
│ ├── compiler
│ │ └── plugin.proto
│ ├── descriptor.proto
│ ├── duration.proto
│ ├── empty.proto
│ ├── field_mask.proto
│ ├── source_context.proto
│ ├── struct.proto
│ ├── timestamp.proto
│ ├── type.proto
│ └── wrappers.proto
└── readme.txtbin目录下的protoc就是编译器。
配置环境变量:
修改~/.bash_profile文件
export PROTOBUF=/Users/itlab/dev-tools/protoc-21.8-osx-x86_64
export PATH=$PROTOBUF/bin:$PATH修改后执行source ~/.bash_profile。
验证
~ protoc --version
libprotoc 3.21.8没有问题!
生成Java文件
进入之前定义的proto文件目录中
base git:(main) protoc -I=. --java_out=. person.proto-I代表输入 后面的.代表当前路径,--java_out代表使用Java输出,.代表当前路径,person.proto代表protobuf的文件。 执行完毕后就会看到生成了一个PersonOuterClass.java的文件。
文件内容如下:
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: person.proto
public final class PersonOuterClass {
private PersonOuterClass() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
public interface PersonOrBuilder extends
// @@protoc_insertion_point(interface_extends:Person)
com.google.protobuf.MessageOrBuilder {
/**
* <code>optional string name = 1;</code>
* @return Whether the name field is set.
*/
boolean hasName();
/**
* <code>optional string name = 1;</code>
* @return The name.
*/
java.lang.String getName();
/**
* <code>optional string name = 1;</code>
* @return The bytes for name.
*/
com.google.protobuf.ByteString
getNameBytes();
/**
* <code>optional int32 id = 2;</code>
* @return Whether the id field is set.
*/
boolean hasId();
/**
* <code>optional int32 id = 2;</code>
* @return The id.
*/
int getId();
/**
* <code>optional string email = 3;</code>
* @return Whether the email field is set.
*/
boolean hasEmail();
/**
* <code>optional string email = 3;</code>
* @return The emil.
*/
java.lang.String getEmail();
/**
* <code>optional string email = 3;</code>
* @return The bytes for email.
*/
com.google.protobuf.ByteString
getEmailBytes();
}
/**
* Protobuf type {@code Person}
*/
public static final class Person extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:Person)
PersonOrBuilder {
private static final long serialVersionUID = 0L;
// Use Person.newBuilder() to construct.
private Person(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private Person() {
name_ = &#34;&#34;;
email_ = &#34;&#34;;
}
@java.lang.Override
@SuppressWarnings({&#34;unused&#34;})
protected java.lang.Object newInstance(
UnusedPrivateParameter unused) {
return new Person();
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return PersonOuterClass.internal_static_Person_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return PersonOuterClass.internal_static_Person_fieldAccessorTable
.ensureFieldAccessorsInitialized(
PersonOuterClass.Person.class, PersonOuterClass.Person.Builder.class);
}
private int bitField0_;
public static final int NAME_FIELD_NUMBER = 1;
private volatile java.lang.Object name_;
/**
* <code>optional string name = 1;</code>
* @return Whether the name field is set.
*/
@java.lang.Override
public boolean hasName() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>optional string name = 1;</code>
* @return The name.
*/
@java.lang.Override
public java.lang.String getName() {
java.lang.Object ref = name_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
name_ = s;
return s;
}
}
/**
* <code>optional string name = 1;</code>
* @return The bytes for name.
*/
@java.lang.Override
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
public static final int ID_FIELD_NUMBER = 2;
private int id_;
/**
* <code>optional int32 id = 2;</code>
* @return Whether the id field is set.
*/
@java.lang.Override
public boolean hasId() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>optional int32 id = 2;</code>
* @return The id.
*/
@java.lang.Override
public int getId() {
return id_;
}
public static final int EMAIL_FIELD_NUMBER = 3;
private volatile java.lang.Object email_;
/**
* <code>optional string email = 3;</code>
* @return Whether the email field is set.
*/
@java.lang.Override
public boolean hasEmail() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>optional string email = 3;</code>
* @return The email.
*/
@java.lang.Override
public java.lang.String getEmail() {
java.lang.Object ref = email_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
email_ = s;
return s;
}
}
/**
* <code>optional string email = 3;</code>
* @return The bytes for email.
*/
@java.lang.Override
public com.google.protobuf.ByteString
getEmailBytes() {
java.lang.Object ref = email_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
email_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
private byte memoizedIsInitialized = -1;
@java.lang.Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
@java.lang.Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (((bitField0_ & 0x00000001) != 0)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 1, name_);
}
if (((bitField0_ & 0x00000002) != 0)) {
output.writeInt32(2, id_);
}
if (((bitField0_ & 0x00000004) != 0)) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 3, email_);
}
getUnknownFields().writeTo(output);
}
@java.lang.Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) != 0)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, name_);
}
if (((bitField0_ & 0x00000002) != 0)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, id_);
}
if (((bitField0_ & 0x00000004) != 0)) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, email_);
}
size += getUnknownFields().getSerializedSize();
memoizedSize = size;
return size;
}
@java.lang.Override
public boolean equals(final java.lang.Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof PersonOuterClass.Person)) {
return super.equals(obj);
}
PersonOuterClass.Person other = (PersonOuterClass.Person) obj;
if (hasName() != other.hasName()) return false;
if (hasName()) {
if (!getName()
.equals(other.getName())) return false;
}
if (hasId() != other.hasId()) return false;
if (hasId()) {
if (getId()
!= other.getId()) return false;
}
if (hasEmail() != other.hasEmail()) return false;
if (hasEmail()) {
if (!getEmail()
.equals(other.getEmail())) return false;
}
if (!getUnknownFields().equals(other.getUnknownFields())) return false;
return true;
}
@java.lang.Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
if (hasName()) {
hash = (37 * hash) + NAME_FIELD_NUMBER;
hash = (53 * hash) + getName().hashCode();
}
if (hasId()) {
hash = (37 * hash) + ID_FIELD_NUMBER;
hash = (53 * hash) + getId();
}
if (hasEmail()) {
hash = (37 * hash) + EMAIL_FIELD_NUMBER;
hash = (53 * hash) + getEmail().hashCode();
}
hash = (29 * hash) + getUnknownFields().hashCode();
memoizedHashCode = hash;
return hash;
}
public static PersonOuterClass.Person parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static PersonOuterClass.Person parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static PersonOuterClass.Person parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static PersonOuterClass.Person parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static PersonOuterClass.Person parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static PersonOuterClass.Person parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static PersonOuterClass.Person parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static PersonOuterClass.Person parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input, extensionRegistry);
}
public static PersonOuterClass.Person parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static PersonOuterClass.Person parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input, extensionRegistry);
}
public static PersonOuterClass.Person parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static PersonOuterClass.Person parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input, extensionRegistry);
}
@java.lang.Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(PersonOuterClass.Person prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
@java.lang.Override
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
}
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code Person}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:Person)
PersonOuterClass.PersonOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return PersonOuterClass.internal_static_Person_descriptor;
}
@java.lang.Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return PersonOuterClass.internal_static_Person_fieldAccessorTable
.ensureFieldAccessorsInitialized(
PersonOuterClass.Person.class, PersonOuterClass.Person.Builder.class);
}
// Construct using PersonOuterClass.Person.newBuilder()
private Builder() {
}
private Builder(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
super(parent);
}
@java.lang.Override
public Builder clear() {
super.clear();
name_ = &#34;&#34;;
bitField0_ = (bitField0_ & ~0x00000001);
id_ = 0;
bitField0_ = (bitField0_ & ~0x00000002);
email_ = &#34;&#34;;
bitField0_ = (bitField0_ & ~0x00000004);
return this;
}
@java.lang.Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return PersonOuterClass.internal_static_Person_descriptor;
}
@java.lang.Override
public PersonOuterClass.Person getDefaultInstanceForType() {
return PersonOuterClass.Person.getDefaultInstance();
}
@java.lang.Override
public PersonOuterClass.Person build() {
PersonOuterClass.Person result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
@java.lang.Override
public PersonOuterClass.Person buildPartial() {
PersonOuterClass.Person result = new PersonOuterClass.Person(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) != 0)) {
to_bitField0_ |= 0x00000001;
}
result.name_ = name_;
if (((from_bitField0_ & 0x00000002) != 0)) {
result.id_ = id_;
to_bitField0_ |= 0x00000002;
}
if (((from_bitField0_ & 0x00000004) != 0)) {
to_bitField0_ |= 0x00000004;
}
result.email_ = email_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
@java.lang.Override
public Builder clone() {
return super.clone();
}
@java.lang.Override
public Builder setField(
com.google.protobuf.Descriptors.FieldDescriptor field,
java.lang.Object value) {
return super.setField(field, value);
}
@java.lang.Override
public Builder clearField(
com.google.protobuf.Descriptors.FieldDescriptor field) {
return super.clearField(field);
}
@java.lang.Override
public Builder clearOneof(
com.google.protobuf.Descriptors.OneofDescriptor oneof) {
return super.clearOneof(oneof);
}
@java.lang.Override
public Builder setRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
int index, java.lang.Object value) {
return super.setRepeatedField(field, index, value);
}
@java.lang.Override
public Builder addRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
java.lang.Object value) {
return super.addRepeatedField(field, value);
}
@java.lang.Override
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof PersonOuterClass.Person) {
return mergeFrom((PersonOuterClass.Person)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(PersonOuterClass.Person other) {
if (other == PersonOuterClass.Person.getDefaultInstance()) return this;
if (other.hasName()) {
bitField0_ |= 0x00000001;
name_ = other.name_;
onChanged();
}
if (other.hasId()) {
setId(other.getId());
}
if (other.hasEmail()) {
bitField0_ |= 0x00000004;
email_ = other.email_;
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
onChanged();
return this;
}
@java.lang.Override
public final boolean isInitialized() {
return true;
}
@java.lang.Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
if (extensionRegistry == null) {
throw new java.lang.NullPointerException();
}
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
case 10: {
name_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000001;
break;
} // case 10
case 16: {
id_ = input.readInt32();
bitField0_ |= 0x00000002;
break;
} // case 16
case 26: {
email_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000004;
break;
} // case 26
default: {
if (!super.parseUnknownField(input, extensionRegistry, tag)) {
done = true; // was an endgroup tag
}
break;
} // default:
} // switch (tag)
} // while (!done)
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.unwrapIOException();
} finally {
onChanged();
} // finally
return this;
}
private int bitField0_;
private java.lang.Object name_ = &#34;&#34;;
/**
* <code>optional string name = 1;</code>
* @return Whether the name field is set.
*/
public boolean hasName() {
return ((bitField0_ & 0x00000001) != 0);
}
/**
* <code>optional string name = 1;</code>
* @return The name.
*/
public java.lang.String getName() {
java.lang.Object ref = name_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
name_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>optional string name = 1;</code>
* @return The bytes for name.
*/
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>optional string name = 1;</code>
* @param value The name to set.
* @return This builder for chaining.
*/
public Builder setName(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
name_ = value;
onChanged();
return this;
}
/**
* <code>optional string name = 1;</code>
* @return This builder for chaining.
*/
public Builder clearName() {
bitField0_ = (bitField0_ & ~0x00000001);
name_ = getDefaultInstance().getName();
onChanged();
return this;
}
/**
* <code>optional string name = 1;</code>
* @param value The bytes for name to set.
* @return This builder for chaining.
*/
public Builder setNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
checkByteStringIsUtf8(value);
bitField0_ |= 0x00000001;
name_ = value;
onChanged();
return this;
}
private int id_ ;
/**
* <code>optional int32 id = 2;</code>
* @return Whether the id field is set.
*/
@java.lang.Override
public boolean hasId() {
return ((bitField0_ & 0x00000002) != 0);
}
/**
* <code>optional int32 id = 2;</code>
* @return The id.
*/
@java.lang.Override
public int getId() {
return id_;
}
/**
* <code>optional int32 id = 2;</code>
* @param value The id to set.
* @return This builder for chaining.
*/
public Builder setId(int value) {
bitField0_ |= 0x00000002;
id_ = value;
onChanged();
return this;
}
/**
* <code>optional int32 id = 2;</code>
* @return This builder for chaining.
*/
public Builder clearId() {
bitField0_ = (bitField0_ & ~0x00000002);
id_ = 0;
onChanged();
return this;
}
private java.lang.Object email_ = &#34;&#34;;
/**
* <code>optional string email = 3;</code>
* @return Whether the email field is set.
*/
public boolean hasEmail() {
return ((bitField0_ & 0x00000004) != 0);
}
/**
* <code>optional string email = 3;</code>
* @return The email.
*/
public java.lang.String getEmail() {
java.lang.Object ref = email_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
email_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>optional string email = 3;</code>
* @return The bytes for email.
*/
public com.google.protobuf.ByteString
getEmailBytes() {
java.lang.Object ref = email_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
email_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>optional string email = 3;</code>
* @param value The email to set.
* @return This builder for chaining.
*/
public Builder setEmail(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
email_ = value;
onChanged();
return this;
}
/**
* <code>optional string email = 3;</code>
* @return This builder for chaining.
*/
public Builder clearEmail() {
bitField0_ = (bitField0_ & ~0x00000004);
email_ = getDefaultInstance().getEmail();
onChanged();
return this;
}
/**
* <code>optional string email = 3;</code>
* @param value The bytes for email to set.
* @return This builder for chaining.
*/
public Builder setEmailBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
checkByteStringIsUtf8(value);
bitField0_ |= 0x00000004;
email_ = value;
onChanged();
return this;
}
@java.lang.Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFields(unknownFields);
}
@java.lang.Override
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.mergeUnknownFields(unknownFields);
}
// @@protoc_insertion_point(builder_scope:Person)
}
// @@protoc_insertion_point(class_scope:Person)
private static final PersonOuterClass.Person DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new PersonOuterClass.Person();
}
public static PersonOuterClass.Person getDefaultInstance() {
return DEFAULT_INSTANCE;
}
private static final com.google.protobuf.Parser<Person>
PARSER = new com.google.protobuf.AbstractParser<Person>() {
@java.lang.Override
public Person parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
Builder builder = newBuilder();
try {
builder.mergeFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(builder.buildPartial());
} catch (com.google.protobuf.UninitializedMessageException e) {
throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(e)
.setUnfinishedMessage(builder.buildPartial());
}
return builder.buildPartial();
}
};
public static com.google.protobuf.Parser<Person> parser() {
return PARSER;
}
@java.lang.Override
public com.google.protobuf.Parser<Person> getParserForType() {
return PARSER;
}
@java.lang.Override
public PersonOuterClass.Person getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_Person_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_Person_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
java.lang.String[] descriptorData = {
&#34;\n\014person.proto\&#34;Z\n\006Person\022\021\n\004name\030\001 \001(\tH\000&#34; +
&#34;\210\001\001\022\017\n\002id\030\002 \001(\005H\001\210\001\001\022\022\n\005email\030\003 \001(\tH\002\210\001\001&#34; +
&#34;B\007\n\005_nameB\005\n\003_idB\010\n\006_emailb\006proto3&#34;
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
});
internal_static_Person_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_Person_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_Person_descriptor,
new java.lang.String[] { &#34;Name&#34;, &#34;Id&#34;, &#34;Email&#34;, &#34;Name&#34;, &#34;Id&#34;, &#34;Email&#34;, });
}
// @@protoc_insertion_point(outer_class_scope)
}消息结构说明
protobuf将定义的数据结构叫做消息,.proto文件的书写是有严格要求的,主要分为proto2和proto3两种语法,我目前就学习proto3。
定义消息类型
首先看一个官方的例子
syntax = &#34;proto3&#34;;
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}因为protobuf分为2和3两种,需要通过syntax来指定当前文件使用哪种,如果不写默认是proto2。 message关键字用于定义消息,SearchRequest是消息的名字,该消息有三个字段。
指定字段类型
三个前面的string、int32代表字段类型,protobuf定义了几种类型。
分配字段编号
字段后面的1、2、3代表的是字段的编号,一个消息内是唯一的。1-15的编号占用一个字节,16-2047的编号占用2个字节。最小字段编号是 1,最大的是 2^29 - 1。
指定字段规则
在类型之前可以使用规则标识来声明,规则标识有如下几种。 singular,这是默认规则,就是说被他修饰的字段只能出现0次或者1次。 optional,与singular类似,他有两种状态,如果该字段有值,就会被序列化,否则不会。 repeated,字段可以重复0次或者多次出现。 map,键值对类型
添加更多消息类型
一个.proto文件中,可以有多个消息,比如
syntax = &#34;proto3&#34;;
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}
message SearchResponse {
}有两个消息,一个是SearchRequest,另一个是SearchResponse
添加注释
分为单行和多行注释,属性上一般使用单行,消息体上方一般使用多行。单行使用//,多行使用/---/
/* SearchRequest represents a search query, with pagination options to
* indicate which results to include in the response. */
syntax = &#34;proto3&#34;;
message SearchRequest {
string query = 1;
int32 page_number = 2; // Which page number do we want?
int32 result_per_page = 3; // Number of results to return per page.
}类型对照表
一下表格列出了proto的类型和各种语言类型的关系。
.proto Type | Notes | C++ Type | Java/Kotlin Type[1] | Python Type[3] | Go Type | Ruby Type | C# Type | PHP Type | Dart Type | double | double | double | float | float64 | Float | double | float | double | float | float | float | float | float32 | Float | float | float | double | int32 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int | int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | long | int/long[4] | int64 | Bignum | long | integer/string[6] | Int64 | uint32 | Uses variable-length encoding. | uint32 | int[2] | int/long[4] | uint32 | Fixnum or Bignum (as required) | uint | integer | int | uint64 | Uses variable-length encoding. | uint64 | long[2] | int/long[4] | uint64 | Bignum | ulong | integer/string[6] | Int64 | sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int | sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long | int/long[4] | int64 | Bignum | long | integer/string[6] | Int64 | fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 228. | uint32 | int[2] | int/long[4] | uint32 | Fixnum or Bignum (as required) | uint | integer | int | fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | long[2] | int/long[4] | uint64 | Bignum | ulong | integer/string[6] | Int64 | sfixed32 | Always four bytes. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int | sfixed64 | Always eight bytes. | int64 | long | int/long[4] | int64 | Bignum | long | integer/string[6] | Int64 | bool | bool | boolean | bool | bool | TrueClass/FalseClass | bool | boolean | bool | string | A string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232. | string | String | str/unicode[5] | string | String (UTF-8) | string | string | String | bytes | May contain any arbitrary sequence of bytes no longer than 232. | string | ByteString | str (Python 2) bytes (Python 3) | []byte | String (ASCII-8BIT) | ByteString | string | List | 默认值
- string:默认值是空字符串
- bytes:默认值是空字节数组
- bools:默认是false
- 数字类型:默认是0.
- enums:枚举类型默认值是第一个定义的枚举值。
- 消息类型:默认值取决于具体的语言
枚举
枚举使用enum声明,必须要有值=0的枚举定义,而且必须放到枚举定义的第一行,类似如下官方代码。
syntax = &#34;proto3&#34;
enum Corpus {
CORPUS_UNSPECIFIED = 0;
CORPUS_UNIVERSAL = 1;
CORPUS_WEB = 2;
CORPUS_IMAGES = 3;
CORPUS_LOCAL = 4;
CORPUS_NEWS = 5;
CORPUS_PRODUCTS = 6;
CORPUS_VIDEO = 7;
}
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
Corpus corpus = 4;
}如果枚举中某两个或者多个的值相同,需要使用来声明别名。
syntax = &#34;proto3&#34;
enum EnumAllowingAlias {
option allow_alias = true;
EAA_UNSPECIFIED = 0;
EAA_STARTED = 1;
EAA_RUNNING = 1;
EAA_FINISHED = 2;
}
enum EnumNotAllowingAlias {
ENAA_UNSPECIFIED = 0;
ENAA_STARTED = 1;
// ENAA_RUNNING = 1; // Uncommenting this line will cause a compile error inside Google and a warning message outside.
ENAA_FINISHED = 2;
}可以看到EAA_STARTED = 1和 EAA_RUNNING = 1的值相同,他俩其实就是相同的,也可以理解为别名。
使用其他类型
一个消息里可以使用另一个消息作为字段类型
message SearchResponse {
repeated Result results = 1;
}
message Result {
string url = 1;
string title = 2;
repeated string snippets = 3;
}Result消息作为了SearchResponse的字段类型。
导入消息
如果将上面的两个消息分别放入不同的proto文件中,就得需要导入,导入使用import关键字,具体尝试下。
看如下图,两个文件的结构。
image-20221019195256890
右侧是导入,使用的是import &#34;other/Result.proto&#34;;
有的时候,可能SearchResponse.proto和Result.proto本来在同一个目录,但是Result后来被移动到了other路径下,此时,要向上面所说的那样使用import &#34;other/Result.proto&#34;; 这样已经定义好的文件要修改,麻烦。这时候可以保留移动前的文件,下图中图2的文件,内容修改一下(修改一个文件跟修改多个文件工作量还是不同的)如下图。
image-20221019195847367
此时SearchResponse.proto不需要更改,保持原样。
image-20221019200111442
使用 proto2 消息类型
可以导入proto2消息类型并在您的 proto3 消息中使用它们,反之亦然。但是,proto2 枚举不能直接在 proto3 语法中使用(如果导入的 proto2 消息使用它们也没关系)。
嵌套消息
消息可以嵌套
message SearchResponse {
message Result {
string url = 1;
string title = 2;
repeated string snippets = 3;
}
repeated Result results = 1;
}其他人如果想使用Result,则需要使用外层消息名.内层消息名。这跟Java的内部类类似。
更新消息类型
待更新。。。
序列化与发序列化
这里我新建一个protobuf-java的maven项目,需要引入依赖
创建proto消息
syntax = &#34;proto3&#34;;
package com.itlab1024.protobuf;
option java_multiple_files = true;
// 完整的包名是com.itlab1024.protobuf.proto.java
option java_package = &#34;java&#34;;
option java_outer_classname = &#34;AddressBookProto&#34;;
message Person {
optional string name = 1;
optional int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
optional string number = 1;
optional PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
message AddressBook {
repeated Person people = 1;
}使用编译器编译为java。
java git:(main) pwd
/Users/itlab/workspace/github/protobuf-tutorial/protobuf-java/src/main/java
java git:(main) protoc -I=com/itlab1024/protobuf/protos --java_out=. addressbook.proto编译结果如下:
image-20221020101102783
生成了很多java文件。
在maven pom.xml文件中引入protobuf-java等依赖
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.21.8</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.21.8</version>
</dependency>接下来在main方法中进行序列化和反序列化,
package com.itlab1024.protobuf;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.InvalidProtocolBufferException;
import com.itlab1024.protobuf.protos.java.AddressBook;
import com.itlab1024.protobuf.protos.java.Person;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) throws InvalidProtocolBufferException {
Person person = Person.newBuilder().setId(1).setEmail(&#34;itlab1024@163.com&#34;)
.setName(&#34;IT实验室&#34;)
.addPhones(Person.PhoneNumber.newBuilder().setNumber(&#34;13648886666&#34;).setType(Person.PhoneType.MOBILE).build()).build();
AddressBook addressBook = AddressBook.newBuilder().addPeople(person).build();
// 序列化
byte[] bytes = addressBook.toByteArray();
System.out.println(Arrays.toString(bytes));
// 反序列化
AddressBook book = AddressBook.parseFrom(bytes);
List<Person> peoples = book.getPeopleList();
for (Person people : peoples) {
System.out.println(&#34;姓名:&#34; + people.getName());
System.out.println(&#34;邮箱:&#34; + people.getEmail());
System.out.println(&#34;手机号个数:&#34; + people.getPhonesCount());
}
}
}运行结果如下,可以看到序列化和反序列化的结果是能够正确打印出来的
image-20221020103621897
勾搭交流
- Github https://github.com/itlab1024
- 知乎 https://www.zhihu.com/people/xpp1109
- Java语言交流群
- 稀土掘金:https://juejin.cn/user/1473775002718264
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|