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 |
|---|---|
static CoderProvider |
coderProvider()
|
T |
decode(InputStream inStream,
Coder.Context context) |
void |
encode(T value,
OutputStream outStream,
Coder.Context context) |
boolean |
equals(Object other) |
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(String protoMessageClassName,
List<String> extensionHostClassNames)
Deprecated.
For JSON deserialization only.
|
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. |
getCoderArguments, getInstanceComponents, initializeCloudObject, ofasCloudObject, consistentWithEquals, getComponents, getEncodedElementByteSize, getEncodedTypeDescriptor, isRegisterByteSizeObserverCheap, registerByteSizeObserver, structuralValue, toString, verifyDeterministic, verifyDeterministicpublic static CoderProvider coderProvider()
public 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, Coder.Context context) throws IOException
IOExceptionpublic T decode(InputStream inStream, Coder.Context context) throws IOException
IOExceptionpublic boolean equals(Object other)
equals in class StructuredCoder<T extends com.google.protobuf.Message>public int hashCode()
hashCode in class StructuredCoder<T extends com.google.protobuf.Message>public void verifyDeterministic()
throws Coder.NonDeterministicException
verifyDeterministic in interface Coder<T extends com.google.protobuf.Message>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.@Deprecated public static <T extends com.google.protobuf.Message> ProtoCoder<T> of(String protoMessageClassName, @Nullable List<String> extensionHostClassNames)
Copyright © 2016–2017 The Apache Software Foundation. All rights reserved.