Class AvroCoder<T>
- Type Parameters:
T- the type of elements handled by this coder
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AvroGenericCoder
Coder using Avro binary format.
Each instance of AvroCoder<T> encapsulates an Avro datum factory and schema for
objects of type T.
The Avro datum factory and schema may be provided explicitly via of(AvroDatumFactory, Schema) or omitted via specific(Class) or
reflect(Class) in which case it will be inferred using Avro's SpecificData or ReflectData
For complete details about schema generation and how it can be controlled please see the
org.apache.avro.specific and org.apache.avro.reflect packages.
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 in GroupByKey operations.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream inStream) Decodes a value of typeTfrom the given input stream in the given context.voidencode(T value, OutputStream outStream) Encodes the given value of typeTonto the given output stream.booleanstatic AvroCoder<GenericRecord> Returns anAvroCoderinstance for the Avro schema.static CoderProviderReturns aCoderProviderwhich uses theAvroCoderif possible for all types.Returns the datum factory used for encoding/decoding.Returns the DatumReader used for decoding.Returns the DatumWriter used for encoding.Returns theTypeDescriptorfor the type encoded.Returns the schema used by this coder.getType()Returns the type this coder encodes/decodes.inthashCode()static <T> AvroCoder<T> Returns anAvroCoderinstance for the provided element class.static <T> AvroCoder<T> Returns anAvroCoderinstance for the given class, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.static <T> AvroCoder<T> Returns anAvroCoderinstance for the provided element type using the provided Avro schemastatic <T> AvroCoder<T> Returns anAvroCoderinstance for the provided element type using the provided Avro schema, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.static AvroGenericCoderReturns anAvroGenericCoderinstance for the Avro schema.static <T> AvroCoder<T> of(AvroDatumFactory<T> datumFactory, Schema schema) Returns anAvroCoderinstance for the providedAvroDatumFactoryusing the provided Avro schema.static <T> AvroCoder<T> of(TypeDescriptor<T> type) Returns anAvroCoderinstance for the provided element type.static <T> AvroCoder<T> of(TypeDescriptor<T> type, boolean useReflectApi) Returns anAvroCoderinstance for the provided element type, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.static <T> AvroCoder<T> Returns anAvroCoderinstance for the provided element type respecting Avro's Reflect* suite for encoding and decoding.static <T> AvroCoder<T> Returns anAvroCoderinstance for the provided element type respecting Avro's Reflect* suite for encoding and decoding.static <T> AvroCoder<T> reflect(TypeDescriptor<T> type) Returns anAvroCoderinstance for the provided element type respecting Avro's Reflect* suite for encoding and decoding.static <T> AvroCoder<T> Returns anAvroCoderinstance for the provided element type respecting Avro's Specific* suite for encoding and decoding.static <T> AvroCoder<T> Returns anAvroCoderinstance for the provided element type respecting Avro's Specific* suite for encoding and decoding.static <T> AvroCoder<T> specific(TypeDescriptor<T> type) Returns anAvroCoderinstance for the provided element type respecting Avro's Specific* suite for encoding and decoding.booleanDeprecated.kept for backward API compatibility only.voidThrowCoder.NonDeterministicExceptionif the coding is not deterministic.Methods inherited from class org.apache.beam.sdk.coders.CustomCoder
getCoderArgumentsMethods inherited from class org.apache.beam.sdk.coders.Coder
consistentWithEquals, decode, encode, getEncodedElementByteSize, getEncodedElementByteSizeUsingCoder, isRegisterByteSizeObserverCheap, registerByteSizeObserver, structuralValue, verifyDeterministic, verifyDeterministic
-
Constructor Details
-
AvroCoder
-
AvroCoder
-
AvroCoder
-
-
Method Details
-
generic
Returns anAvroCoderinstance for the Avro schema. The implicit type is GenericRecord. -
specific
Returns anAvroCoderinstance for the provided element type respecting Avro's Specific* suite for encoding and decoding. -
specific
Returns anAvroCoderinstance for the provided element type respecting Avro's Specific* suite for encoding and decoding. -
specific
Returns anAvroCoderinstance for the provided element type respecting Avro's Specific* suite for encoding and decoding.The schema must correspond to the type provided.
-
reflect
Returns anAvroCoderinstance for the provided element type respecting Avro's Reflect* suite for encoding and decoding. -
reflect
Returns anAvroCoderinstance for the provided element type respecting Avro's Reflect* suite for encoding and decoding. -
reflect
Returns anAvroCoderinstance for the provided element type respecting Avro's Reflect* suite for encoding and decoding.The schema must correspond to the type provided.
-
of
Returns anAvroGenericCoderinstance for the Avro schema. The implicit type is GenericRecord. -
of
Returns anAvroCoderinstance for the provided element type.- Type Parameters:
T- the element type
-
of
Returns anAvroCoderinstance 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
Returns anAvroCoderinstance for the provided element class.- Type Parameters:
T- the element type
-
of
Returns anAvroCoderinstance 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
Returns anAvroCoderinstance for the provided element type using the provided Avro schemaThe schema must correspond to the type provided.
- Type Parameters:
T- the element type
-
of
Returns anAvroCoderinstance for the providedAvroDatumFactoryusing the provided Avro schema.The schema must correspond to the provided datumFactory's type.
- Type Parameters:
T- the element type
-
of
Returns anAvroCoderinstance for the provided element type using the provided Avro schema, respecting whether to use Avro's Reflect* or Specific* suite for encoding and decoding.The schema must correspond to the type provided.
- Type Parameters:
T- the element type
-
getCoderProvider
Returns aCoderProviderwhich uses theAvroCoderif possible for all types.It is unsafe to register this as a
CoderProviderbecause Avro will reflectively accept dangerous types such asObject.This method is invoked reflectively from
DefaultCoder. -
getType
Returns the type this coder encodes/decodes. -
getDatumFactory
Returns the datum factory used for encoding/decoding. -
getDatumWriter
Returns the DatumWriter used for encoding. -
getDatumReader
Returns the DatumReader used for decoding. -
useReflectApi
Deprecated.kept for backward API compatibility only.- Returns:
- true if internal
AvroDatumFactoryis an instance ofAvroDatumFactory.ReflectDatumFactory
-
encode
Description copied from class:CoderEncodes the given value of typeTonto 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:
encodein classCoder<T>- Throws:
IOException- if writing to theOutputStreamfails for some reason
-
decode
Description copied from class:CoderDecodes a value of typeTfrom 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:
decodein classCoder<T>- Throws:
IOException- if reading from theInputStreamfails for some reason
-
verifyDeterministic
Description copied from class:CustomCoderThrowCoder.NonDeterministicExceptionif the coding is not deterministic.In order for a
Coderto 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
Coderalways produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
- Overrides:
verifyDeterministicin classCustomCoder<T>- Throws:
Coder.NonDeterministicException- when the type may not be deterministically encoded using the givenSchema, thedirectBinaryEncoder, and theReflectDatumWriterorGenericDatumWriter.
- two values that compare as equal (via
-
getSchema
Returns the schema used by this coder. -
getEncodedTypeDescriptor
Description copied from class:CoderReturns theTypeDescriptorfor the type encoded.- Overrides:
getEncodedTypeDescriptorin classCoder<T>
-
equals
-
hashCode
public int hashCode()
-