Java 如何读取大型JSON文件?
预期产出: 关键字:S3Url 值:Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_excel中的2_diff.pdfJava 如何读取大型JSON文件?,java,json,json-simple,Java,Json,Json Simple,预期产出: 关键字:S3Url 值:Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_excel中的2_diff.pdf How to read a large JSON file ? { "Count": 361888, "Items": [ { "S3Url": {"S": Grouper/1904/1/private/drafts/D1_2/siepon_D1_
How to read a large JSON file ?
{
"Count": 361888,
"Items":
[
{ "S3Url": {"S": Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_2_diff.pdf" },
"JSONFile": {"S": Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_2_diff.pdf.json" },
"ErrTs": {"N": "1488010286704"}
},
{ "S3Url": {"S": Mentor/47200043/Public/07/11-07-1984-05-000s-june-2007-mesh-ad-hoc- agenda.ppt.pdf" },
"JSONFile": {"S": "Mentor/47200043/Public/07/11-07-1984-05-000s-june-2007- mesh-ad-hoc-agenda.ppt.pdf.json"},
"ErrTs": {"N": "1490497271699"}
}
],
"ScannedCount": 23
}
This is the input JSON File format. File is too large so cannot use:
*Jsonparser parser=new Jsonparser();
*Object obj=parser.parse(new FileReader(JSON_FILE_PATH))
Error is :
java.lang.OutOfMemoryError: Java heap space
increase the maximum heap size by using JVM options "-Xmx512M" won't work.
tried the code :
jsonParser.parse(new FileReader(JSON_FILE_PATH), new ContentHandler() {
private String key;
private Object value;
// A bunch of "default" methods
@Override public void startJSON() { }
@Override public void endJSON() { }
@Override public boolean startObject() { return true; }
@Override public boolean endObject() { return true; }
@Override public boolean startArray() { return true; }
@Override public boolean endArray() { return true; }
@Override
public boolean startObjectEntry(final String key) {
this.key = key;
return true;
}
@Override
public boolean endObjectEntry() {
System.out.println(key + " => " + value);
return true;
}
@Override
public boolean primitive(final Object value) {
this.value = value;
return true;
}
});
}
实际产量:
关键字:S
值:Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_excel中的2_diff.pdf
关键字:S
值:Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_excel中的2_diff.pdf
How to read a large JSON file ?
{
"Count": 361888,
"Items":
[
{ "S3Url": {"S": Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_2_diff.pdf" },
"JSONFile": {"S": Grouper/1904/1/private/drafts/D1_2/siepon_D1_2/siepon_C11_D1_2_diff.pdf.json" },
"ErrTs": {"N": "1488010286704"}
},
{ "S3Url": {"S": Mentor/47200043/Public/07/11-07-1984-05-000s-june-2007-mesh-ad-hoc- agenda.ppt.pdf" },
"JSONFile": {"S": "Mentor/47200043/Public/07/11-07-1984-05-000s-june-2007- mesh-ad-hoc-agenda.ppt.pdf.json"},
"ErrTs": {"N": "1490497271699"}
}
],
"ScannedCount": 23
}
This is the input JSON File format. File is too large so cannot use:
*Jsonparser parser=new Jsonparser();
*Object obj=parser.parse(new FileReader(JSON_FILE_PATH))
Error is :
java.lang.OutOfMemoryError: Java heap space
increase the maximum heap size by using JVM options "-Xmx512M" won't work.
tried the code :
jsonParser.parse(new FileReader(JSON_FILE_PATH), new ContentHandler() {
private String key;
private Object value;
// A bunch of "default" methods
@Override public void startJSON() { }
@Override public void endJSON() { }
@Override public boolean startObject() { return true; }
@Override public boolean endObject() { return true; }
@Override public boolean startArray() { return true; }
@Override public boolean endArray() { return true; }
@Override
public boolean startObjectEntry(final String key) {
this.key = key;
return true;
}
@Override
public boolean endObjectEntry() {
System.out.println(key + " => " + value);
return true;
}
@Override
public boolean primitive(final Object value) {
this.value = value;
return true;
}
});
}
此错误可能是由内存泄漏引起的 如何解决java.lang.OutOfMemoryError:java堆空间 1) 解决java中OutOfMemoryError的一个简单方法是使用JVM选项“-Xmx512M”增加最大堆大小,这将立即解决OutOfMemoryError。当我在构建项目时遇到Eclipse、Maven或ANT中的内存错误时,这是我首选的解决方案,因为根据项目的大小,很容易耗尽内存,如果您在java应用程序中设置堆大小,那么最好将-Xmx与-Xms的比率保持为1:1或1:1.5 导出JVM_ARGS=“-Xms1024m-Xmx1024m” 2) 在Java中解决OutOfMemoryError的第二种方法相当困难,当您没有太多内存时,即使增加了最大堆大小,您仍然会得到Java.lang.OutOfMemoryError,在这种情况下,您可能希望评测应用程序并查找任何内存泄漏。您可以使用Eclipse内存分析器检查堆转储,也可以使用任何探查器,如Netbeans或JProbe。这是一个艰难的解决方案,需要一些时间来分析和发现内存泄漏 用于调查和修复Java中OutOfMemoryError的工具 1) 视觉气相色谱 2) Jmap 3) 杰哈特 4) Eclipse内存分析器 5) 学习仿形的书籍
阅读更多信息:您遇到此错误是因为JVM无法分配足够的内存来存储结果
JSONObject
实例,该实例是HashMap
的子类(根据stacktrace,这一点很清楚)。
尽管您声称有一个400MB的JSON文档,但与其他JSON文档相比,它可能更小,增加内存大小对您没有多大帮助。
从JVM资源的角度来看,您可以使用流式处理几乎零成本地解析给定的JSON文档,但您必须编写更复杂的代码。
com.googlecode.json-simple:json-simple
支持通过使用ContentHandler
s进行流式读取
例如:
which is repeating. So please help to read the large Json file in required format.
try(final Reader=getPackageResourceReader(q4344452.class,“document.json”)){
final JSONParser JSONParser=new JSONParser();
parse(reader,newcontenthandler()){
私钥;
私人客体价值;
//一堆“默认”方法
@重写公共void startJSON(){}
@重写公共void endJSON(){}
@重写公共布尔startObject(){return true;}
@重写公共布尔内对象(){return true;}
@重写公共布尔startArray(){return true;}
@重写公共布尔endArray(){return true;}
@凌驾
公共布尔startObjectEntry(最终字符串键){
this.key=key;
返回true;
}
@凌驾
公共布尔值endObjectEntry(){
System.out.println(键+“=>”+值);
返回true;
}
@凌驾
公共布尔基元(最终对象值){
这个值=值;
返回true;
}
});
}
当然,这是一个非常原始的示例,这对您来说是有代价的,而不是JVM,但是您可以使用这种方法解析无限的JSON流
输出:
foo=>1bar=>2
文件非常大吗?是的,文件大小大约为400 MB。我想这会对youMate有所帮助。我相信你知道这不是一个“为我编写代码”的服务,所以我向你推荐了如何处理JSON解析的巨大问题。现在你把它修改成另一个问题借用我答案中的代码。您必须自己实现
ContentHandler
。我只能给你一个提示:如果你仍然有OutOfMemoryError
,那么你可能正在将解析的数据收集到内存中,而不是在其他地方写入whem(仍然不清楚——没有stacktrace).你能给我一个链接吗?我无法测试你的code@PranjalAhluwalia您面临什么问题?@Lyubomyr Shaydariv问题是我正在从文件中读取,因此jsonParser.parse(reader,new ContentHandler(){正在抛出错误,并且无法为reader reader=getPackageResourceReader(Q4344452.class,“document.json”)我正在使用Java1。7@PranjalAhluwalia这是一个我用来提取捆绑资源的自定义读卡器。请将上面的读卡器
实例替换为您的新文件读卡器(JSON\u FILE\u PATH)
。