Class AvroCoder<T>
- java.lang.Object
-
- org.apache.beam.sdk.coders.Coder<T>
-
- org.apache.beam.sdk.coders.CustomCoder<T>
-
- org.apache.beam.sdk.coders.AvroCoder<T>
-
- Type Parameters:
T
- the type of elements handled by this coder
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
AvroGenericCoder
public class AvroCoder<T> extends CustomCoder<T>
ACoder
using Avro binary format.Each instance of
AvroCoder<T>
encapsulates an Avro schema for objects of typeT
.The Avro schema may be provided explicitly via
of(Class, Schema)
or omitted viaof(Class)
, in which case it will be inferred using Avro'sReflectData
.For complete details about schema generation and how it can be controlled please see the
org.apache.avro.reflect
package. Only concrete classes with a no-argument constructor can be mapped to Avro records. All inherited fields that are not static or transient are included. Fields are not permitted to be null unless annotated byNullable
or aUnion
schema containing"null"
.To use, specify the
Coder
type on a PCollection:PCollection<MyCustomElement> records = input.apply(...) .setCoder(AvroCoder.of(MyCustomElement.class));
or annotate the element class using
@DefaultCoder
.@DefaultCoder(AvroCoder.class) public class MyCustomElement { ... }
The implementation attempts to determine if the Avro encoding of the given type will satisfy the criteria of
Coder.verifyDeterministic()
by inspecting both the type and the Schema provided or generated by Avro. Only coders that are deterministic can be used inGroupByKey
operations.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AvroCoder.JodaTimestampConversion
Conversion for DateTime.-
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 other)
static CoderProvider
getCoderProvider()
Returns aCoderProvider
which uses theAvroCoder
if possible for all types.TypeDescriptor<T>
getEncodedTypeDescriptor()
Returns theTypeDescriptor
for the type encoded.org.apache.avro.Schema
getSchema()
Returns the schema used by this coder.java.lang.Class<T>
getType()
Returns the type this coder encodes/decodes.int
hashCode()
static <T> AvroCoder<T>
of(java.lang.Class<T> clazz)
Returns anAvroCoder
instance for the provided element class.static <T> AvroCoder<T>
of(java.lang.Class<T> type, boolean useReflectApi)
Returns anAvroCoder
instance for the given class, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.static <T> AvroCoder<T>
of(java.lang.Class<T> type, org.apache.avro.Schema schema)
Returns anAvroCoder
instance for the provided element type using the provided Avro schema.static <T> AvroCoder<T>
of(java.lang.Class<T> type, org.apache.avro.Schema schema, boolean useReflectApi)
Returns anAvroCoder
instance for the given class and schema, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.static AvroGenericCoder
of(org.apache.avro.Schema schema)
Returns anAvroGenericCoder
instance for the Avro schema.static <T> AvroCoder<T>
of(TypeDescriptor<T> type)
Returns anAvroCoder
instance for the provided element type.static <T> AvroCoder<T>
of(TypeDescriptor<T> type, boolean useReflectApi)
Returns anAvroCoder
instance for the provided element type, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.boolean
useReflectApi()
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
consistentWithEquals, decode, encode, getEncodedElementByteSize, isRegisterByteSizeObserverCheap, registerByteSizeObserver, structuralValue, verifyDeterministic, verifyDeterministic
-
-
-
-
Method Detail
-
of
public static <T> AvroCoder<T> of(TypeDescriptor<T> type)
Returns anAvroCoder
instance for the provided element type.- Type Parameters:
T
- the element type
-
of
public static <T> AvroCoder<T> of(TypeDescriptor<T> type, boolean useReflectApi)
Returns anAvroCoder
instance for the provided element type, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.- Type Parameters:
T
- the element type
-
of
public static <T> AvroCoder<T> of(java.lang.Class<T> clazz)
Returns anAvroCoder
instance for the provided element class.- Type Parameters:
T
- the element type
-
of
public static AvroGenericCoder of(org.apache.avro.Schema schema)
Returns anAvroGenericCoder
instance for the Avro schema. The implicit type is GenericRecord.
-
of
public static <T> AvroCoder<T> of(java.lang.Class<T> type, boolean useReflectApi)
Returns anAvroCoder
instance for the given class, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.- Type Parameters:
T
- the element type
-
of
public static <T> AvroCoder<T> of(java.lang.Class<T> type, org.apache.avro.Schema schema)
Returns anAvroCoder
instance for the provided element type using the provided Avro schema.The schema must correspond to the type provided.
- Type Parameters:
T
- the element type
-
of
public static <T> AvroCoder<T> of(java.lang.Class<T> type, org.apache.avro.Schema schema, boolean useReflectApi)
Returns anAvroCoder
instance for the given class and schema, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.- Type Parameters:
T
- the element type
-
getCoderProvider
public static CoderProvider getCoderProvider()
Returns aCoderProvider
which uses theAvroCoder
if possible for all types.It is unsafe to register this as a
CoderProvider
because Avro will reflectively accept dangerous types such asObject
.This method is invoked reflectively from
DefaultCoder
.
-
getType
public java.lang.Class<T> getType()
Returns the type this coder encodes/decodes.
-
useReflectApi
public boolean useReflectApi()
-
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
- when the type may not be deterministically encoded using the givenSchema
, thedirectBinaryEncoder
, and theReflectDatumWriter
orGenericDatumWriter
.
- two values that compare as equal (via
-
getSchema
public org.apache.avro.Schema getSchema()
Returns the schema used by this coder.
-
getEncodedTypeDescriptor
public TypeDescriptor<T> getEncodedTypeDescriptor()
Description copied from class:Coder
Returns theTypeDescriptor
for the type encoded.- Overrides:
getEncodedTypeDescriptor
in classCoder<T>
-
equals
public boolean equals(@Nullable java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-