【问题标题】:Generate Avro Schema for Java POJO with Generic Types为具有泛型类型的 Java POJO 生成 Avro Schema
【发布时间】: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


    【解决方案1】:
    private <T> String writePojoToParquet(List<T> pojos, String fileKey){
            String fileName = fileKey + ".parquet";
            Path path = new Path(fileName.replace("/", "_"));
            //No matter what delete file always.
            String strPath = path.toString();
            FileUtils.delete(strPath);
            FileUtils.delete(strPath + ".crc");
            logger.debug("Writing data to parquet file {}", strPath);
            Configuration conf = new Configuration();
            try (ParquetWriter<T> writer =
                         AvroParquetWriter.<T>builder(path)
                                 .withSchema(ReflectData.AllowNull.get().getSchema(pojos.get(0).getClass()))
                                 .withDataModel(ReflectData.get())
                                 .withConf(conf)
                                 .withCompressionCodec(CompressionCodecName.SNAPPY)
                                 .withWriteMode(ParquetFileWriter.Mode.OVERWRITE)
                                 .enableValidation()
                                 .enableDictionaryEncoding()
                                 .build()) {
                for (T p : pojos) {
                    writer.write(p);
                }
                return strPath;
            } catch (IOException e) {
                logger.error("Error while writing data to parquet file {}.", strPath, e);
            }
            return null;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多