Class SerializableCoder<T extends Serializable>
- Type Parameters:
T
- the type of elements handled by this coder
- All Implemented Interfaces:
Serializable
Coder
for Java classes that implement Serializable
.
SerializableCoder
should be used only for objects that have proper Object.equals(java.lang.Object)
and Object.hashCode()
implementations.
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.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
ACoderProviderRegistrar
which registers aCoderProvider
which can handle serializable types.Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SerializableCoder
(Class<T> type, TypeDescriptor<T> typeDescriptor) -
Method Summary
Modifier and TypeMethodDescriptionboolean
decode
(InputStream inStream) Decodes a value of typeT
from the given input stream in the given context.void
encode
(T value, OutputStream outStream) Encodes the given value of typeT
onto the given output stream.boolean
static CoderProvider
Returns aCoderProvider
which uses theSerializableCoder
if possible for all types.Returns theTypeDescriptor
for the type encoded.int
hashCode()
static <T extends Serializable>
SerializableCoder<T> Returns aSerializableCoder
instance for the provided element class.static <T extends Serializable>
SerializableCoder<T> of
(TypeDescriptor<T> type) Returns aSerializableCoder
instance for the provided element type.structuralValue
(T value) The structural value of the object is the object itself.toString()
void
ThrowCoder.NonDeterministicException
if the coding is not deterministic.Methods inherited from class org.apache.beam.sdk.coders.CustomCoder
getCoderArguments
Methods inherited from class org.apache.beam.sdk.coders.Coder
decode, encode, getEncodedElementByteSize, getEncodedElementByteSizeUsingCoder, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
Constructor Details
-
SerializableCoder
-
-
Method Details
-
of
Returns aSerializableCoder
instance for the provided element type.- Type Parameters:
T
- the element type
-
consistentWithEquals
public boolean consistentWithEquals()Description copied from class:Coder
Returnstrue
if thisCoder
is injective with respect toObject.equals(java.lang.Object)
.Whenever the encoded bytes of two values are equal, then the original values are equal according to
Objects.equals()
. Note that this is well-defined fornull
.This condition is most notably false for arrays. More generally, this condition is false whenever
equals()
compares object identity, rather than performing a semantic/structural comparison.By default, returns false.
- Overrides:
consistentWithEquals
in classCoder<T extends Serializable>
-
structuralValue
The structural value of the object is the object itself. TheSerializableCoder
should be only used for objects with a properObject.equals(java.lang.Object)
implementation.- Overrides:
structuralValue
in classCoder<T extends Serializable>
-
of
Returns aSerializableCoder
instance for the provided element class.- Type Parameters:
T
- the element type
-
getCoderProvider
Returns aCoderProvider
which uses theSerializableCoder
if possible for all types.This method is invoked reflectively from
DefaultCoder
. -
getRecordType
-
encode
Description copied from class:Coder
Encodes the given value of typeT
onto 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:
encode
in classCoder<T extends Serializable>
- Throws:
IOException
- if writing to theOutputStream
fails for some reason
-
decode
Description copied from class:Coder
Decodes a value of typeT
from 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:
decode
in classCoder<T extends Serializable>
- Throws:
IOException
- if reading from theInputStream
fails for some reasonCoderException
- if the value could not be decoded for some reason
-
verifyDeterministic
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.
- Overrides:
verifyDeterministic
in classCustomCoder<T extends Serializable>
- Throws:
Coder.NonDeterministicException
- always. Java serialization is not deterministic with respect toObject.equals(java.lang.Object)
for all types.
- two values that compare as equal (via
-
equals
-
hashCode
public int hashCode() -
getEncodedTypeDescriptor
Description copied from class:Coder
Returns theTypeDescriptor
for the type encoded.- Overrides:
getEncodedTypeDescriptor
in classCoder<T extends Serializable>
-
toString
-