从某个 Java 对象生成 Avro Schema

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22954315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:35:35  来源:igfitidea点击:

Generate Avro Schema from certain Java Object

javaserializationavrojsonschema

提问by Richard Le

Apache Avro provides a compact, fast, binary data format, rich data structure for serialization. However, it requires user to define a schema (in JSON) for object which need to be serialized.

Apache Avro 为序列化提供了一种紧凑、快速、二进制数据格式、丰富的数据结构。但是,它需要用户为需要序列化的对象定义模式(以 JSON 格式)。

In some case, this can not be possible (e.g: the class of that Java object has some members whose types are external java classes in external libraries). Hence, I wonder there is a tool can get the information from object's .class file and generate the Avro schema for that object (like Gson use object's .class information to convert certain object to JSON string).

在某些情况下,这是不可能的(例如:该 Java 对象的类有一些成员,其类型是外部库中的外部 Java 类)。因此,我想知道有一种工具可以从对象的 .class 文件中获取信息并为该对象生成 Avro 模式(如 Gson 使用对象的 .class 信息将某些对象转换为 JSON 字符串)。

采纳答案by MoustafaAAtta

Take a look at the Java reflection API.

看看Java 反射 API

Getting a schema looks like:

获取架构如下所示:

Schema schema = ReflectData.get().getSchema(T);

See the example from Doug on another question for a working example.

有关工作示例,请参阅Doug 在另一个问题上的示例

Credits of this answer belong to Sean Busby.

这个答案的学分属于肖恩巴斯比。

回答by charlb

Here's how to Generate an Avro Schema from POJO definition

以下是从 POJO 定义生成 Avro Schema 的方法

ObjectMapper mapper = new ObjectMapper(new AvroFactory());
AvroSchemaGenerator gen = new AvroSchemaGenerator();
mapper.acceptJsonFormatVisitor(RootType.class, gen);
AvroSchema schemaWrapper = gen.getGeneratedSchema();
org.apache.avro.Schema avroSchema = schemaWrapper.getAvroSchema();
String asJson = avroSchema.toString(true);

回答by abasar

** Example**

** 例子**

Pojo class

Pojo 类

public class ExportData implements Serializable {
    private String body;
    // ... getters and setters
}

Serialize

连载

File file = new File(fileName);
DatumWriter<ExportData> writer = new ReflectDatumWriter<>(ExportData.class);
DataFileWriter<ExportData> dataFileWriter = new DataFileWriter<>(writer);
Schema schema = ReflectData.get().getSchema(ExportData.class);
dataFileWriter.create(schema, file);
for (Row row : resultSet) {
    String rec = row.getString(0);
    dataFileWriter.append(new ExportData(rec));
}
dataFileWriter.close();

Deserialize

反序列化

File file = new File(avroFilePath);
DatumReader<ExportData> datumReader = new ReflectDatumReader<>(ExportData.class);
DataFileReader<ExportData> dataFileReader = new DataFileReader<>(file, datumReader);
ExportData record = null;
while (dataFileReader.hasNext()){
    record = dataFileReader.next(record);
    // process record
}