T - the type of elements handled by this coderpublic class SerializableCoder<T extends Serializable> extends AtomicCoder<T>
Coder for Java classes that implement Serializable.
To use, specify the coder type on a PCollection:
PCollection<MyRecord> records =
foo.apply(...).setCoder(SerializableCoder.of(MyRecord.class));
SerializableCoder does not guarantee a deterministic encoding, as Java
serialization may produce different binary encodings for two equivalent
objects.
Coder.Context, Coder.NonDeterministicException| Modifier and Type | Field and Description |
|---|---|
static CoderProvider |
PROVIDER
A
CoderProvider that constructs a SerializableCoder
for any class that implements serializable. |
| Modifier | Constructor and Description |
|---|---|
protected |
SerializableCoder(Class<T> type) |
| Modifier and Type | Method and Description |
|---|---|
org.apache.beam.sdk.util.CloudObject |
asCloudObject()
Returns the
CloudObject that represents this Coder. |
T |
decode(InputStream inStream,
Coder.Context context)
Decodes a value of type
T from the given input stream in
the given context. |
void |
encode(T value,
OutputStream outStream,
Coder.Context context)
Encodes the given value of type
T onto the given output stream
in the given context. |
boolean |
equals(Object other) |
String |
getEncodingId()
An identifier for the binary format written by
Coder.encode(T, java.io.OutputStream, org.apache.beam.sdk.coders.Coder.Context). |
Class<T> |
getRecordType() |
int |
hashCode() |
static <T extends Serializable> |
of(Class<T> clazz)
Returns a
SerializableCoder instance for the provided element class. |
static SerializableCoder<?> |
of(String classType) |
static <T extends Serializable> |
of(TypeDescriptor<T> type)
Returns a
SerializableCoder instance for the provided element type. |
void |
verifyDeterministic()
Throw
Coder.NonDeterministicException if the coding is not deterministic. |
getCoderArguments, getInstanceComponentsconsistentWithEquals, getAllowedEncodings, getComponents, getEncodedElementByteSize, isRegisterByteSizeObserverCheap, registerByteSizeObserver, structuralValue, toString, verifyDeterministic, verifyDeterministicpublic static final CoderProvider PROVIDER
CoderProvider that constructs a SerializableCoder
for any class that implements serializable.public static <T extends Serializable> SerializableCoder<T> of(TypeDescriptor<T> type)
SerializableCoder instance for the provided element type.T - the element typepublic static <T extends Serializable> SerializableCoder<T> of(Class<T> clazz)
SerializableCoder instance for the provided element class.T - the element typepublic static SerializableCoder<?> of(String classType) throws ClassNotFoundException
ClassNotFoundExceptionpublic void encode(T value, OutputStream outStream, Coder.Context context) throws IOException, CoderException
CoderT onto the given output stream
in the given context.IOException - if writing to the OutputStream fails
for some reasonCoderException - if the value could not be encoded for some reasonpublic T decode(InputStream inStream, Coder.Context context) throws IOException, CoderException
CoderT from the given input stream in
the given context. Returns the decoded value.IOException - if reading from the InputStream fails
for some reasonCoderException - if the value could not be decoded for some reasonpublic String getEncodingId()
CoderCoder.encode(T, java.io.OutputStream, org.apache.beam.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, org.apache.beam.sdk.coders.Coder.Context) to correctly
read data from the prior format.
getEncodingId in interface Coder<T extends Serializable>getEncodingId in class StandardCoder<T extends Serializable>public org.apache.beam.sdk.util.CloudObject asCloudObject()
CoderCloudObject that represents this Coder.asCloudObject in interface Coder<T extends Serializable>asCloudObject in class StandardCoder<T extends Serializable>public void verifyDeterministic()
throws Coder.NonDeterministicException
Coder.NonDeterministicException if the coding is not deterministic.
In order for a Coder to be considered deterministic,
the following must be true:
Object.equals()
or Comparable.compareTo(), if supported) have the same
encoding.
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.
verifyDeterministic in interface Coder<T extends Serializable>verifyDeterministic in class DeterministicStandardCoder<T extends Serializable>NonDeterministicException - always. Java serialization is not
deterministic with respect to Object.equals(java.lang.Object) for all types.Coder.NonDeterministicException - if this coder is not deterministic.public boolean equals(Object other)
StandardCoderequals in class StandardCoder<T extends Serializable>true if the two StandardCoder instances have the
same class and equal components.public int hashCode()
hashCode in class StandardCoder<T extends Serializable>