Package org.apache.beam.sdk.schemas
Class SchemaCoder<T>
- java.lang.Object
-
- org.apache.beam.sdk.coders.Coder<T>
-
- org.apache.beam.sdk.coders.CustomCoder<T>
-
- org.apache.beam.sdk.schemas.SchemaCoder<T>
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
RowCoder
@Experimental(SCHEMAS) public class SchemaCoder<T> extends CustomCoder<T>
SchemaCoder
is used as the coder for types that have schemas registered.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SchemaCoder(Schema schema, TypeDescriptor<T> typeDescriptor, SerializableFunction<T,Row> toRowFunction, SerializableFunction<Row,T> fromRowFunction)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> Coder<T>
coderForFieldType(Schema.FieldType fieldType)
boolean
consistentWithEquals()
T
decode(java.io.InputStream inStream)
Decodes a value of typeT
from the given input stream in the given context.void
encode(T value, java.io.OutputStream outStream)
Encodes the given value of typeT
onto the given output stream.boolean
equals(@Nullable java.lang.Object o)
TypeDescriptor<T>
getEncodedTypeDescriptor()
Returns theTypeDescriptor
for the type encoded.SerializableFunction<Row,T>
getFromRowFunction()
Returns the toRow conversion function.Schema
getSchema()
Returns the schema associated with this type.SerializableFunction<T,Row>
getToRowFunction()
Returns the fromRow conversion function.int
hashCode()
static SchemaCoder<Row>
of(Schema schema)
static <T> SchemaCoder<T>
of(Schema schema, TypeDescriptor<T> typeDescriptor, SerializableFunction<T,Row> toRowFunction, SerializableFunction<Row,T> fromRowFunction)
Returns aSchemaCoder
for the specified class.static void
overrideEncodingPositions(java.util.UUID uuid, java.util.Map<java.lang.String,java.lang.Integer> encodingPositions)
Override encoding positions for the given schema.java.lang.String
toString()
void
verifyDeterministic()
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, isRegisterByteSizeObserverCheap, registerByteSizeObserver, structuralValue, verifyDeterministic, verifyDeterministic
-
-
-
-
Field Detail
-
schema
protected final Schema schema
-
-
Constructor Detail
-
SchemaCoder
protected SchemaCoder(Schema schema, TypeDescriptor<T> typeDescriptor, SerializableFunction<T,Row> toRowFunction, SerializableFunction<Row,T> fromRowFunction)
-
-
Method Detail
-
of
public static <T> SchemaCoder<T> of(Schema schema, TypeDescriptor<T> typeDescriptor, SerializableFunction<T,Row> toRowFunction, SerializableFunction<Row,T> fromRowFunction)
Returns aSchemaCoder
for the specified class. If no schema is registered for this class, then throwsNoSuchSchemaException
. The parameter functions to convert from and to Rows must implement the equals contract.
-
of
public static SchemaCoder<Row> of(Schema schema)
-
overrideEncodingPositions
public static void overrideEncodingPositions(java.util.UUID uuid, java.util.Map<java.lang.String,java.lang.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, java.io.OutputStream outStream) throws java.io.IOException
Description copied from class:Coder
Encodes the given value of typeT
onto the given output stream.- Specified by:
encode
in classCoder<T>
- Throws:
java.io.IOException
- if writing to theOutputStream
fails for some reasonCoderException
- if the value could not be encoded for some reason
-
decode
public T decode(java.io.InputStream inStream) throws java.io.IOException
Description copied from class:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Specified by:
decode
in classCoder<T>
- Throws:
java.io.IOException
- if reading from theInputStream
fails for some reasonCoderException
- if the value could not be decoded for some reason
-
verifyDeterministic
public void verifyDeterministic() throws Coder.NonDeterministicException
Description copied from class:CustomCoder
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>
- Throws:
Coder.NonDeterministicException
- aCustomCoder
is presumed nondeterministic.
- two values that compare as equal (via
-
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>
-
coderForFieldType
public static <T> Coder<T> coderForFieldType(Schema.FieldType fieldType)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(@Nullable java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
getEncodedTypeDescriptor
public TypeDescriptor<T> getEncodedTypeDescriptor()
Description copied from class:Coder
Returns theTypeDescriptor
for the type encoded.- Overrides:
getEncodedTypeDescriptor
in classCoder<T>
-
-