见笑了,我先简单列举下这里面几个方法我简单理解理解:
1、newBuilder() new出一个实例对象
2、 Person person = builder.build();
byte[] buf = person.toByteArray();
将对象序列化为字节。 ----------为了方便传输吗? 方便了什么呢?
3、Person person2 = PersonProbuf.Person.parseFrom(buf); -----------反序列化,得到对象实例。
问题1: 这个在大数据什么场景下使用,有什么好处呢? 因为没有做过项目,所以很是盲目。
问题2: 这么盲目,有没有什么改善学习效率的方法呢? 自己搭建了一个分布式的集群环境,部署了hadoop Hbase等基本组件。
person1.proto:
[mw_shl_code=applescript,true]message PersonTestId {
// ID(必需)
required int32 id = 1;
}
[/mw_shl_code]
protoc --java_out=./src ./proto/person1.proto生成的java代码:
[mw_shl_code=java,true]// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: proto/person1.proto
public final class Person1 {
private Person1() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface PersonTestIdOrBuilder
extends com.google.protobuf.MessageOrBuilder {
// required int32 id = 1;
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
boolean hasId();
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
int getId();
}
/**
* Protobuf type {@code PersonTestId}
*/
public static final class PersonTestId extends
com.google.protobuf.GeneratedMessage
implements PersonTestIdOrBuilder {
// Use PersonTestId.newBuilder() to construct.
private PersonTestId(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private PersonTestId(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
private static final PersonTestId defaultInstance;
public static PersonTestId getDefaultInstance() {
return defaultInstance;
}
public PersonTestId getDefaultInstanceForType() {
return defaultInstance;
}
private final com.google.protobuf.UnknownFieldSet unknownFields;
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private PersonTestId(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
int mutable_bitField0_ = 0;
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 8: {
bitField0_ |= 0x00000001;
id_ = input.readInt32();
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return Person1.internal_static_PersonTestId_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return Person1.internal_static_PersonTestId_fieldAccessorTable
.ensureFieldAccessorsInitialized(
Person1.PersonTestId.class, Person1.PersonTestId.Builder.class);
}
public static com.google.protobuf.Parser<PersonTestId> PARSER =
new com.google.protobuf.AbstractParser<PersonTestId>() {
public PersonTestId parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new PersonTestId(input, extensionRegistry);
}
};
@java.lang.Override
public com.google.protobuf.Parser<PersonTestId> getParserForType() {
return PARSER;
}
private int bitField0_;
// required int32 id = 1;
public static final int ID_FIELD_NUMBER = 1;
private int id_;
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
public boolean hasId() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
public int getId() {
return id_;
}
private void initFields() {
id_ = 0;
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1;
if (!hasId()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeInt32(1, id_);
}
getUnknownFields().writeTo(output);
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(1, id_);
}
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@java.lang.Override
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static Person1.PersonTestId parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static Person1.PersonTestId parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static Person1.PersonTestId parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static Person1.PersonTestId parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static Person1.PersonTestId parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static Person1.PersonTestId parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Person1.PersonTestId parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static Person1.PersonTestId parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static Person1.PersonTestId parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static Person1.PersonTestId parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(Person1.PersonTestId prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code PersonTestId}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder>
implements Person1.PersonTestIdOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return Person1.internal_static_PersonTestId_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return Person1.internal_static_PersonTestId_fieldAccessorTable
.ensureFieldAccessorsInitialized(
Person1.PersonTestId.class, Person1.PersonTestId.Builder.class);
}
// Construct using Person1.PersonTestId.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
id_ = 0;
bitField0_ = (bitField0_ & ~0x00000001);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return Person1.internal_static_PersonTestId_descriptor;
}
public Person1.PersonTestId getDefaultInstanceForType() {
return Person1.PersonTestId.getDefaultInstance();
}
public Person1.PersonTestId build() {
Person1.PersonTestId result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public Person1.PersonTestId buildPartial() {
Person1.PersonTestId result = new Person1.PersonTestId(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.id_ = id_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof Person1.PersonTestId) {
return mergeFrom((Person1.PersonTestId)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(Person1.PersonTestId other) {
if (other == Person1.PersonTestId.getDefaultInstance()) return this;
if (other.hasId()) {
setId(other.getId());
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
public final boolean isInitialized() {
if (!hasId()) {
return false;
}
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
Person1.PersonTestId parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (Person1.PersonTestId) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
// required int32 id = 1;
private int id_ ;
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
public boolean hasId() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
public int getId() {
return id_;
}
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
public Builder setId(int value) {
bitField0_ |= 0x00000001;
id_ = value;
onChanged();
return this;
}
/**
* <code>required int32 id = 1;</code>
*
* <pre>
* ID(必需)
* </pre>
*/
public Builder clearId() {
bitField0_ = (bitField0_ & ~0x00000001);
id_ = 0;
onChanged();
return this;
}
// @@protoc_insertion_point(builder_scope:PersonTestId)
}
static {
defaultInstance = new PersonTestId(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:PersonTestId)
}
private static com.google.protobuf.Descriptors.Descriptor
internal_static_PersonTestId_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_PersonTestId_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
java.lang.String[] descriptorData = {
"\n\023proto/person1.proto\"\032\n\014PersonTestId\022\n\n" +
"\002id\030\001 \002(\005"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
internal_static_PersonTestId_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_PersonTestId_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_PersonTestId_descriptor,
new java.lang.String[] { "Id", });
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
}
// @@protoc_insertion_point(outer_class_scope)
}
[/mw_shl_code]
|