java - 为具有泛型类型的 Java POJO 生成 Avro 架构

标签 java generics reflection avro

我尝试使用以下方法在运行时获取 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 不支持泛型类型。有没有办法可以在运行时生成架构期间从类中省略某些类字段?

最佳答案

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;
    }

关于java - 为具有泛型类型的 Java POJO 生成 Avro 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59091375/

相关文章:

c# - 实现基本接口(interface)问题

c# - 检查类型最快的方法是什么?

c# - 如何在 .net 中使用反射获取对象名称?

java - Maven POM 到 Ivy 描述符 XSLT

java - 使用 Android 进行 Tinify API

c# - JavaScript 中的 SHA256Cng

java - 如何将 Java lambda 表达式用于正则表达式

java - 如何在 Eclipse 中运行通用参数化 JUnit 测试?

c# - 是否可以根据类型实现泛型方法的注册?

c++ - 为什么没有 'this_class' - 'this' 的静态等价物?