Class NullableSerializer<T>
- java.lang.Object
-
- org.apache.flink.api.common.typeutils.TypeSerializer<T>
-
- org.apache.flink.api.java.typeutils.runtime.NullableSerializer<T>
-
- Type Parameters:
T- type to serialize
- All Implemented Interfaces:
Serializable
public class NullableSerializer<T> extends TypeSerializer<T>
Serializer wrapper to add support ofnullvalue serialization.If the target serializer does not support
nullvalues of its type, you can use this class to wrap this serializer. This is a generic treatment ofnullvalue serialization which comes with the cost of additional byte in the final serialized value. TheNullableSerializerwill interceptnullvalue serialization case and prepend the target serialized value with a boolean flag marking whether it isnullor not.TypeSerializer<T> originalSerializer = ...; TypeSerializer<T> serializerWithNullValueSupport = NullableSerializer.wrap(originalSerializer); // or TypeSerializer<T> serializerWithNullValueSupport = NullableSerializer.wrapIfNullIsNotSupported(originalSerializer);}- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNullableSerializer.NullableSerializerSnapshot<T>Snapshot for serializers of nullable types, containing the snapshot of its original serializer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> booleancheckIfNullSupported(TypeSerializer<T> serializer)This method checks ifserializersupportsnullvalue.voidcopy(DataInputView source, DataOutputView target)Copies exactly one record from the source input view to the target output view.Tcopy(T from)Creates a deep copy of the given element in a new element.Tcopy(T from, T reuse)Creates a copy from the given element.TcreateInstance()Creates a new instance of the data type.Tdeserialize(DataInputView source)De-serializes a record from the given source input view.Tdeserialize(T reuse, DataInputView source)De-serializes a record from the given source input view into the given reuse record instance if mutable.TypeSerializer<T>duplicate()Creates a deep copy of this serializer if it is necessary, i.e.booleanequals(Object obj)intgetLength()Gets the length of the data type, if it is a fix length data type.inthashCode()booleanisImmutableType()Gets whether the type is an immutable type.voidserialize(T record, DataOutputView target)Serializes the given record to the given target output view.TypeSerializerSnapshot<T>snapshotConfiguration()Snapshots the configuration of this TypeSerializer.static <T> TypeSerializer<T>wrap(TypeSerializer<T> originalSerializer, boolean padNullValueIfFixedLen)This method wraps theoriginalSerializerwith theNullableSerializerif not already wrapped.static <T> TypeSerializer<T>wrapIfNullIsNotSupported(TypeSerializer<T> originalSerializer, boolean padNullValueIfFixedLen)This method tries to serializenullvalue with theoriginalSerializerand wraps it in case ofNullPointerException, otherwise it returns theoriginalSerializer.
-
-
-
Method Detail
-
wrapIfNullIsNotSupported
public static <T> TypeSerializer<T> wrapIfNullIsNotSupported(@Nonnull TypeSerializer<T> originalSerializer, boolean padNullValueIfFixedLen)
This method tries to serializenullvalue with theoriginalSerializerand wraps it in case ofNullPointerException, otherwise it returns theoriginalSerializer.- Parameters:
originalSerializer- serializer to wrap and addnullsupportpadNullValueIfFixedLen- pad null value to preserve the fixed length of original serializer- Returns:
- serializer which supports
nullvalues
-
checkIfNullSupported
public static <T> boolean checkIfNullSupported(@Nonnull TypeSerializer<T> serializer)
This method checks ifserializersupportsnullvalue.- Parameters:
serializer- serializer to check
-
wrap
public static <T> TypeSerializer<T> wrap(@Nonnull TypeSerializer<T> originalSerializer, boolean padNullValueIfFixedLen)
This method wraps theoriginalSerializerwith theNullableSerializerif not already wrapped.- Parameters:
originalSerializer- serializer to wrap and addnullsupportpadNullValueIfFixedLen- pad null value to preserve the fixed length of original serializer- Returns:
- wrapped serializer which supports
nullvalues
-
isImmutableType
public boolean isImmutableType()
Description copied from class:TypeSerializerGets whether the type is an immutable type.- Specified by:
isImmutableTypein classTypeSerializer<T>- Returns:
- True, if the type is immutable.
-
duplicate
public TypeSerializer<T> duplicate()
Description copied from class:TypeSerializerCreates a deep copy of this serializer if it is necessary, i.e. if it is stateful. This can return itself if the serializer is not stateful.We need this because Serializers might be used in several threads. Stateless serializers are inherently thread-safe while stateful serializers might not be thread-safe.
- Specified by:
duplicatein classTypeSerializer<T>
-
createInstance
public T createInstance()
Description copied from class:TypeSerializerCreates a new instance of the data type.- Specified by:
createInstancein classTypeSerializer<T>- Returns:
- A new instance of the data type.
-
copy
public T copy(T from)
Description copied from class:TypeSerializerCreates a deep copy of the given element in a new element.- Specified by:
copyin classTypeSerializer<T>- Parameters:
from- The element reuse be copied.- Returns:
- A deep copy of the element.
-
copy
public T copy(T from, T reuse)
Description copied from class:TypeSerializerCreates a copy from the given element. The method makes an attempt to store the copy in the given reuse element, if the type is mutable. This is, however, not guaranteed.- Specified by:
copyin classTypeSerializer<T>- Parameters:
from- The element to be copied.reuse- The element to be reused. May or may not be used.- Returns:
- A deep copy of the element.
-
getLength
public int getLength()
Description copied from class:TypeSerializerGets the length of the data type, if it is a fix length data type.- Specified by:
getLengthin classTypeSerializer<T>- Returns:
- The length of the data type, or
-1for variable length data types.
-
serialize
public void serialize(T record, DataOutputView target) throws IOException
Description copied from class:TypeSerializerSerializes the given record to the given target output view.- Specified by:
serializein classTypeSerializer<T>- Parameters:
record- The record to serialize.target- The output view to write the serialized data to.- Throws:
IOException- Thrown, if the serialization encountered an I/O related error. Typically raised by the output view, which may have an underlying I/O channel to which it delegates.
-
deserialize
public T deserialize(DataInputView source) throws IOException
Description copied from class:TypeSerializerDe-serializes a record from the given source input view.- Specified by:
deserializein classTypeSerializer<T>- Parameters:
source- The input view from which to read the data.- Returns:
- The deserialized element.
- Throws:
IOException- Thrown, if the de-serialization encountered an I/O related error. Typically raised by the input view, which may have an underlying I/O channel from which it reads.
-
deserialize
public T deserialize(T reuse, DataInputView source) throws IOException
Description copied from class:TypeSerializerDe-serializes a record from the given source input view into the given reuse record instance if mutable.- Specified by:
deserializein classTypeSerializer<T>- Parameters:
reuse- The record instance into which to de-serialize the data.source- The input view from which to read the data.- Returns:
- The deserialized element.
- Throws:
IOException- Thrown, if the de-serialization encountered an I/O related error. Typically raised by the input view, which may have an underlying I/O channel from which it reads.
-
copy
public void copy(DataInputView source, DataOutputView target) throws IOException
Description copied from class:TypeSerializerCopies exactly one record from the source input view to the target output view. Whether this operation works on binary data or partially de-serializes the record to determine its length (such as for records of variable length) is up to the implementer. Binary copies are typically faster. A copy of a record containing two integer numbers (8 bytes total) is most efficiently implemented astarget.write(source, 8);.- Specified by:
copyin classTypeSerializer<T>- Parameters:
source- The input view from which to read the record.target- The target output view to which to write the record.- Throws:
IOException- Thrown if any of the two views raises an exception.
-
equals
public boolean equals(Object obj)
- Specified by:
equalsin classTypeSerializer<T>
-
hashCode
public int hashCode()
- Specified by:
hashCodein classTypeSerializer<T>
-
snapshotConfiguration
public TypeSerializerSnapshot<T> snapshotConfiguration()
Description copied from class:TypeSerializerSnapshots the configuration of this TypeSerializer. This method is only relevant if the serializer is used to state stored in checkpoints/savepoints.The snapshot of the TypeSerializer is supposed to contain all information that affects the serialization format of the serializer. The snapshot serves two purposes: First, to reproduce the serializer when the checkpoint/savepoint is restored, and second, to check whether the serialization format is compatible with the serializer used in the restored program.
IMPORTANT: TypeSerializerSnapshots changed after Flink 1.6. Serializers implemented against Flink versions up to 1.6 should still work, but adjust to new model to enable state evolution and be future-proof. See the class-level comments, section "Upgrading TypeSerializers to the new TypeSerializerSnapshot model" for details.
- Specified by:
snapshotConfigurationin classTypeSerializer<T>- Returns:
- snapshot of the serializer's current configuration (cannot be
null). - See Also:
TypeSerializerSnapshot.resolveSchemaCompatibility(TypeSerializerSnapshot)
-
-