【发布时间】:2020-07-01 09:58:40
【问题描述】:
我有一个很大的 json 文件,大小约为 40Gb。当我尝试将此对象数组文件转换为 java 对象列表时,它崩溃了。我已经使用了所有大小的最大堆 xmx,但没有任何效果!
public Set<Interlocutor> readJsonInterlocutorsToPersist() {
String userHome = System.getProperty(USER_HOME);
log.debug("Read file interlocutors "+userHome);
try {
ObjectMapper mapper = new ObjectMapper();
// JSON file to Java object
Set<Interlocutor> interlocutorDeEntities = mapper.readValue(
new File(userHome + INTERLOCUTORS_TO_PERSIST),
new TypeReference<Set<Interlocutor>>() {
});
return interlocutorDeEntities;
} catch (Exception e) {
log.error("Exception while Reading InterlocutorsToPersist file.",
e.getMessage());
return null;
}
}
有没有办法使用BufferedReader 读取此文件,然后逐个对象推送?
【问题讨论】:
-
如果是 40GB 的 JSON,我怀疑整个数据集是否适合您的记忆。甚至曾经反序列化为一组对象。
-
可以用
JsonParser.nextToken()编写你的自我解析器 -
流式 API 可用如下:sites.google.com/site/gson/streaming 这些将在将字符串数据 (json) 反编译为 Java 对象后立即处理。
-
为什么需要它作为对象列表?