【发布时间】:2019-11-28 14:45:27
【问题描述】:
我正在尝试使用以下方法在运行时获取 Avro Schema:
private Schema getSchema(Class clazz) {
Schema s = ReflectData.get().getSchema(clazz);
AvroSchema avroSchema = new AvroSchema(s);
return avroSchema.getAvroSchema();
}
但是由于我的 POJO 类包含如下泛型:
public abstract class Data<T> implements Serializable {
private static final long serialVersionUID = 1L;
private String dataType;
private T id;
public Data() {
}
public Data(String dataType) {
this.dataType = dataType;
}
public Data(String dataType, T id) {
this.dataType = dataType;
this.id = id;
}
}
我得到以下异常:
Exception in thread "main" org.apache.avro.AvroRuntimeException: avro.shaded.com.google.common.util.concurrent.UncheckedExecutionException: org.apache.avro.AvroTypeException: Unknown type: T
at org.apache.avro.specific.SpecificData.getSchema(SpecificData.java:227)
我了解 Avro 不支持泛型类型。有没有办法可以在运行时生成模式时从我的类中省略某些类字段?
【问题讨论】:
标签: java generics reflection avro