杰克逊多个对象和巨大的json文件 [英] Jackson multiple objects and huge json files

查看:79
本文介绍了杰克逊多个对象和巨大的json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得答案可能与此重复:杰克逊 - Json到POJO有多个条目但我认为这个问题可能不同。我也使用原始数据绑定而不是完整数据绑定。

I get the feeling that the answer might be a duplicate of this: Jackson - Json to POJO With Multiple Entries but I think that potentially the question is different enough. Also I'm using raw data binding rather than full data binding.

所以就像那个问题的提问者一样,我在文件中有多个对象,我正在尝试将它们变成POJO并将它们填入我设计的数据库中,以便我可以快速而不是慢慢地访问数据。

So like the asker of that question, I have multiple objects in a file and I'm trying to turn them into POJOs and stuff them into a database of my design so I can access the data quickly rather than slowly.

这里的文件大小为几十GB,每个文件中有多达数百万个对象。无论如何,这是我到目前为止:

The files here are in the order of tens of GB, with up to millions of objects in each file. Anyway here is what I have so far:

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> data = mapper.readValue(new File("foo.json"), Map.class);
System.out.println(data.get("bar"));

这对于打印foo中第一个对象的bar元素非常有用,但我需要一种方法以一种不会耗尽我所有记忆的方式遍历每个元素。

And this works great for printing the bar element of the first object in foo, but I need a way to iterate through every element in a way that won't eat up all my memory.

谢谢。

推荐答案

使用此代码示例查看基本概念。

Use this code sample to see the basic idea.

final InputStream in = new FileInputStream("json.json");
try {
  for (Iterator it = new ObjectMapper().readValues(
      new JsonFactory().createJsonParser(in), Map.class); it.hasNext();)
    System.out.println(it.next());
}
finally { in.close();} }

这篇关于杰克逊多个对象和巨大的json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆