Class ProtoCoder<T extends Message>
- All Implemented Interfaces:
- Serializable
- Direct Known Subclasses:
- DynamicProtoCoder
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);
 Versioning
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 Determinism
 In 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.
ProtoCoder and Encoding Stability
 When 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.
- See Also:
- 
Nested Class SummaryNested classes/interfaces inherited from class org.apache.beam.sdk.coders.CoderCoder.Context, Coder.NonDeterministicException
- 
Field SummaryFields
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedProtoCoder(Class<T> protoMessageClass, Set<Class<?>> extensionHostClasses) Private constructor.
- 
Method SummaryModifier and TypeMethodDescriptiondecode(InputStream inStream) Decodes a value of typeTfrom the given input stream in the given context.decode(InputStream inStream, Coder.Context context) Decodes a value of typeTfrom the given input stream in the given context.voidencode(T value, OutputStream outStream) Encodes the given value of typeTonto the given output stream.voidencode(T value, OutputStream outStream, Coder.Context context) Encodes the given value of typeTonto the given output stream in the given context.booleanstatic CoderProviderReturns theTypeDescriptorfor the type encoded.Returns theExtensionRegistrylisting all known Protocol Buffers extension messages toTregistered with thisProtoCoder.Returns the Protocol BuffersMessagetype thisProtoCodersupports.Get the memoizedParser, possibly initializing it lazily.inthashCode()static <T extends Message>
 ProtoCoder<T> Returns aProtoCoderfor the given Protocol BuffersMessage.static <T extends Message>
 ProtoCoder<T> of(TypeDescriptor<T> protoMessageType) voidThrowCoder.NonDeterministicExceptionif the coding is not deterministic.withExtensionsFrom(Class<?>... moreExtensionHosts) withExtensionsFrom(Iterable<Class<?>> moreExtensionHosts) Returns aProtoCoderlike this one, but with the extensions from the given classes registered.Methods inherited from class org.apache.beam.sdk.coders.CustomCodergetCoderArgumentsMethods inherited from class org.apache.beam.sdk.coders.CoderconsistentWithEquals, getEncodedElementByteSize, getEncodedElementByteSizeUsingCoder, isRegisterByteSizeObserverCheap, registerByteSizeObserver, structuralValue, verifyDeterministic, verifyDeterministic
- 
Field Details- 
serialVersionUIDpublic static final long serialVersionUID- See Also:
 
 
- 
- 
Constructor Details- 
ProtoCoderPrivate constructor.
 
- 
- 
Method Details- 
ofReturns aProtoCoderfor the given Protocol BuffersMessage.
- 
of
- 
withExtensionsFromReturns 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. 
- 
withExtensionsFromSeewithExtensionsFrom(Iterable).Does not modify this object. 
- 
encodeDescription copied from class:CoderEncodes the given value of typeTonto the given output stream. Multiple elements can be encoded next to each other on the output stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
- encodein class- Coder<T extends Message>
- Throws:
- IOException- if writing to the- OutputStreamfails for some reason
 
- 
encodeDescription copied from class:CoderEncodes the given value of typeTonto the given output stream in the given context.- Overrides:
- encodein class- Coder<T extends Message>
- Throws:
- IOException- if writing to the- OutputStreamfails for some reason
 
- 
decodeDescription copied from class:CoderDecodes a value of typeTfrom the given input stream in the given context. Returns the decoded value. Multiple elements can be encoded next to each other on the input stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
- decodein class- Coder<T extends Message>
- Throws:
- IOException- if reading from the- InputStreamfails for some reason
 
- 
decodeDescription copied from class:CoderDecodes a value of typeTfrom the given input stream in the given context. Returns the decoded value.- Overrides:
- decodein class- Coder<T extends Message>
- Throws:
- IOException- if reading from the- InputStreamfails for some reason
 
- 
equals
- 
hashCodepublic int hashCode()
- 
verifyDeterministicDescription copied from class:CustomCoderThrowCoder.NonDeterministicExceptionif the coding is not deterministic.In order for a Coderto be considered deterministic, the following must be true:- two values that compare as equal (via Object.equals()orComparable.compareTo(), if supported) have the same encoding.
- the Coderalways produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
 - Overrides:
- verifyDeterministicin class- CustomCoder<T extends Message>
- Throws:
- Coder.NonDeterministicException- a- CustomCoderis presumed nondeterministic.
 
- two values that compare as equal (via 
- 
getMessageTypeReturns the Protocol BuffersMessagetype thisProtoCodersupports.
- 
getEncodedTypeDescriptorDescription copied from class:CoderReturns theTypeDescriptorfor the type encoded.- Overrides:
- getEncodedTypeDescriptorin class- Coder<T extends Message>
 
- 
getExtensionHosts
- 
getExtensionRegistryReturns theExtensionRegistrylisting all known Protocol Buffers extension messages toTregistered with thisProtoCoder.
- 
getParserGet the memoizedParser, possibly initializing it lazily.
- 
getCoderProviderReturns aCoderProviderwhich uses theProtoCoderforproto messages.This method is invoked reflectively from DefaultCoder.
 
-