Search in sources :

Example 1 with SpecificData

use of org.apache.avro.specific.SpecificData in project flink by apache.

the class AvroFactory method getSpecificDataForClass.

/**
 * Creates a {@link SpecificData} object for a given class. Possibly uses the specific data from
 * the generated class with logical conversions applied (avro >= 1.9.x).
 *
 * <p>Copied over from {@code SpecificData#getForClass(Class<T> c)} we do not use the method
 * directly, because we want to be API backwards compatible with older Avro versions which did
 * not have this method
 */
public static <T extends SpecificData> SpecificData getSpecificDataForClass(Class<T> type, ClassLoader cl) {
    try {
        Field specificDataField = type.getDeclaredField("MODEL$");
        specificDataField.setAccessible(true);
        return (SpecificData) specificDataField.get((Object) null);
    } catch (IllegalAccessException e) {
        throw new FlinkRuntimeException("Could not access the MODEL$ field of avro record", e);
    } catch (NoSuchFieldException e) {
        return new SpecificData(cl);
    }
}
Also used : Field(java.lang.reflect.Field) SpecificData(org.apache.avro.specific.SpecificData) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException)

Example 2 with SpecificData

use of org.apache.avro.specific.SpecificData in project camel by apache.

the class AvroDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
    ObjectHelper.notNull(actualSchema, "schema", this);
    ClassLoader classLoader = null;
    Class<?> clazz = camelContext.getClassResolver().resolveClass(actualSchema.getFullName());
    if (clazz != null) {
        classLoader = clazz.getClassLoader();
    }
    SpecificData specificData = new SpecificDataNoCache(classLoader);
    DatumReader<GenericRecord> reader = new SpecificDatumReader<GenericRecord>(null, null, specificData);
    reader.setSchema(actualSchema);
    Decoder decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
    Object result = reader.read(null, decoder);
    return result;
}
Also used : SpecificData(org.apache.avro.specific.SpecificData) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) GenericRecord(org.apache.avro.generic.GenericRecord) Decoder(org.apache.avro.io.Decoder)

Example 3 with SpecificData

use of org.apache.avro.specific.SpecificData in project flink by apache.

the class AvroFactory method fromSpecific.

@SuppressWarnings({ "OptionalUsedAsFieldOrParameterType", "unchecked" })
private static <T> AvroFactory<T> fromSpecific(Class<T> type, ClassLoader cl, Optional<Schema> previousSchema) {
    SpecificData specificData = getSpecificDataForClass((Class<? extends SpecificData>) type, cl);
    Schema newSchema = extractAvroSpecificSchema(type, specificData);
    return new AvroFactory<>(specificData, newSchema, new SpecificDatumReader<>(previousSchema.orElse(newSchema), newSchema, specificData), new SpecificDatumWriter<>(newSchema, specificData));
}
Also used : SpecificData(org.apache.avro.specific.SpecificData) Schema(org.apache.avro.Schema)

Example 4 with SpecificData

use of org.apache.avro.specific.SpecificData in project flink by apache.

the class AvroDeserializationSchema method checkAvroInitialized.

void checkAvroInitialized() {
    if (datumReader != null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
        @SuppressWarnings("unchecked") SpecificData specificData = AvroFactory.getSpecificDataForClass((Class<? extends SpecificData>) recordClazz, cl);
        this.datumReader = new SpecificDatumReader<>(specificData);
        this.reader = AvroFactory.extractAvroSpecificSchema(recordClazz, specificData);
    } else {
        this.reader = new Schema.Parser().parse(schemaString);
        GenericData genericData = new GenericData(cl);
        this.datumReader = new GenericDatumReader<>(null, this.reader, genericData);
    }
    this.inputStream = new MutableByteArrayInputStream();
    this.decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
}
Also used : SpecificData(org.apache.avro.specific.SpecificData) MutableByteArrayInputStream(org.apache.flink.formats.avro.utils.MutableByteArrayInputStream) GenericData(org.apache.avro.generic.GenericData)

Example 5 with SpecificData

use of org.apache.avro.specific.SpecificData in project beam by apache.

the class AvroCoder method of.

/**
 * Returns an {@code AvroCoder} instance for the given class, respecting whether to use Avro's
 * Reflect* or Specific* suite for encoding and decoding.
 *
 * @param <T> the element type
 */
public static <T> AvroCoder<T> of(Class<T> type, boolean useReflectApi) {
    ClassLoader cl = type.getClassLoader();
    SpecificData data = useReflectApi ? new ReflectData(cl) : new SpecificData(cl);
    return of(type, data.getSchema(type), useReflectApi);
}
Also used : ReflectData(org.apache.avro.reflect.ReflectData) SpecificData(org.apache.avro.specific.SpecificData)

Aggregations

SpecificData (org.apache.avro.specific.SpecificData)5 Field (java.lang.reflect.Field)1 Schema (org.apache.avro.Schema)1 GenericData (org.apache.avro.generic.GenericData)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 Decoder (org.apache.avro.io.Decoder)1 ReflectData (org.apache.avro.reflect.ReflectData)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1 MutableByteArrayInputStream (org.apache.flink.formats.avro.utils.MutableByteArrayInputStream)1 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)1