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 classACoderProviderRegistrarwhich registers aCoderProviderwhich can handle serializable types.Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSerializableCoder(Class<T> type, TypeDescriptor<T> typeDescriptor) -
Method Summary
Modifier and TypeMethodDescriptionbooleandecode(InputStream inStream) 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.booleanstatic CoderProviderReturns aCoderProviderwhich uses theSerializableCoderif possible for all types.Returns theTypeDescriptorfor the type encoded.inthashCode()static <T extends Serializable>
SerializableCoder<T> Returns aSerializableCoderinstance for the provided element class.static <T extends Serializable>
SerializableCoder<T> of(TypeDescriptor<T> type) Returns aSerializableCoderinstance for the provided element type.structuralValue(T value) The structural value of the object is the object itself.toString()voidThrowCoder.NonDeterministicExceptionif the coding is not deterministic.Methods inherited from class org.apache.beam.sdk.coders.CustomCoder
getCoderArgumentsMethods inherited from class org.apache.beam.sdk.coders.Coder
decode, encode, getEncodedElementByteSize, getEncodedElementByteSizeUsingCoder, isRegisterByteSizeObserverCheap, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
Constructor Details
-
SerializableCoder
-
-
Method Details
-
of
Returns aSerializableCoderinstance for the provided element type.- Type Parameters:
T- the element type
-
consistentWithEquals
public boolean consistentWithEquals()Description copied from class:CoderReturnstrueif thisCoderis 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:
consistentWithEqualsin classCoder<T extends Serializable>
-
structuralValue
The structural value of the object is the object itself. TheSerializableCodershould be only used for objects with a properObject.equals(java.lang.Object)implementation.- Overrides:
structuralValuein classCoder<T extends Serializable>
-
of
Returns aSerializableCoderinstance for the provided element class.- Type Parameters:
T- the element type
-
getCoderProvider
Returns aCoderProviderwhich uses theSerializableCoderif possible for all types.This method is invoked reflectively from
DefaultCoder. -
getRecordType
-
encode
Description 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 classCoder<T extends Serializable>- Throws:
IOException- if writing to theOutputStreamfails for some reason
-
decode
Description 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 classCoder<T extends Serializable>- Throws:
IOException- if reading from theInputStreamfails for some reasonCoderException- if the value could not be decoded for some reason
-
verifyDeterministic
ThrowCoder.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 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:CoderReturns theTypeDescriptorfor the type encoded.- Overrides:
getEncodedTypeDescriptorin classCoder<T extends Serializable>
-
toString
-