Class SerializableCoder<T extends Serializable>

Type Parameters:
T - the type of elements handled by this coder
All Implemented Interfaces:
Serializable

public class SerializableCoder<T extends Serializable> extends CustomCoder<T>
A 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:
  • Constructor Details

  • Method Details

    • of

      public static <T extends Serializable> SerializableCoder<T> of(TypeDescriptor<T> type)
      Returns a SerializableCoder instance for the provided element type.
      Type Parameters:
      T - the element type
    • consistentWithEquals

      public boolean consistentWithEquals()
      Description copied from class: Coder
      Returns true if this Coder is injective with respect to Object.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 for null.

      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 class Coder<T extends Serializable>
    • structuralValue

      public Object structuralValue(T value)
      The structural value of the object is the object itself. The SerializableCoder should be only used for objects with a proper Object.equals(java.lang.Object) implementation.
      Overrides:
      structuralValue in class Coder<T extends Serializable>
    • of

      public static <T extends Serializable> SerializableCoder<T> of(Class<T> clazz)
      Returns a SerializableCoder instance for the provided element class.
      Type Parameters:
      T - the element type
    • getCoderProvider

      public static CoderProvider getCoderProvider()
      Returns a CoderProvider which uses the SerializableCoder if possible for all types.

      This method is invoked reflectively from DefaultCoder.

    • getRecordType

      public Class<T> getRecordType()
    • encode

      public void encode(T value, OutputStream outStream) throws IOException
      Description copied from class: Coder
      Encodes the given value of type T 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 class Coder<T extends Serializable>
      Throws:
      IOException - if writing to the OutputStream fails for some reason
    • decode

      public T decode(InputStream inStream) throws IOException, CoderException
      Description copied from class: Coder
      Decodes a value of type T 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 class Coder<T extends Serializable>
      Throws:
      IOException - if reading from the InputStream fails for some reason
      CoderException - if the value could not be decoded for some reason
    • verifyDeterministic

      public void verifyDeterministic() throws Coder.NonDeterministicException
      Throw Coder.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() or Comparable.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 class CustomCoder<T extends Serializable>
      Throws:
      Coder.NonDeterministicException - always. Java serialization is not deterministic with respect to Object.equals(java.lang.Object) for all types.
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getEncodedTypeDescriptor

      public TypeDescriptor<T> getEncodedTypeDescriptor()
      Description copied from class: Coder
      Returns the TypeDescriptor for the type encoded.
      Overrides:
      getEncodedTypeDescriptor in class Coder<T extends Serializable>
    • toString

      public String toString()
      Overrides:
      toString in class Object