在 JAVA 中使用 Apache AVRO 的 SerDe

SerDe using Apache AVRO in JAVA

我正在尝试序列化和反序列化 JSON 个对象的列表

下面是我的JSON文件

[
    {
        "id": "01",
        "Status": "Open",
        "siteId": "01",
        "siteName": "M1"
    },
    {
        "id": "02",
        "Status": "Open",
        "siteId": "02",
        "siteName": "M2"
    },
    {
        "id": "03",
        "Status": "Open",
        "siteId": "03",
        "siteName": "M3"
    }
]

代码:

public static void main(String args[]) throws IOException {
        //creating schema
        Schema schema = ReflectData.get().getSchema(document.class);
        System.out.println("schema created: "+ schema.toString());

        //get JSON list
        List<document> list = getJsonList();


        File fileR = new File("<<path>>/jsonlist.avro");

        //Serialize objects to file
        DatumWriter<document> writerR = new ReflectDatumWriter(document.class);  // Serialize objects to in-memory binary data
        DataFileWriter<document> outR = new DataFileWriter(writerR).create(schema, fileR);    // Write binary data to file
        for(int i = 0 ;i<list.size();i++)
        {
            outR.append((document) list.get(i));
        }

        outR.close();
        System.out.println("Serialize objects to file...\n");

        //Deserialize objects from file
        DatumReader<document> readerR = new ReflectDatumReader(document.class);
        DataFileReader<document> inR = new DataFileReader(fileR, readerR);
        System.out.println("Deserialize objects from file...");
        for(document doc : inR) {
            System.out.println(doc.toString());
        }
        inR.close();

}
private static List<document> getJsonList() throws IOException {
        final ObjectMapper objectMapper = new ObjectMapper();
        String fileName = "jsonList.json";
        ClassLoader classLoader = <<className>>.class.getClassLoader();
        File file = new File(classLoader.getResource(fileName).getFile());
        System.out.println("File Found : " + file.exists());

        //list of json objects
        List<document> list = objectMapper.readValue(file,new TypeReference<List<document>>(){});
        returnlist;
    }

当我尝试 运行 上面的代码时,文件正在序列化以检查反序列化 输出为

document@3246fb96
document@2e222612

我不确定序列化代码是否正确或反序列化代码哪里出错了

转介: https://liyanxu.blog/2018/02/07/apache-avro-examples/

求推荐!!

您的代码确实有效,您错过的是将 toString() 方法添加到您的文档 class。

添加如下内容:

@Override
public String toString() {
    return String.format("Document ID: {%s}, status: {%s}, site ID: {%s}, site name: {%s}", id, Status, siteId, siteName);
}

到文档 class (实际上应该是大写字母的文档)。 它会打印出你想要的东西,例如像这样的例子:

Deserialize objects from file...
Document ID: {01}, status: {Open}, site ID: {01}, site name: {M1}
Document ID: {02}, status: {Open}, site ID: {02}, site name: {M2}
Document ID: {03}, status: {Open}, site ID: {03}, site name: {M3}

或者,您可以使用 Lombok 注释 @ToString。有兴趣可以去看看here