2
Apache Avro可以在序列化期間處理參數化類型嗎?Apache Avro框架可以在序列化期間處理參數化類型嗎?
我看到這個異常從Avro的框架拋出,當我試圖序列使用泛型實例 -
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)
類我試圖序列看起來像這樣
public class Property<T> {
private T propertyValue;
}
我想基於傳入的POJO實例即時生成模式。我的序列化代碼看起來是這樣的 -
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);
線在我的代碼觸發異常:
Schema schema = reflectData.getSchema(propertyValue.getClass());
相同的代碼工作正常,沒有參數化類型的類。