T - the Protocol Buffers Message handled by this Coder.public class ProtoCoder<T extends com.google.protobuf.Message> extends CustomCoder<T>
Coder using Google Protocol Buffers binary format. ProtoCoder supports both
Protocol Buffers syntax versions 2 and 3.
To learn more about Protocol Buffers, visit: https://developers.google.com/protocol-buffers
ProtoCoder is registered in the global CoderRegistry as the default
Coder for any Message object. Custom message extensions are also supported, but
these extensions must be registered for a particular ProtoCoder instance and that
instance must be registered on the PCollection that needs the extensions:
import MyProtoFile;
import MyProtoFile.MyMessage;
Coder<MyMessage> coder = ProtoCoder.of(MyMessage.class).withExtensionsFrom(MyProtoFile.class);
PCollection<MyMessage> records = input.apply(...).setCoder(coder);
ProtoCoder supports both versions 2 and 3 of the Protocol Buffers syntax. However,
the Java runtime version of the google.com.protobuf library must match exactly the
version of protoc that was used to produce the JAR files containing the compiled
.proto messages.
For more information, see the Protocol Buffers documentation.
ProtoCoder and DeterminismIn general, Protocol Buffers messages can be encoded deterministically within a single pipeline as long as:
map
fields..proto file JAR.ProtoCoder and Encoding StabilityWhen changing Protocol Buffers messages, follow the rules in the Protocol Buffers language
guides for
proto2
and
proto3
syntaxes, depending on your message type. Following these guidelines will ensure that the
old encoded data can be read by new versions of the code.
Generally, any change to the message type, registered extensions, runtime library, or compiled proto JARs may change the encoding. Thus even if both the original and updated messages can be encoded deterministically within a single job, these deterministic encodings may not be the same across jobs.
Coder.Context, Coder.NonDeterministicException| Modifier and Type | Method and Description |
|---|---|
T |
decode(InputStream inStream) |
T |
decode(InputStream inStream,
Coder.Context context) |
void |
encode(T value,
OutputStream outStream) |
void |
encode(T value,
OutputStream outStream,
Coder.Context context) |
boolean |
equals(Object other) |
static CoderProvider |
getCoderProvider()
|
Set<Class<?>> |
getExtensionHosts() |
com.google.protobuf.ExtensionRegistry |
getExtensionRegistry()
Returns the
ExtensionRegistry listing all known Protocol Buffers extension messages
to T registered with this ProtoCoder. |
Class<T> |
getMessageType()
Returns the Protocol Buffers
Message type this ProtoCoder supports. |
int |
hashCode() |
static <T extends com.google.protobuf.Message> |
of(Class<T> protoMessageClass)
Returns a
ProtoCoder for the given Protocol Buffers Message. |
static <T extends com.google.protobuf.Message> |
of(TypeDescriptor<T> protoMessageType)
|
void |
verifyDeterministic() |
ProtoCoder<T> |
withExtensionsFrom(Class<?>... moreExtensionHosts)
|
ProtoCoder<T> |
withExtensionsFrom(Iterable<Class<?>> moreExtensionHosts)
Returns a
ProtoCoder like this one, but with the extensions from the given classes
registered. |
getCoderArgumentsconsistentWithEquals, getEncodedElementByteSize, getEncodedTypeDescriptor, isRegisterByteSizeObserverCheap, registerByteSizeObserver, structuralValue, verifyDeterministic, verifyDeterministicpublic static <T extends com.google.protobuf.Message> ProtoCoder<T> of(Class<T> protoMessageClass)
ProtoCoder for the given Protocol Buffers Message.public static <T extends com.google.protobuf.Message> ProtoCoder<T> of(TypeDescriptor<T> protoMessageType)
public ProtoCoder<T> withExtensionsFrom(Iterable<Class<?>> moreExtensionHosts)
ProtoCoder like this one, but with the extensions from the given classes
registered.
Each of the extension host classes must be an class automatically generated by the
Protocol Buffers compiler, protoc, that contains messages.
Does not modify this object.
public ProtoCoder<T> withExtensionsFrom(Class<?>... moreExtensionHosts)
withExtensionsFrom(Iterable).
Does not modify this object.
public void encode(T value, OutputStream outStream) throws IOException
encode in class Coder<T extends com.google.protobuf.Message>IOExceptionpublic void encode(T value, OutputStream outStream, Coder.Context context) throws IOException
encode in class Coder<T extends com.google.protobuf.Message>IOExceptionpublic T decode(InputStream inStream) throws IOException
decode in class Coder<T extends com.google.protobuf.Message>IOExceptionpublic T decode(InputStream inStream, Coder.Context context) throws IOException
decode in class Coder<T extends com.google.protobuf.Message>IOExceptionpublic void verifyDeterministic()
throws Coder.NonDeterministicException
verifyDeterministic in class CustomCoder<T extends com.google.protobuf.Message>Coder.NonDeterministicExceptionpublic Class<T> getMessageType()
Message type this ProtoCoder supports.public com.google.protobuf.ExtensionRegistry getExtensionRegistry()
ExtensionRegistry listing all known Protocol Buffers extension messages
to T registered with this ProtoCoder.public static CoderProvider getCoderProvider()
CoderProvider which uses the ProtoCoder for
proto messages.
This method is invoked reflectively from DefaultCoder.
Copyright © 2016–2017 The Apache Software Foundation. All rights reserved.