Can the Apache Avro framework handle parameterized types during serialization?

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.

+2
source share
1 answer

Avro 1.7.7 - AVRO-1571. , Avro :

private static final String PROPERTY_STRING_SCHEMA =
    "{ " +
      "\"type\": \"record\", " +
      "\"name\": \"PropertyString\", " +
      "\"fields\": [" +
        "{ \"name\": \"propertyValue\", \"type\": \"string\" }" +
      "] " +
    "}";

@AvroSchema(PROPERTY_STRING_SCHEMA)
private Property<String> password; 
+2

Source: https://habr.com/ru/post/1611688/


All Articles