Google Cloud Dataflow SDK for Java, version 1.9.1
Class DelegateCoder<T,IntermediateT>
- java.lang.Object
-
- com.google.cloud.dataflow.sdk.coders.StandardCoder<T>
-
- com.google.cloud.dataflow.sdk.coders.DeterministicStandardCoder<T>
-
- com.google.cloud.dataflow.sdk.coders.AtomicCoder<T>
-
- com.google.cloud.dataflow.sdk.coders.CustomCoder<T>
-
- com.google.cloud.dataflow.sdk.coders.DelegateCoder<T,IntermediateT>
-
- Type Parameters:
T
- The type of objects coded by this Coder.IntermediateT
- The type of objects aT
will be converted to for coding.
- All Implemented Interfaces:
- Coder<T>, Serializable
- Direct Known Subclasses:
- StringDelegateCoder
public class DelegateCoder<T,IntermediateT> extends CustomCoder<T>
ADelegateCoder<T, IntermediateT>
wraps aCoder
forIntermediateT
and encodes/decodes values of typeT
by converting to/fromIntermediateT
and then encoding/decoding using the underlyingCoder<IntermediateT>
.The conversions from
T
toIntermediateT
and vice versa must be supplied asDelegateCoder.CodingFunction
, a serializable function that may throw anyException
. If a thrown exception is an instance ofCoderException
orIOException
, it will be re-thrown, otherwise it will be wrapped as aCoderException
.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static interface
DelegateCoder.CodingFunction<InputT,OutputT>
ACodingFunction<InputT, OutputT>
is a serializable function fromInputT
toOutputT
that may throw anyException
.-
Nested classes/interfaces inherited from interface com.google.cloud.dataflow.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
-
Constructor Summary
Constructors Modifier Constructor and Description protected
DelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description T
decode(InputStream inStream, Coder.Context context)
Decodes a value of typeT
from the given input stream in the given context.void
encode(T value, OutputStream outStream, Coder.Context context)
Encodes the given value of typeT
onto the given output stream in the given context.Collection<String>
getAllowedEncodings()
A collection of encodings supported byCoder.decode(java.io.InputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)
in addition to the encoding fromCoder.getEncodingId()
(which is assumed supported).Coder<IntermediateT>
getCoder()
Returns the coder used to encode/decode the intermediate values produced/consumed by the coding functions of thisDelegateCoder
.String
getEncodingId()
An identifier for the binary format written byCoder.encode(T, java.io.OutputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)
.static <T,IntermediateT>
DelegateCoder<T,IntermediateT>of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)
Object
structuralValue(T value)
Returns an object with anObject.equals()
method that represents structural equality on the argument.String
toString()
void
verifyDeterministic()
ThrowCoder.NonDeterministicException
if the coding is not deterministic.-
Methods inherited from class com.google.cloud.dataflow.sdk.coders.CustomCoder
asCloudObject, of
-
Methods inherited from class com.google.cloud.dataflow.sdk.coders.AtomicCoder
getCoderArguments, getInstanceComponents
-
Methods inherited from class com.google.cloud.dataflow.sdk.coders.StandardCoder
consistentWithEquals, equals, getComponents, getEncodedElementByteSize, hashCode, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
-
-
-
Constructor Detail
-
DelegateCoder
protected DelegateCoder(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)
-
-
Method Detail
-
of
public static <T,IntermediateT> DelegateCoder<T,IntermediateT> of(Coder<IntermediateT> coder, DelegateCoder.CodingFunction<T,IntermediateT> toFn, DelegateCoder.CodingFunction<IntermediateT,T> fromFn)
-
encode
public void encode(T value, OutputStream outStream, Coder.Context context) throws CoderException, IOException
Description copied from interface:Coder
Encodes the given value of typeT
onto the given output stream in the given context.- Throws:
CoderException
- if the value could not be encoded for some reasonIOException
- if writing to theOutputStream
fails for some reason
-
decode
public T decode(InputStream inStream, Coder.Context context) throws CoderException, IOException
Description copied from interface:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Throws:
CoderException
- if the value could not be decoded for some reasonIOException
- if reading from theInputStream
fails for some reason
-
getCoder
public Coder<IntermediateT> getCoder()
Returns the coder used to encode/decode the intermediate values produced/consumed by the coding functions of thisDelegateCoder
.
-
verifyDeterministic
public void verifyDeterministic() throws Coder.NonDeterministicException
ThrowCoder.NonDeterministicException
if the coding is not deterministic.In order for a
Coder
to 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
Coder
always produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
- Specified by:
verifyDeterministic
in interfaceCoder<T>
- Overrides:
verifyDeterministic
in classCustomCoder<T>
- Throws:
NonDeterministicException
- when the underlying coder'sverifyDeterministic()
throws aCoder.NonDeterministicException
. For this to be safe, the intermediateCodingFunction<T, IntermediateT>
must also be deterministic.Coder.NonDeterministicException
- if this coder is not deterministic.
- two values that compare as equal (via
-
structuralValue
public Object structuralValue(T value) throws Exception
Returns an object with anObject.equals()
method that represents structural equality on the argument.For any two values
x
andy
of typeT
, if their encoded bytes are the same, then it must be the case thatstructuralValue(x).equals(@code structuralValue(y)
.Most notably:
- The structural value for an array coder should perform a structural comparison of the contents of the arrays, rather than the default behavior of comparing according to object identity.
- The structural value for a coder accepting
null
should be a proper object with anequals()
method, even if the input value isnull
.
See also
Coder.consistentWithEquals()
.- Specified by:
structuralValue
in interfaceCoder<T>
- Overrides:
structuralValue
in classStandardCoder<T>
- Returns:
- a structural for a value of type
T
obtained by first converting toIntermediateT
and then obtaining a structural value according to the underlying coder. - Throws:
Exception
-
toString
public String toString()
- Overrides:
toString
in classStandardCoder<T>
-
getEncodingId
public String getEncodingId()
An identifier for the binary format written byCoder.encode(T, java.io.OutputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)
.This value, along with the fully qualified class name, forms an identifier for the binary format of this coder. Whenever this value changes, the new encoding is considered incompatible with the prior format: It is presumed that the prior version of the coder will be unable to correctly read the new format and the new version of the coder will be unable to correctly read the old format.
If the format is changed in a backwards-compatible way (the Coder can still accept data from the prior format), such as by adding optional fields to a Protocol Buffer or Avro definition, and you want Dataflow to understand that the new coder is compatible with the prior coder, this value must remain unchanged. It is then the responsibility of
Coder.decode(java.io.InputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)
to correctly read data from the prior format.- Specified by:
getEncodingId
in interfaceCoder<T>
- Overrides:
getEncodingId
in classCustomCoder<T>
- Returns:
- a
String
composed from the underlying coder class name and its encoding id. Note that this omits any description of the coding functions. These should be modified with care.
-
getAllowedEncodings
public Collection<String> getAllowedEncodings()
A collection of encodings supported byCoder.decode(java.io.InputStream, com.google.cloud.dataflow.sdk.coders.Coder.Context)
in addition to the encoding fromCoder.getEncodingId()
(which is assumed supported).This information is not currently used for any purpose. It is descriptive only, and this method is subject to change.
- Specified by:
getAllowedEncodings
in interfaceCoder<T>
- Overrides:
getAllowedEncodings
in classStandardCoder<T>
- Returns:
- allowed encodings which are composed from the underlying coder class and its allowed encoding ids. Note that this omits any description of the coding functions. These should be modified with care.
- See Also:
Coder.getEncodingId()
-
-