SerDe在JAVA中使用Apache AVRO [英] SerDe using Apache AVRO in JAVA
问题描述
我正在尝试序列化和反序列化JSON对象列表
I am trying to serialize and deserialize a list of JSON objects
下面是我的JSON文件
Below is my JSON file
[
{
"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;
}
当我尝试运行以上代码时,文件将被序列化以检查反序列化输出为
When I am trying to run the above code the file is getting serialized to check the deseralization the output is coming as
document@3246fb96
document@2e222612
我不确定用于序列化的代码是否正确或反序列化代码中我出了什么问题
I am not sure if the code for serialization is correct or where I am going wrong in the deserialization code
已推荐: https://liyanxu.blog/2018/02/07/apache-avro-examples/
请提出建议!
推荐答案
您的代码实际上有效,您错过的是在文档类中添加toString()方法.
Your code actually works, what you missed is to add a toString() method to your Document class.
添加类似这样的内容:
@Override
public String toString() {
return String.format("Document ID: {%s}, status: {%s}, site ID: {%s}, site name: {%s}", id, Status, siteId, siteName);
}
到文档类(它实际上应该是带有大写字母的Document).它将打印出您想要的内容,例如以下示例所示的内容:
to the document class (which should really be Document with capital letter). And it will print out what you want, for example something like this given the example:
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.如果您有兴趣,可以在此处进行调查.
Alternatively, you could use the Lombok annotation @ToString. You can look into it here if you are interested.
这篇关于SerDe在JAVA中使用Apache AVRO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!