Class ProtoCoder<T extends com.google.protobuf.Message>
- java.lang.Object
-
- org.apache.beam.sdk.coders.Coder<T>
-
- org.apache.beam.sdk.coders.CustomCoder<T>
-
- org.apache.beam.sdk.extensions.protobuf.ProtoCoder<T>
-
- Type Parameters:
T- the Protocol BuffersMessagehandled by thisCoder.
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
DynamicProtoCoder
public class ProtoCoder<T extends com.google.protobuf.Message> extends org.apache.beam.sdk.coders.CustomCoder<T>ACoderusing Google Protocol Buffers binary format.ProtoCodersupports both Protocol Buffers syntax versions 2 and 3.To learn more about Protocol Buffers, visit: https://developers.google.com/protocol-buffers
ProtoCoderis registered in the globalCoderRegistryas the defaultCoderfor anyMessageobject. Custom message extensions are also supported, but these extensions must be registered for a particularProtoCoderinstance and that instance must be registered on thePCollectionthat 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);Versioning
ProtoCodersupports both versions 2 and 3 of the Protocol Buffers syntax. However, the Java runtime version of thegoogle.com.protobuflibrary must match exactly the version ofprotocthat was used to produce the JAR files containing the compiled.protomessages.For more information, see the Protocol Buffers documentation.
ProtoCoderand DeterminismIn general, Protocol Buffers messages can be encoded deterministically within a single pipeline as long as:
- The encoded messages (and any transitively linked messages) do not use
mapfields. - Every Java VM that encodes or decodes the messages use the same runtime version of the
Protocol Buffers library and the same compiled
.protofile JAR.
ProtoCoderand Encoding StabilityWhen changing Protocol Buffers messages, follow the rules in the Protocol Buffers language guides for
proto2andproto3syntaxes, 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.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static longserialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description protectedProtoCoder(java.lang.Class<T> protoMessageClass, java.util.Set<java.lang.Class<?>> extensionHostClasses)Private constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Tdecode(java.io.InputStream inStream)Tdecode(java.io.InputStream inStream, org.apache.beam.sdk.coders.Coder.Context context)voidencode(T value, java.io.OutputStream outStream)voidencode(T value, java.io.OutputStream outStream, org.apache.beam.sdk.coders.Coder.Context context)booleanequals(@Nullable java.lang.Object other)static org.apache.beam.sdk.coders.CoderProvidergetCoderProvider()java.util.Set<java.lang.Class<?>>getExtensionHosts()com.google.protobuf.ExtensionRegistrygetExtensionRegistry()Returns theExtensionRegistrylisting all known Protocol Buffers extension messages toTregistered with thisProtoCoder.java.lang.Class<T>getMessageType()Returns the Protocol BuffersMessagetype thisProtoCodersupports.protected com.google.protobuf.Parser<T>getParser()Get the memoizedParser, possibly initializing it lazily.inthashCode()static <T extends com.google.protobuf.Message>
ProtoCoder<T>of(java.lang.Class<T> protoMessageClass)Returns aProtoCoderfor the given Protocol BuffersMessage.static <T extends com.google.protobuf.Message>
ProtoCoder<T>of(org.apache.beam.sdk.values.TypeDescriptor<T> protoMessageType)voidverifyDeterministic()ProtoCoder<T>withExtensionsFrom(java.lang.Class<?>... moreExtensionHosts)ProtoCoder<T>withExtensionsFrom(java.lang.Iterable<java.lang.Class<?>> moreExtensionHosts)Returns aProtoCoderlike this one, but with the extensions from the given classes registered.
-
-
-
Field Detail
-
serialVersionUID
public static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProtoCoder
protected ProtoCoder(java.lang.Class<T> protoMessageClass, java.util.Set<java.lang.Class<?>> extensionHostClasses)
Private constructor.
-
-
Method Detail
-
of
public static <T extends com.google.protobuf.Message> ProtoCoder<T> of(java.lang.Class<T> protoMessageClass)
Returns aProtoCoderfor the given Protocol BuffersMessage.
-
of
public static <T extends com.google.protobuf.Message> ProtoCoder<T> of(org.apache.beam.sdk.values.TypeDescriptor<T> protoMessageType)
-
withExtensionsFrom
public ProtoCoder<T> withExtensionsFrom(java.lang.Iterable<java.lang.Class<?>> moreExtensionHosts)
Returns aProtoCoderlike 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.
-
withExtensionsFrom
public ProtoCoder<T> withExtensionsFrom(java.lang.Class<?>... moreExtensionHosts)
SeewithExtensionsFrom(Iterable).Does not modify this object.
-
encode
public void encode(T value, java.io.OutputStream outStream) throws java.io.IOException
- Specified by:
encodein classorg.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>- Throws:
java.io.IOException
-
encode
public void encode(T value, java.io.OutputStream outStream, org.apache.beam.sdk.coders.Coder.Context context) throws java.io.IOException
- Overrides:
encodein classorg.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>- Throws:
java.io.IOException
-
decode
public T decode(java.io.InputStream inStream) throws java.io.IOException
- Specified by:
decodein classorg.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>- Throws:
java.io.IOException
-
decode
public T decode(java.io.InputStream inStream, org.apache.beam.sdk.coders.Coder.Context context) throws java.io.IOException
- Overrides:
decodein classorg.apache.beam.sdk.coders.Coder<T extends com.google.protobuf.Message>- Throws:
java.io.IOException
-
equals
public boolean equals(@Nullable java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
verifyDeterministic
public void verifyDeterministic() throws org.apache.beam.sdk.coders.Coder.NonDeterministicException- Overrides:
verifyDeterministicin classorg.apache.beam.sdk.coders.CustomCoder<T extends com.google.protobuf.Message>- Throws:
org.apache.beam.sdk.coders.Coder.NonDeterministicException
-
getMessageType
public java.lang.Class<T> getMessageType()
Returns the Protocol BuffersMessagetype thisProtoCodersupports.
-
getExtensionHosts
public java.util.Set<java.lang.Class<?>> getExtensionHosts()
-
getExtensionRegistry
public com.google.protobuf.ExtensionRegistry getExtensionRegistry()
Returns theExtensionRegistrylisting all known Protocol Buffers extension messages toTregistered with thisProtoCoder.
-
getParser
protected com.google.protobuf.Parser<T> getParser()
Get the memoizedParser, possibly initializing it lazily.
-
getCoderProvider
public static org.apache.beam.sdk.coders.CoderProvider getCoderProvider()
Returns aCoderProviderwhich uses theProtoCoderforproto messages.This method is invoked reflectively from
DefaultCoder.
-
-