public class CoderRegistry extends Object implements CoderProvider
CoderRegistry allows registering the default Coder to use for a Java class,
and looking up and instantiating the default Coder for a Java type.
CoderRegistry uses the following mechanisms to determine a default Coder for a
Java class, in order of precedence:
CoderFactory can be registered to handle a particular class via
registerCoder(Class, CoderFactory).Coder class with the static methods to satisfy
CoderFactories.fromStaticMethods(java.lang.Class<T>) can be registered via
registerCoder(Class, Class).registerStandardCoders().DefaultCoder can be used to annotate a type with
the default Coder type. The Coder class must satisfy the requirements
of CoderProviders.fromStaticMethods(java.lang.Class<T>).
CoderProvider is used to attempt to provide a Coder
for any type. By default, there are two chained fallback coders:
ProtoCoder.coderProvider(), which can provide a coder to efficiently serialize any
Protocol Buffers message, and then SerializableCoder.PROVIDER, which can provide a
Coder for any type that is serializable via Java serialization. The fallback
CoderProvider can be get and set respectively using
getFallbackCoderProvider() and setFallbackCoderProvider(org.apache.beam.sdk.coders.CoderProvider). Multiple
fallbacks can be chained together using CoderProviders.firstOf(org.apache.beam.sdk.coders.CoderProvider...).
| Constructor and Description |
|---|
CoderRegistry() |
| Modifier and Type | Method and Description |
|---|---|
<T> Coder<T> |
getCoder(TypeDescriptor<T> typeDescriptor)
|
<T,OutputT> |
getDefaultCoder(Class<? extends T> subClass,
Class<T> baseClass,
Map<Type,? extends Coder<?>> knownCoders,
TypeVariable<?> param)
|
<T> Coder<T> |
getDefaultCoder(Class<T> clazz)
Returns the
Coder to use by default for values of the given class. |
<T> Coder<T> |
getDefaultCoder(T exampleValue)
Returns the
Coder to use for the provided example value, if it can be determined. |
<InputT,OutputT> |
getDefaultCoder(TypeDescriptor<OutputT> typeDescriptor,
TypeDescriptor<InputT> inputTypeDescriptor,
Coder<InputT> inputCoder)
|
<T> Coder<T> |
getDefaultCoder(TypeDescriptor<T> typeDescriptor)
Returns the
Coder to use by default for values of the given type. |
<InputT,OutputT> |
getDefaultOutputCoder(SerializableFunction<InputT,OutputT> fn,
Coder<InputT> inputCoder)
|
CoderProvider |
getFallbackCoderProvider()
Returns the fallback
CoderProvider for this registry. |
void |
registerCoder(Class<?> clazz,
Class<?> coderClazz)
Registers
coderClazz as the default Coder class to handle encoding and
decoding instances of clazz, overriding prior registrations if any exist. |
void |
registerCoder(Class<?> clazz,
CoderFactory coderFactory)
Registers
coderFactory as the default CoderFactory to produce Coder
instances to decode and encode instances of clazz. |
<T> void |
registerCoder(Class<T> rawClazz,
Coder<T> coder)
Register the provided
Coder for encoding all values of the specified Class. |
void |
registerStandardCoders()
Registers standard Coders with this CoderRegistry.
|
void |
setFallbackCoderProvider(CoderProvider coderProvider)
Sets the fallback
CoderProvider for this registry. |
public void registerStandardCoders()
public void registerCoder(Class<?> clazz, Class<?> coderClazz)
coderClazz as the default Coder class to handle encoding and
decoding instances of clazz, overriding prior registrations if any exist.
Supposing T is the static type corresponding to the clazz, then
coderClazz should have a static factory method with the following signature:
public static Coder<T> of(Coder<X> argCoder1, Coder<Y> argCoder2, ...)
This method will be called to create instances of Coder<T> for values of type
T, passing Coders for each of the generic type parameters of T. If T
takes no generic type parameters, then the of() factory method should have no
arguments.
If T is a parameterized type, then it should additionally have a method with the
following signature:
public static List<Object> getInstanceComponents(T exampleValue);
This method will be called to decompose a value during the Coder inference process,
to automatically choose Coders for the components.
clazz - the class of objects to be encodedcoderClazz - a class with static factory methods to provide Coderspublic void registerCoder(Class<?> clazz, CoderFactory coderFactory)
coderFactory as the default CoderFactory to produce Coder
instances to decode and encode instances of clazz. This will override prior
registrations if any exist.public <T> void registerCoder(Class<T> rawClazz, Coder<T> coder)
Coder for encoding all values of the specified Class.
This will override prior registrations if any exist.
Not for use with generic rawtypes. Instead, register a CoderFactory via
registerCoder(Class, CoderFactory) or ensure your Coder class has the
appropriate static methods and register it directly via registerCoder(Class, Class).
public <T> Coder<T> getDefaultCoder(TypeDescriptor<T> typeDescriptor) throws CannotProvideCoderException
Coder to use by default for values of the given type.CannotProvideCoderException - if there is no default Coder.public <T> Coder<T> getCoder(TypeDescriptor<T> typeDescriptor) throws CannotProvideCoderException
getCoder in interface CoderProviderCannotProvideCoderException - if no coder can be providedpublic <InputT,OutputT> Coder<OutputT> getDefaultCoder(TypeDescriptor<OutputT> typeDescriptor, TypeDescriptor<InputT> inputTypeDescriptor, Coder<InputT> inputCoder) throws CannotProvideCoderException
Coder to use by default for values of the given type, where the given input
type uses the given Coder.CannotProvideCoderException - if there is no default Coder.public <InputT,OutputT> Coder<OutputT> getDefaultOutputCoder(SerializableFunction<InputT,OutputT> fn, Coder<InputT> inputCoder) throws CannotProvideCoderException
Coder to use on elements produced by this function, given the Coder
used for its input elements.CannotProvideCoderExceptionpublic <T,OutputT> Coder<OutputT> getDefaultCoder(Class<? extends T> subClass, Class<T> baseClass, Map<Type,? extends Coder<?>> knownCoders, TypeVariable<?> param) throws CannotProvideCoderException
Coder to use for the specified type parameter specialization of the
subclass, given Coders to use for all other type parameters (if any).CannotProvideCoderException - if there is no default Coder.public <T> Coder<T> getDefaultCoder(T exampleValue) throws CannotProvideCoderException
Coder to use for the provided example value, if it can be determined.CannotProvideCoderException - if there is no default Coder or
more than one Coder matchespublic <T> Coder<T> getDefaultCoder(Class<T> clazz) throws CannotProvideCoderException
Coder to use by default for values of the given class. The following three
sources for a Coder will be attempted, in order:
Coder class registered explicitly via a call to registerCoder(java.lang.Class<?>, java.lang.Class<?>),
DefaultCoder annotation on the class,
CoderProvider, which may be able to generate a
Coder for an arbitrary class.
CannotProvideCoderException - if a Coder cannot be providedpublic void setFallbackCoderProvider(CoderProvider coderProvider)
CoderProvider for this registry. If no other method succeeds in
providing a Coder<T> for a type T, then the registry will attempt to create
a Coder using this CoderProvider.
By default, this is set to the chain of ProtoCoder.coderProvider() and
SerializableCoder.PROVIDER.
public CoderProvider getFallbackCoderProvider()
CoderProvider for this registry.
See setFallbackCoderProvider(org.apache.beam.sdk.coders.CoderProvider).