Class Conversion<T>
- Type Parameters:
T
- a Java type that can represent the named logical type
- Direct Known Subclasses:
Conversions.BigDecimalConversion
,Conversions.DecimalConversion
,Conversions.DurationConversion
,Conversions.UUIDConversion
,ProtoConversions.TimestampMicrosConversion
,ProtoConversions.TimestampMillisConversion
,ProtoConversions.TimestampNanosConversion
,TimeConversions.DateConversion
,TimeConversions.LocalTimestampMicrosConversion
,TimeConversions.LocalTimestampMillisConversion
,TimeConversions.LocalTimestampNanosConversion
,TimeConversions.TimeMicrosConversion
,TimeConversions.TimeMillisConversion
,TimeConversions.TimestampMicrosConversion
,TimeConversions.TimestampMillisConversion
,TimeConversions.TimestampNanosConversion
Instances of this class can be added to GenericData to convert a logical type
to a particular representation. This can be done manually, using
GenericData.addLogicalTypeConversion(Conversion)
, or automatically.
This last option uses the Java ServiceLoader
, and requires the
implementation to be a public class with a public no-arg constructor, be
named in a file called /META-INF/services/org.apache.avro.Conversion
,
and both must available in the classpath.
Implementations must provide:
getConvertedType()
: get the Java class used for the logical typegetLogicalTypeName()
: get the logical type this implements
Subclasses must also override the conversion methods for Avro's base types
that are valid for the logical type, or else risk causing
UnsupportedOperationException
at runtime.
Optionally, use getRecommendedSchema()
to provide a Schema that will
be used when generating a Schema for the class. This is useful when using
ReflectData
or ProtobufData
, for example.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadjustAndSetValue
(String varName, String valParamName) Certain logical types may require adjusting the code within the "setter" methods to make sure the data that is set is properly formatted.fromArray
(Collection<?> value, Schema schema, LogicalType type) fromBoolean
(Boolean value, Schema schema, LogicalType type) fromBytes
(ByteBuffer value, Schema schema, LogicalType type) fromCharSequence
(CharSequence value, Schema schema, LogicalType type) fromDouble
(Double value, Schema schema, LogicalType type) fromEnumSymbol
(GenericEnumSymbol<?> value, Schema schema, LogicalType type) fromFixed
(GenericFixed value, Schema schema, LogicalType type) fromFloat
(Float value, Schema schema, LogicalType type) fromInt
(Integer value, Schema schema, LogicalType type) fromLong
(Long value, Schema schema, LogicalType type) fromMap
(Map<?, ?> value, Schema schema, LogicalType type) fromRecord
(IndexedRecord value, Schema schema, LogicalType type) Return the Java class representing the logical type.abstract String
Return the logical type this class converts.Collection
<?> toArray
(T value, Schema schema, LogicalType type) toBoolean
(T value, Schema schema, LogicalType type) toBytes
(T value, Schema schema, LogicalType type) toCharSequence
(T value, Schema schema, LogicalType type) toDouble
(T value, Schema schema, LogicalType type) toEnumSymbol
(T value, Schema schema, LogicalType type) toFixed
(T value, Schema schema, LogicalType type) toFloat
(T value, Schema schema, LogicalType type) toInt
(T value, Schema schema, LogicalType type) toLong
(T value, Schema schema, LogicalType type) Map
<?, ?> toMap
(T value, Schema schema, LogicalType type) toRecord
(T value, Schema schema, LogicalType type)
-
Constructor Details
-
Conversion
public Conversion()
-
-
Method Details
-
getConvertedType
Return the Java class representing the logical type.- Returns:
- a Java class returned by from methods and accepted by to methods
-
getLogicalTypeName
Return the logical type this class converts.- Returns:
- a String logical type name
-
adjustAndSetValue
Certain logical types may require adjusting the code within the "setter" methods to make sure the data that is set is properly formatted. This method allows the Conversion to generate custom setter code if required.- Parameters:
varName
- the name of the variable holding the converted valuevalParamName
- the name of the parameter with the new converted value- Returns:
- a String for the body of the setter method
-
getRecommendedSchema
-
fromBoolean
-
fromInt
-
fromLong
-
fromFloat
-
fromDouble
-
fromCharSequence
-
fromEnumSymbol
-
fromFixed
-
fromBytes
-
fromArray
-
fromMap
-
fromRecord
-
toBoolean
-
toInt
-
toLong
-
toFloat
-
toDouble
-
toCharSequence
-
toEnumSymbol
-
toFixed
-
toBytes
-
toArray
-
toMap
-
toRecord
-