T - the Protocol Buffers Message handled by this Coder.public class ProtoCoder<T extends com.google.protobuf.Message>
extends org.apache.beam.sdk.coders.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.
| Modifier and Type | Field and Description |
|---|---|
static long |
serialVersionUID |
| Modifier | Constructor and Description |
|---|---|
protected |
ProtoCoder(java.lang.Class<T> protoMessageClass,
java.util.Set<java.lang.Class<?>> extensionHostClasses)
Private constructor.
|
| Modifier and Type | Method and Description |
|---|---|
T |
decode(java.io.InputStream inStream) |
T |
decode(java.io.InputStream inStream,
org.apache.beam.sdk.coders.Coder.Context context) |
void |
encode(T value,
java.io.OutputStream outStream) |
void |
encode(T value,
java.io.OutputStream outStream,
org.apache.beam.sdk.coders.Coder.Context context) |
boolean |
equals(@Nullable java.lang.Object other) |
static org.apache.beam.sdk.coders.CoderProvider |
getCoderProvider()
|
java.util.Set<java.lang.Class<?>> |
getExtensionHosts() |
com.google.protobuf.ExtensionRegistry |
getExtensionRegistry()
Returns the
ExtensionRegistry listing all known Protocol Buffers extension messages to
T registered with this ProtoCoder. |
java.lang.Class<T> |
getMessageType()
Returns the Protocol Buffers
Message type this ProtoCoder supports. |
protected com.google.protobuf.Parser<T> |
getParser()
Get the memoized
Parser, possibly initializing it lazily. |
int |
hashCode() |
static <T extends com.google.protobuf.Message> |
of(java.lang.Class<T> protoMessageClass)
Returns a
ProtoCoder for the given Protocol Buffers Message. |
static <T extends com.google.protobuf.Message> |
of(org.apache.beam.sdk.values.TypeDescriptor<T> protoMessageType)
|
void |
verifyDeterministic() |
ProtoCoder<T> |
withExtensionsFrom(java.lang.Class<?>... moreExtensionHosts)
|
ProtoCoder<T> |
withExtensionsFrom(java.lang.Iterable<java.lang.Class<?>> moreExtensionHosts)
Returns a
ProtoCoder like this one, but with the extensions from the given classes
registered. |
public static final long serialVersionUID
protected ProtoCoder(java.lang.Class<T> protoMessageClass, java.util.Set<java.lang.Class<?>> extensionHostClasses)
public static <T extends com.google.protobuf.Message> ProtoCoder<T> of(java.lang.Class<T> protoMessageClass)
ProtoCoder for the given Protocol Buffers Message.public static <T extends com.google.protobuf.Message> ProtoCoder<T> of(org.apache.beam.sdk.values.TypeDescriptor<T> protoMessageType)
public ProtoCoder<T> withExtensionsFrom(java.lang.Iterable<java.lang.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(java.lang.Class<?>... moreExtensionHosts)
withExtensionsFrom(Iterable).
Does not modify this object.
public void encode(T value, java.io.OutputStream outStream) throws java.io.IOException
encode in class org.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>java.io.IOExceptionpublic void encode(T value, java.io.OutputStream outStream, org.apache.beam.sdk.coders.Coder.Context context) throws java.io.IOException
encode in class org.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>java.io.IOExceptionpublic T decode(java.io.InputStream inStream) throws java.io.IOException
decode in class org.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>java.io.IOExceptionpublic T decode(java.io.InputStream inStream, org.apache.beam.sdk.coders.Coder.Context context) throws java.io.IOException
decode in class org.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>java.io.IOExceptionpublic boolean equals(@Nullable java.lang.Object other)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic void verifyDeterministic()
throws org.apache.beam.sdk.coders.Coder.NonDeterministicException
verifyDeterministic in class org.apache.beam.sdk.coders.CustomCoder<T extends com.google.protobuf.Message>org.apache.beam.sdk.coders.Coder.NonDeterministicExceptionpublic java.lang.Class<T> getMessageType()
Message type this ProtoCoder supports.public java.util.Set<java.lang.Class<?>> getExtensionHosts()
public com.google.protobuf.ExtensionRegistry getExtensionRegistry()
ExtensionRegistry listing all known Protocol Buffers extension messages to
T registered with this ProtoCoder.protected com.google.protobuf.Parser<T> getParser()
Parser, possibly initializing it lazily.public static org.apache.beam.sdk.coders.CoderProvider getCoderProvider()
CoderProvider which uses the ProtoCoder for proto
messages.
This method is invoked reflectively from DefaultCoder.