Can Apache Avro handle parameterized types during serialization?
I see this exception thrown from the Avro environment when I try to serialize an instance that uses generics -
org.apache.avro.AvroTypeException: Unknown type: T
at org.apache.avro.specific.SpecificData.createSchema(SpecificData.java:255)
at org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:514)
at org.apache.avro.reflect.ReflectData.createFieldSchema(ReflectData.java:593)
at org.apache.avro.reflect.ReflectData$AllowNull.createFieldSchema(ReflectData.java:75)
at org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:472)
at org.apache.avro.specific.SpecificData.getSchema(SpecificData.java:189)
The class I'm trying to serialize is as follows
public class Property<T> {
private T propertyValue;
}
I am trying to generate an on-the-fly schema based on an incoming POJO instance. My serialization code is as follows:
ByteArrayOutputStream os = new ByteArrayOutputStream();
ReflectData reflectData = ReflectData.AllowNull.get();
Schema schema = reflectData.getSchema(propertyValue.getClass());
DatumWriter<T> writer = new ReflectDatumWriter<T>(schema);
Encoder encoder = EncoderFactory.get().jsonEncoder(schema, os);
writer.write(propertyValue, encoder);
A line in my code that throws an exception:
Schema schema = reflectData.getSchema(propertyValue.getClass());
The same code is great for classes that do not have parameterized types.
source
share