Google Cloud Dataflow SDK for Java, version 1.9.1
Class CoderRegistry
- java.lang.Object
-
- com.google.cloud.dataflow.sdk.coders.CoderRegistry
-
- All Implemented Interfaces:
- CoderProvider
public class CoderRegistry extends Object implements CoderProvider
ACoderRegistry
allows registering the defaultCoder
to use for a Java class, and looking up and instantiating the defaultCoder
for a Java type.CoderRegistry
uses the following mechanisms to determine a defaultCoder
for a Java class, in order of precedence:- Registration:
- A
CoderFactory
can be registered to handle a particular class viaregisterCoder(Class, CoderFactory)
. - A
Coder
class with the static methods to satisfyCoderFactories.fromStaticMethods(java.lang.Class<T>)
can be registered viaregisterCoder(Class, Class)
. - Built-in types are registered via
registerStandardCoders()
.
- A
- Annotations:
DefaultCoder
can be used to annotate a type with the defaultCoder
type. TheCoder
class must satisfy the requirements ofCoderProviders.fromStaticMethods(java.lang.Class<T>)
. - Fallback: A fallback
CoderProvider
is used to attempt to provide aCoder
for any type. By default, this isSerializableCoder.PROVIDER
, which can provide aCoder
for any type that is serializable via Java serialization. The fallbackCoderProvider
can be get and set viagetFallbackCoderProvider()
andsetFallbackCoderProvider(com.google.cloud.dataflow.sdk.coders.CoderProvider)
. Multiple fallbacks can be chained together usingCoderProviders.firstOf(com.google.cloud.dataflow.sdk.coders.CoderProvider...)
.
-
-
Constructor Summary
Constructors Constructor and Description CoderRegistry()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description <T> Coder<T>
getCoder(TypeDescriptor<T> typeDescriptor)
<T,OutputT>
Coder<OutputT>getDefaultCoder(Class<? extends T> subClass, Class<T> baseClass, Map<Type,? extends Coder<?>> knownCoders, TypeVariable<?> param)
<T> Coder<T>
getDefaultCoder(Class<T> clazz)
Returns theCoder
to use by default for values of the given class.<T> Coder<T>
getDefaultCoder(T exampleValue)
Returns theCoder
to use for the provided example value, if it can be determined.<InputT,OutputT>
Coder<OutputT>getDefaultCoder(TypeDescriptor<OutputT> typeDescriptor, TypeDescriptor<InputT> inputTypeDescriptor, Coder<InputT> inputCoder)
<T> Coder<T>
getDefaultCoder(TypeDescriptor<T> typeDescriptor)
Returns theCoder
to use by default for values of the given type.<T> Map<Type,Coder<?>>
getDefaultCoders(Class<? extends T> subClass, Class<T> baseClass, Map<Type,? extends Coder<?>> knownCoders)
Deprecated.this method is not part of the public interface and will be made private<InputT,OutputT>
Coder<OutputT>getDefaultOutputCoder(SerializableFunction<InputT,OutputT> fn, Coder<InputT> inputCoder)
CoderProvider
getFallbackCoderProvider()
Returns the fallbackCoderProvider
for this registry.void
registerCoder(Class<?> clazz, Class<?> coderClazz)
RegisterscoderClazz
as the defaultCoder
class to handle encoding and decoding instances ofclazz
, overriding prior registrations if any exist.void
registerCoder(Class<?> clazz, CoderFactory coderFactory)
RegisterscoderFactory
as the defaultCoderFactory
to produceCoder
instances to decode and encode instances ofclazz
.<T> void
registerCoder(Class<T> rawClazz, Coder<T> coder)
Register the providedCoder
for encoding all values of the specifiedClass
.void
registerStandardCoders()
Registers standard Coders with this CoderRegistry.void
setFallbackCoderProvider(CoderProvider coderProvider)
Sets the fallbackCoderProvider
for this registry.
-
-
-
Method Detail
-
registerStandardCoders
public void registerStandardCoders()
Registers standard Coders with this CoderRegistry.
-
registerCoder
public void registerCoder(Class<?> clazz, Class<?> coderClazz)
RegisterscoderClazz
as the defaultCoder
class to handle encoding and decoding instances ofclazz
, overriding prior registrations if any exist.Supposing
T
is the static type corresponding to theclazz
, thencoderClazz
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 typeT
, passing Coders for each of the generic type parameters ofT
. IfT
takes no generic type parameters, then theof()
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 chooseCoders
for the components.- Parameters:
clazz
- the class of objects to be encodedcoderClazz
- a class with static factory methods to provideCoders
-
registerCoder
public void registerCoder(Class<?> clazz, CoderFactory coderFactory)
RegisterscoderFactory
as the defaultCoderFactory
to produceCoder
instances to decode and encode instances ofclazz
. This will override prior registrations if any exist.
-
registerCoder
public <T> void registerCoder(Class<T> rawClazz, Coder<T> coder)
Register the providedCoder
for encoding all values of the specifiedClass
. This will override prior registrations if any exist.Not for use with generic rawtypes. Instead, register a
CoderFactory
viaregisterCoder(Class, CoderFactory)
or ensure yourCoder
class has the appropriate static methods and register it directly viaregisterCoder(Class, Class)
.
-
getDefaultCoder
public <T> Coder<T> getDefaultCoder(TypeDescriptor<T> typeDescriptor) throws CannotProvideCoderException
Returns theCoder
to use by default for values of the given type.- Throws:
CannotProvideCoderException
- if there is no default Coder.
-
getCoder
public <T> Coder<T> getCoder(TypeDescriptor<T> typeDescriptor) throws CannotProvideCoderException
- Specified by:
getCoder
in interfaceCoderProvider
- Throws:
CannotProvideCoderException
- if no coder can be provided
-
getDefaultCoder
public <InputT,OutputT> Coder<OutputT> getDefaultCoder(TypeDescriptor<OutputT> typeDescriptor, TypeDescriptor<InputT> inputTypeDescriptor, Coder<InputT> inputCoder) throws CannotProvideCoderException
Returns theCoder
to use by default for values of the given type, where the given input type uses the givenCoder
.- Throws:
CannotProvideCoderException
- if there is no default Coder.
-
getDefaultOutputCoder
public <InputT,OutputT> Coder<OutputT> getDefaultOutputCoder(SerializableFunction<InputT,OutputT> fn, Coder<InputT> inputCoder) throws CannotProvideCoderException
Returns theCoder
to use on elements produced by this function, given theCoder
used for its input elements.- Throws:
CannotProvideCoderException
-
getDefaultCoder
public <T,OutputT> Coder<OutputT> getDefaultCoder(Class<? extends T> subClass, Class<T> baseClass, Map<Type,? extends Coder<?>> knownCoders, TypeVariable<?> param) throws CannotProvideCoderException
Returns theCoder
to use for the specified type parameter specialization of the subclass, givenCoders
to use for all other type parameters (if any).- Throws:
CannotProvideCoderException
- if there is no default Coder.
-
getDefaultCoder
public <T> Coder<T> getDefaultCoder(T exampleValue) throws CannotProvideCoderException
Returns theCoder
to use for the provided example value, if it can be determined.- Throws:
CannotProvideCoderException
- if there is no defaultCoder
or more than oneCoder
matches
-
getDefaultCoder
public <T> Coder<T> getDefaultCoder(Class<T> clazz) throws CannotProvideCoderException
Returns theCoder
to use by default for values of the given class. The following three sources for aCoder
will be attempted, in order:- A
Coder
class registered explicitly via a call toregisterCoder(java.lang.Class<?>, java.lang.Class<?>)
, - A
DefaultCoder
annotation on the class, - This registry's fallback
CoderProvider
, which may be able to generate aCoder
for an arbitrary class.
- Throws:
CannotProvideCoderException
- if aCoder
cannot be provided
- A
-
setFallbackCoderProvider
public void setFallbackCoderProvider(CoderProvider coderProvider)
Sets the fallbackCoderProvider
for this registry. If no other method succeeds in providing aCoder<T>
for a typeT
, then the registry will attempt to create aCoder
using thisCoderProvider
.By default, this is set to
SerializableCoder.PROVIDER
.
-
getFallbackCoderProvider
public CoderProvider getFallbackCoderProvider()
Returns the fallbackCoderProvider
for this registry.See
setFallbackCoderProvider(com.google.cloud.dataflow.sdk.coders.CoderProvider)
.
-
getDefaultCoders
@Deprecated public <T> Map<Type,Coder<?>> getDefaultCoders(Class<? extends T> subClass, Class<T> baseClass, Map<Type,? extends Coder<?>> knownCoders)
Deprecated. this method is not part of the public interface and will be made privateReturns aMap
from each ofbaseClass
's type parameters to theCoder
to use by default for it, in the context ofsubClass
's specialization ofbaseClass
.If no
Coder
can be inferred for a particular type parameter, then that type variable will be absent from the returnedMap
.For example, if
baseClass
isMap.class
, whereMap<K, V>
has type parametersK
andV
, andsubClass
extendsMap<String, Integer>
then the result will map the type variableK
to aCoder<String>
and the type variableV
to aCoder<Integer>
.The
knownCoders
parameter can be used to provide knownCoders
for any of the parameters; these will be used to infer the others.Note that inference is attempted for every type variable. For a type
MyType<One, Two, Three>
inference will be attempted for all ofOne
,Two
,Three
, even if the requester only wants aCoder
forTwo
.For this reason
getDefaultCoders
(plural) does not throw an exception if aCoder
for a particular type variable cannot be inferred, but merely omits the entry from the returnedMap
. It is the responsibility of the caller (usuallygetDefaultCoder(com.google.cloud.dataflow.sdk.values.TypeDescriptor<T>)
to extract the desired coder or throw aCannotProvideCoderException
when appropriate.- Parameters:
subClass
- the concrete type whose specializations are being inferredbaseClass
- the base type, a parameterized classknownCoders
- a map corresponding to the set of knownCoders
indexed by parameter name
-
-