Package org.apache.avro.generic

# Class GenericData

java.lang.Object
org.apache.avro.generic.GenericData

Direct Known Subclasses:
  `SpecificData`

public class GenericData extends Object
Utilities for generic Java data. See `GenericRecordBuilder` for a convenient way to build `GenericRecord` instances.

See Also:
  - `GenericRecordBuilder`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static class`
`GenericData.AbstractArray<T>`
`static class`
`GenericData.Array<T>`
Default implementation of an array.
`static class`
`GenericData.EnumSymbol`
Default implementation of `GenericEnumSymbol`.
`static class`
`GenericData.Fixed`
Default implementation of `GenericFixed`.
`static interface`
`GenericData.InstanceSupplier`
`static class`
`GenericData.Record`
Default implementation of `GenericRecord`.
`static enum`
`GenericData.StringType`
Used to specify the Java type for a string schema.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`FAST_READER_PROP`
`static final String`
`STRING_PROP`
`protected static final String`
`STRING_TYPE_STRING`

## Constructor Summary

Constructors
Constructor
Description
`GenericData()`
For subclasses.
`GenericData(ClassLoader classLoader)`
For subclasses.

## Method Summary

Modifier and Type
Method
Description
`void`
`addLogicalTypeConversion(Conversion<?> conversion)`
Registers the given conversion to be used when reading and writing with this data model.
`int`
`compare(Object o1, Object o2, Schema s)`
Compare objects according to their schema.
`protected int`
`compare(Object o1, Object o2, Schema s, boolean equals)`
Comparison implementation.
`protected int`
`compareMaps(Map<?,?> m1, Map<?,?> m2)`
`DatumReader`
`createDatumReader(Schema schema)`
Returns a `DatumReader` for this kind of data.
`DatumReader`
`createDatumReader(Schema writer, Schema reader)`
Returns a `DatumReader` for this kind of data.
`DatumWriter`
`createDatumWriter(Schema schema)`
Returns a `DatumWriter` for this kind of data.
`Object`
`createEnum(String symbol, Schema schema)`
Called to create an enum value.
`Object`
`createFixed(Object old, byte[] bytes, Schema schema)`
Called to create an fixed value.
`Object`
`createFixed(Object old, Schema schema)`
Called to create an fixed value.
`Object`
`createString(Object value)`
Called to create an string value.
`<T> T`
`deepCopy(Schema schema, T value)`
Makes a deep copy of a value given its schema.
`static GenericData`
`get()`
Return the singleton instance.
`protected Collection`
`getArrayAsCollection(Object datum)`
Called to access an array as a collection.
`ClassLoader`
`getClassLoader()`
Return the class loader that's used (by subclasses).
`<T> Conversion<T>`
`getConversionByClass(Class<T> datumClass)`
Returns the first conversion found for the given class.
`<T> Conversion<T>`
`getConversionByClass(Class<T> datumClass, LogicalType logicalType)`
Returns the conversion for the given class and logical type.
`<T> Conversion<T>`
`getConversionFor(LogicalType logicalType)`
Returns the Conversion for the given logical type.
`Collection<Conversion<?>>`
`getConversions()`
`Object`
`getDefaultValue(Schema.Field field)`
Gets the default value of the given field, if any.
`protected Schema`
`getEnumSchema(Object enu)`
Called to obtain the schema of a enum.
`FastReaderBuilder`
`getFastReaderBuilder()`
`Object`
`getField(Object record, String name, int position)`
Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to retrieve a record field value from a reused instance.
`protected Object`
`getField(Object record, String name, int pos, Object state)`
Version of `getField(java.lang.Object, java.lang.String, int)` that has state.
`protected Schema`
`getFixedSchema(Object fixed)`
Called to obtain the schema of a fixed.
`GenericData.InstanceSupplier`
`getNewRecordSupplier(Schema schema)`
create a supplier that allows to get new record instances for a given schema in an optimized way
`protected Map<Class<?>,String>`
`getPrimitiveTypeCache()`
Called to obtain the primitive type cache.
`protected Schema`
`getRecordSchema(Object record)`
Called to obtain the schema of a record.
`protected Object`
`getRecordState(Object record, Schema schema)`
Produce state for repeated calls to `getField(Object,String,int,Object)` and `setField(Object,String,int,Object,Object)` on the same record.
`protected String`
`getSchemaName(Object datum)`
Return the schema full name for a datum.
`int`
`hashCode(Object o, Schema s)`
Compute a hash code according to a schema, consistent with `compare(Object,Object,Schema)`.
`Schema`
`induce(Object datum)`
Create a schema given an example datum.
`protected boolean`
`instanceOf(Schema schema, Object datum)`
Called by `resolveUnion(Schema,Object)`.
`protected boolean`
`isArray(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isBoolean(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isBytes(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isDouble(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isEnum(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`boolean`
`isFastReaderEnabled()`
`protected boolean`
`isFixed(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isFloat(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isInteger(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isLong(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isMap(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isRecord(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean`
`isString(Object datum)`
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`Object`
`newArray(Object old, int size, Schema schema)`
Called to create new array instances.
`Object`
`newMap(Object old, int size)`
Called to create new array instances.
`Object`
`newRecord(Object old, Schema schema)`
Called to create new record instances.
`static Schema.Type`
`optimalValueType(Schema schema, LogicalType logicalType, Class<?> convertedElementType)`
Determine the optimal value type for an array.
`int`
`resolveUnion(Schema union, Object datum)`
Return the index for a datum within a union.
`GenericData`
`setFastReaderEnabled(boolean flag)`
`void`
`setField(Object record, String name, int position, Object value)`
Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to set a record fields value to a record instance.
`protected void`
`setField(Object record, String name, int position, Object value, Object state)`
Version of `setField(java.lang.Object, java.lang.String, int, java.lang.Object)` that has state.
`static void`
`setStringType(Schema s, GenericData.StringType stringType)`
Set the Java type to be used when reading this schema.
`String`
`toString(Object datum)`
Renders a Java datum as JSON.
`protected void`
`toString(Object datum, StringBuilder buffer, IdentityHashMap<Object,Object> seenObjects)`
Renders a Java datum as JSON.
`boolean`
`validate(Schema schema, Object datum)`
Returns true if a Java datum matches a schema.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### STRING_PROP

public static final String STRING_PROP

See Also:
  - Constant Field Values

### STRING_TYPE_STRING

protected static final String STRING_TYPE_STRING

See Also:
  - Constant Field Values

### FAST_READER_PROP

public static final String FAST_READER_PROP

See Also:
  - Constant Field Values

## Constructor Details

### GenericData

public GenericData()
For subclasses. Applications normally use `get()`.

### GenericData

public GenericData(ClassLoader classLoader)
For subclasses. GenericData does not use a ClassLoader.

## Method Details

### setStringType

public static void setStringType(Schema s, GenericData.StringType stringType)
Set the Java type to be used when reading this schema. Meaningful only only string schemas and map schemas (for the keys).

### get

public static GenericData get()
Return the singleton instance.

### getClassLoader

public ClassLoader getClassLoader()
Return the class loader that's used (by subclasses).

### getConversions

public Collection<Conversion<?>> getConversions()

### addLogicalTypeConversion

public void addLogicalTypeConversion(Conversion<?> conversion)
Registers the given conversion to be used when reading and writing with this data model. Conversions can also be registered automatically, as documented on the class `Conversion<T>`.

Parameters:
  `conversion` - a logical type Conversion.

### getConversionByClass

public <T> Conversion<T> getConversionByClass(Class<T> datumClass)
Returns the first conversion found for the given class.

Parameters:
  `datumClass` - a Class
Returns:
  the first registered conversion for the class, or null

### getConversionByClass

public <T> Conversion<T> getConversionByClass(Class<T> datumClass, LogicalType logicalType)
Returns the conversion for the given class and logical type.

Parameters:
  `datumClass` - a Class
  `logicalType` - a LogicalType
Returns:
  the conversion for the class and logical type, or null

### getConversionFor

public <T> Conversion<T> getConversionFor(LogicalType logicalType)
Returns the Conversion for the given logical type.

Parameters:
  `logicalType` - a logical type
Returns:
  the conversion for the logical type, or null

### setFastReaderEnabled

public GenericData setFastReaderEnabled(boolean flag)

### isFastReaderEnabled

public boolean isFastReaderEnabled()

### getFastReaderBuilder

public FastReaderBuilder getFastReaderBuilder()

### createDatumReader

public DatumReader createDatumReader(Schema schema)
Returns a `DatumReader` for this kind of data.

### createDatumReader

public DatumReader createDatumReader(Schema writer, Schema reader)
Returns a `DatumReader` for this kind of data.

### createDatumWriter

public DatumWriter createDatumWriter(Schema schema)
Returns a `DatumWriter` for this kind of data.

### validate

public boolean validate(Schema schema, Object datum)
Returns true if a Java datum matches a schema.

### toString

public String toString(Object datum)
Renders a Java datum as JSON.

### toString

protected void toString(Object datum, StringBuilder buffer, IdentityHashMap<Object,Object> seenObjects)
Renders a Java datum as JSON.

### induce

public Schema induce(Object datum)
Create a schema given an example datum.

### setField

public void setField(Object record, String name, int position, Object value)
Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to set a record fields value to a record instance. The default implementation is for `IndexedRecord`.

### getField

public Object getField(Object record, String name, int position)
Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to retrieve a record field value from a reused instance. The default implementation is for `IndexedRecord`.

### getRecordState

protected Object getRecordState(Object record, Schema schema)
Produce state for repeated calls to `getField(Object,String,int,Object)` and `setField(Object,String,int,Object,Object)` on the same record.

### setField

protected void setField(Object record, String name, int position, Object value, Object state)
Version of `setField(java.lang.Object, java.lang.String, int, java.lang.Object)` that has state.

### getField

protected Object getField(Object record, String name, int pos, Object state)
Version of `getField(java.lang.Object, java.lang.String, int)` that has state.

### resolveUnion

public int resolveUnion(Schema union, Object datum)
Return the index for a datum within a union. Implemented with `Schema.getIndexNamed(String)` and `getSchemaName(Object)`.

### getSchemaName

protected String getSchemaName(Object datum)
Return the schema full name for a datum. Called by `resolveUnion(Schema,Object)`.

### getPrimitiveTypeCache

protected Map<Class<?>,String> getPrimitiveTypeCache()
Called to obtain the primitive type cache. May be overridden for alternate record representations.

### instanceOf

protected boolean instanceOf(Schema schema, Object datum)
Called by `resolveUnion(Schema,Object)`. May be overridden for alternate data representations.

### isArray

protected boolean isArray(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### getArrayAsCollection

protected Collection getArrayAsCollection(Object datum)
Called to access an array as a collection.

### isRecord

protected boolean isRecord(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### getRecordSchema

protected Schema getRecordSchema(Object record)
Called to obtain the schema of a record. By default calls {GenericContainer#getSchema(). May be overridden for alternate record representations.

### isEnum

protected boolean isEnum(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### getEnumSchema

protected Schema getEnumSchema(Object enu)
Called to obtain the schema of a enum. By default calls {GenericContainer#getSchema(). May be overridden for alternate enum representations.

### isMap

protected boolean isMap(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### isFixed

protected boolean isFixed(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### getFixedSchema

protected Schema getFixedSchema(Object fixed)
Called to obtain the schema of a fixed. By default calls {GenericContainer#getSchema(). May be overridden for alternate fixed representations.

### isString

protected boolean isString(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### isBytes

protected boolean isBytes(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### isInteger

protected boolean isInteger(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### isLong

protected boolean isLong(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### isFloat

protected boolean isFloat(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### isDouble

protected boolean isDouble(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### isBoolean

protected boolean isBoolean(Object datum)
Called by the default implementation of `instanceOf(org.apache.avro.Schema, java.lang.Object)`.

### hashCode

public int hashCode(Object o, Schema s)
Compute a hash code according to a schema, consistent with `compare(Object,Object,Schema)`.

### compare

public int compare(Object o1, Object o2, Schema s)
Compare objects according to their schema. If equal, return zero. If greater-than, return 1, if less than return -1. Order is consistent with that of `BinaryData.compare(byte[], int, byte[], int, Schema)`.

### compareMaps

protected int compareMaps(Map<?,?> m1, Map<?,?> m2)

### compare

protected int compare(Object o1, Object o2, Schema s, boolean equals)
Comparison implementation. When equals is true, only checks for equality, not for order.

### getDefaultValue

public Object getDefaultValue(Schema.Field field)
Gets the default value of the given field, if any.

Parameters:
  `field` - the field whose default value should be retrieved.
Returns:
  the default value associated with the given field, or null if none is specified in the schema.

### deepCopy

public <T> T deepCopy(Schema schema, T value)
Makes a deep copy of a value given its schema.

Logical types are converted to raw types, copied, then converted back.

Parameters:
  `schema` - the schema of the value to deep copy.
  `value` - the value to deep copy.
Returns:
  a deep copy of the given value.

### createFixed

public Object createFixed(Object old, Schema schema)
Called to create an fixed value. May be overridden for alternate fixed representations. By default, returns `GenericFixed`.

### createFixed

public Object createFixed(Object old, byte[] bytes, Schema schema)
Called to create an fixed value. May be overridden for alternate fixed representations. By default, returns `GenericFixed`.

### createEnum

public Object createEnum(String symbol, Schema schema)
Called to create an enum value. May be overridden for alternate enum representations. By default, returns a GenericEnumSymbol.

### newRecord

public Object newRecord(Object old, Schema schema)
Called to create new record instances. Subclasses may override to use a different record implementation. The returned instance must conform to the schema provided. If the old object contains fields not present in the schema, they should either be removed from the old object, or it should create a new instance that conforms to the schema. By default, this returns a `GenericData.Record`.

### createString

public Object createString(Object value)
Called to create an string value. May be overridden for alternate string representations.

### newArray

public Object newArray(Object old, int size, Schema schema)
Called to create new array instances. Subclasses may override to use a different array implementation. By default, this returns a `GenericData.Array`.

Parameters:
  `old` - the old array instance to reuse, if possible. If the old array is an appropriate type, it may be cleared and returned.
  `size` - the size of the array to create.
  `schema` - the schema of the array elements.

### optimalValueType

public static Schema.Type optimalValueType(Schema schema, LogicalType logicalType, Class<?> convertedElementType)
Determine the optimal value type for an array. The value type is determined form the convertedElementType if supplied, otherwise the underlying type from the schema

Parameters:
  `schema` - the schema of the array
  `convertedElementType` - the converted elements value type. This may not be the same and the schema if for instance there is a logical type, and a convertor is use
Returns:
  an indicator for the type of the array, useful for `PrimitivesArrays.createOptimizedArray(int, Schema, Schema.Type)`. May be null if the type is not optimised

### newMap

public Object newMap(Object old, int size)
Called to create new array instances. Subclasses may override to use a different map implementation. By default, this returns a `HashMap`.

### getNewRecordSupplier

public GenericData.InstanceSupplier getNewRecordSupplier(Schema schema)
create a supplier that allows to get new record instances for a given schema in an optimized way