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);
}
}
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;
}
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));
}
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);
}
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);
}
Aggregations