Class SchemaCoder<T>

All Implemented Interfaces:
Serializable
Direct Known Subclasses:
RowCoder

public class SchemaCoder<T> extends CustomCoder<T>
SchemaCoder is used as the coder for types that have schemas registered.
See Also:
  • Field Details

    • schema

      protected final Schema schema
  • Constructor Details

  • Method Details

    • of

      public static <T> SchemaCoder<T> of(Schema schema, TypeDescriptor<T> typeDescriptor, SerializableFunction<T,Row> toRowFunction, SerializableFunction<Row,T> fromRowFunction)
      Returns a SchemaCoder for the specified class. If no schema is registered for this class, then throws NoSuchSchemaException. The parameter functions to convert from and to Rows must implement the equals contract.
    • of

      public static SchemaCoder<Row> of(Schema schema)
      Returns a SchemaCoder for Row instances with the given schema.
    • overrideEncodingPositions

      public static void overrideEncodingPositions(UUID uuid, Map<String,Integer> encodingPositions)
      Override encoding positions for the given schema.
    • getSchema

      public Schema getSchema()
      Returns the schema associated with this type.
    • getFromRowFunction

      public SerializableFunction<Row,T> getFromRowFunction()
      Returns the toRow conversion function.
    • getToRowFunction

      public SerializableFunction<T,Row> getToRowFunction()
      Returns the fromRow conversion function.
    • 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>
      Throws:
      IOException - if writing to the OutputStream fails for some reason
    • decode

      public T decode(InputStream inStream) throws IOException
      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>
      Throws:
      IOException - if reading from the InputStream fails for some reason
    • verifyDeterministic

      public void verifyDeterministic() throws Coder.NonDeterministicException
      Description copied from class: CustomCoder
      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>
      Throws:
      Coder.NonDeterministicException - a CustomCoder is presumed nondeterministic.
    • 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>
    • coderForFieldType

      public static <T> Coder<T> coderForFieldType(Schema.FieldType fieldType)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(@Nullable Object o)
      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>