Package org.apache.avro.generic
Class GenericData
java.lang.Object
org.apache.avro.generic.GenericData
- Direct Known Subclasses:
ProtobufData
,SpecificData
,ThriftData
Utilities for generic Java data. See
GenericRecordBuilder
for a
convenient way to build GenericRecord
instances.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static class
Default implementation of an array.static class
Default implementation ofGenericEnumSymbol
.static class
Default implementation ofGenericFixed
.static interface
static class
Default implementation ofGenericRecord
.static enum
Used to specify the Java type for a string schema. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addLogicalTypeConversion
(Conversion<?> conversion) Registers the given conversion to be used when reading and writing with this data model.int
Compare objects according to their schema.protected int
Comparison implementation.protected int
compareMaps
(Map<?, ?> m1, Map<?, ?> m2) createDatumReader
(Schema schema) Returns aDatumReader
for this kind of data.createDatumReader
(Schema writer, Schema reader) Returns aDatumReader
for this kind of data.createDatumWriter
(Schema schema) Returns aDatumWriter
for this kind of data.createEnum
(String symbol, Schema schema) Called to create an enum value.createFixed
(Object old, byte[] bytes, Schema schema) Called to create an fixed value.createFixed
(Object old, Schema schema) Called to create an fixed value.createString
(Object value) Called to create an string value.<T> T
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.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<?>> 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.Called byGenericDatumReader.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
Version ofgetField(java.lang.Object, java.lang.String, int)
that has state.protected Schema
getFixedSchema
(Object fixed) Called to obtain the schema of a fixed.getNewRecordSupplier
(Schema schema) create a supplier that allows to get new record instances for a given schema in an optimized wayCalled 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 togetField(Object,String,int,Object)
andsetField(Object,String,int,Object,Object)
on the same record.protected String
getSchemaName
(Object datum) Return the schema full name for a datum.int
Compute a hash code according to a schema, consistent withcompare(Object,Object,Schema)
.Create a schema given an example datum.protected boolean
instanceOf
(Schema schema, Object datum) Called byresolveUnion(Schema,Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.boolean
protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
.Called to create new array instances.Called to create new record instances.int
resolveUnion
(Schema union, Object datum) Return the index for a datum within a union.setFastReaderEnabled
(boolean flag) void
Called byGenericDatumReader.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
Version ofsetField(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.Renders a Java datum as JSON.protected void
toString
(Object datum, StringBuilder buffer, IdentityHashMap<Object, Object> seenObjects) Renders a Java datum as JSON.boolean
Returns true if a Java datum matches a schema.
-
Field Details
-
STRING_PROP
- See Also:
-
STRING_TYPE_STRING
- See Also:
-
FAST_READER_PROP
- See Also:
-
-
Constructor Details
-
GenericData
public GenericData()For subclasses. Applications normally useget()
. -
GenericData
For subclasses. GenericData does not use a ClassLoader.
-
-
Method Details
-
setStringType
Set the Java type to be used when reading this schema. Meaningful only only string schemas and map schemas (for the keys). -
get
Return the singleton instance. -
getClassLoader
Return the class loader that's used (by subclasses). -
getConversions
-
addLogicalTypeConversion
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 classConversion<T>
.- Parameters:
conversion
- a logical type Conversion.
-
getConversionByClass
Returns the first conversion found for the given class.- Parameters:
datumClass
- a Class- Returns:
- the first registered conversion for the class, or null
-
getConversionByClass
Returns the conversion for the given class and logical type.- Parameters:
datumClass
- a ClasslogicalType
- a LogicalType- Returns:
- the conversion for the class and logical type, or null
-
getConversionFor
Returns the Conversion for the given logical type.- Parameters:
logicalType
- a logical type- Returns:
- the conversion for the logical type, or null
-
setFastReaderEnabled
-
isFastReaderEnabled
public boolean isFastReaderEnabled() -
getFastReaderBuilder
-
createDatumReader
Returns aDatumReader
for this kind of data. -
createDatumReader
Returns aDatumReader
for this kind of data. -
createDatumWriter
Returns aDatumWriter
for this kind of data. -
validate
Returns true if a Java datum matches a schema. -
toString
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
Create a schema given an example datum. -
setField
Called byGenericDatumReader.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 forIndexedRecord
. -
getField
Called byGenericDatumReader.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 forIndexedRecord
. -
getRecordState
Produce state for repeated calls togetField(Object,String,int,Object)
andsetField(Object,String,int,Object,Object)
on the same record. -
setField
Version ofsetField(java.lang.Object, java.lang.String, int, java.lang.Object)
that has state. -
getField
Version ofgetField(java.lang.Object, java.lang.String, int)
that has state. -
resolveUnion
Return the index for a datum within a union. Implemented withSchema.getIndexNamed(String)
andgetSchemaName(Object)
. -
getSchemaName
Return the schema full name for a datum. Called byresolveUnion(Schema,Object)
. -
getPrimitiveTypeCache
Called to obtain the primitive type cache. May be overridden for alternate record representations. -
instanceOf
Called byresolveUnion(Schema,Object)
. May be overridden for alternate data representations. -
isArray
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
getArrayAsCollection
Called to access an array as a collection. -
isRecord
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
getRecordSchema
Called to obtain the schema of a record. By default calls {GenericContainer#getSchema(). May be overridden for alternate record representations. -
isEnum
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
getEnumSchema
Called to obtain the schema of a enum. By default calls {GenericContainer#getSchema(). May be overridden for alternate enum representations. -
isMap
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
isFixed
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
getFixedSchema
Called to obtain the schema of a fixed. By default calls {GenericContainer#getSchema(). May be overridden for alternate fixed representations. -
isString
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
isBytes
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
isInteger
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
isLong
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
isFloat
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
isDouble
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
isBoolean
Called by the default implementation ofinstanceOf(org.apache.avro.Schema, java.lang.Object)
. -
hashCode
Compute a hash code according to a schema, consistent withcompare(Object,Object,Schema)
. -
compare
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 ofBinaryData.compare(byte[], int, byte[], int, Schema)
. -
compareMaps
-
compare
Comparison implementation. When equals is true, only checks for equality, not for order. -
getDefaultValue
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
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
Called to create an fixed value. May be overridden for alternate fixed representations. By default, returnsGenericFixed
. -
createFixed
Called to create an fixed value. May be overridden for alternate fixed representations. By default, returnsGenericFixed
. -
createEnum
Called to create an enum value. May be overridden for alternate enum representations. By default, returns a GenericEnumSymbol. -
newRecord
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 aGenericData.Record
. -
createString
Called to create an string value. May be overridden for alternate string representations. -
newArray
-
newMap
Called to create new array instances. Subclasses may override to use a different map implementation. By default, this returns aHashMap
. -
getNewRecordSupplier
create a supplier that allows to get new record instances for a given schema in an optimized way
-