public class GenericData extends Object
GenericRecordBuilder
for a
convenient way to build GenericRecord
instances.GenericRecordBuilder
Modifier and Type | Class and Description |
---|---|
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 class |
GenericData.StringType
Used to specify the Java type for a string schema.
|
Modifier and Type | Field and Description |
---|---|
static String |
FAST_READER_PROP |
static String |
STRING_PROP |
protected static String |
STRING_TYPE_STRING |
Constructor and Description |
---|
GenericData()
For subclasses.
|
GenericData(ClassLoader classLoader)
For subclasses.
|
Modifier and Type | Method and 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.
|
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.
|
Conversion<Object> |
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 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) . |
protected int |
hashCodeAdd(int hashCode,
Object o,
Schema s)
Add the hash code for an object into an accumulated hash code.
|
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) |
Object |
newMap(Object old,
int size)
Called to create new array instances.
|
Object |
newRecord(Object old,
Schema schema)
Called to create new record instances.
|
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.
|
public static final String STRING_PROP
protected static final String STRING_TYPE_STRING
public static final String FAST_READER_PROP
public GenericData()
get()
.public GenericData(ClassLoader classLoader)
public static void setStringType(Schema s, GenericData.StringType stringType)
public static GenericData get()
public ClassLoader getClassLoader()
public Collection<Conversion<?>> getConversions()
public void addLogicalTypeConversion(Conversion<?> conversion)
conversion
- a logical type Conversion.public <T> Conversion<T> getConversionByClass(Class<T> datumClass)
datumClass
- a Classpublic <T> Conversion<T> getConversionByClass(Class<T> datumClass, LogicalType logicalType)
datumClass
- a ClasslogicalType
- a LogicalTypepublic Conversion<Object> getConversionFor(LogicalType logicalType)
logicalType
- a logical typepublic GenericData setFastReaderEnabled(boolean flag)
public boolean isFastReaderEnabled()
public FastReaderBuilder getFastReaderBuilder()
public DatumReader createDatumReader(Schema schema)
DatumReader
for this kind of data.public DatumReader createDatumReader(Schema writer, Schema reader)
DatumReader
for this kind of data.public DatumWriter createDatumWriter(Schema schema)
DatumWriter
for this kind of data.public boolean validate(Schema schema, Object datum)
protected void toString(Object datum, StringBuilder buffer, IdentityHashMap<Object,Object> seenObjects)
public void setField(Object record, String name, int position, Object value)
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
.public Object getField(Object record, String name, int position)
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
.protected Object getRecordState(Object record, Schema schema)
getField(Object,String,int,Object)
and
setField(Object,String,int,Object,Object)
on the same record.protected void setField(Object record, String name, int position, Object value, Object state)
setField(java.lang.Object, java.lang.String, int, java.lang.Object)
that has state.protected Object getField(Object record, String name, int pos, Object state)
getField(java.lang.Object, java.lang.String, int)
that has state.public int resolveUnion(Schema union, Object datum)
Schema.getIndexNamed(String)
and getSchemaName(Object)
.protected String getSchemaName(Object datum)
resolveUnion(Schema,Object)
.protected boolean instanceOf(Schema schema, Object datum)
resolveUnion(Schema,Object)
. May be overridden for
alternate data representations.protected boolean isArray(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected Collection getArrayAsCollection(Object datum)
protected boolean isRecord(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected Schema getRecordSchema(Object record)
protected boolean isEnum(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected Schema getEnumSchema(Object enu)
protected boolean isMap(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean isFixed(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected Schema getFixedSchema(Object fixed)
protected boolean isString(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean isBytes(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean isInteger(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean isLong(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean isFloat(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean isDouble(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.protected boolean isBoolean(Object datum)
instanceOf(org.apache.avro.Schema, java.lang.Object)
.public int hashCode(Object o, Schema s)
compare(Object,Object,Schema)
.protected int hashCodeAdd(int hashCode, Object o, Schema s)
public int compare(Object o1, Object o2, Schema s)
BinaryData.compare(byte[], int, byte[], int, Schema)
.protected int compare(Object o1, Object o2, Schema s, boolean equals)
public Object getDefaultValue(Schema.Field field)
field
- the field whose default value should be retrieved.public <T> T deepCopy(Schema schema, T value)
Logical types are converted to raw types, copied, then converted back.
schema
- the schema of the value to deep copy.value
- the value to deep copy.public Object createFixed(Object old, Schema schema)
GenericFixed
.public Object createFixed(Object old, byte[] bytes, Schema schema)
GenericFixed
.public Object createEnum(String symbol, Schema schema)
public Object newRecord(Object old, Schema schema)
GenericData.Record
.public Object createString(Object value)
public Object newMap(Object old, int size)
HashMap
.public GenericData.InstanceSupplier getNewRecordSupplier(Schema schema)
Copyright © 2009–2021 The Apache Software Foundation. All rights reserved.