java中,通过FastJson对海量数据的Json文件,边读取边解析
2015-02-10 09:44
1276 查看
当读取json文件的时候,如果json文件巨大,比如json文件中有900万条数据,大小有300多M,就不可以一次把数据都读到内存再解析。
第一内存受不了,第二CPU更受不了,所有的硬件和软件都受不了。
需要一种边读取,边解析的json操作类,FastJson可以实现这个功能,实测这个速度还真是比较快。
fastjson的下载地址:http://repo1.maven.org/maven2/com/alibaba/fastjson/
建议下载最新版本的fastjson
比如有下边一段json:
通过JsonReader可以顺序读取。类似于剥橘子,一层一层解开json,不需要把json都读到内存中。
其中比较重要的几个方法为:
startArray(); 开始解析数组
endArray(); 结束解析数组
startObject(); 开始解析键值对
endObject(); 结束解析键值对
只要掌握了以上几个关键的方法,FastJson大数据读取导入就不难。
还有一些其他的json大数据读取导入技术,后续介绍。
第一内存受不了,第二CPU更受不了,所有的硬件和软件都受不了。
需要一种边读取,边解析的json操作类,FastJson可以实现这个功能,实测这个速度还真是比较快。
fastjson的下载地址:http://repo1.maven.org/maven2/com/alibaba/fastjson/
建议下载最新版本的fastjson
比如有下边一段json:
{ "array": [ 1, 2, 3 ], "arraylist": [ { "a": "b", "c": "d", "e": "f" }, { "a": "b", "c": "d", "e": "f" }, { "a": "b", "c": "d", "e": "f" } ], "object": { "a": "b", "c": "d", "e": "f" }, "string": "Hello World" }如果我们不一次性将其读到内存中进行解析,就要使用FastJson的JSONReader类,解析代码如下:
/** * FastJson逐行解析json * @author drlyee * @date 2015-02-10 */ public void ReadWithFastJson() { String jsonString = "{\"array\":[1,2,3],\"arraylist\":[{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"}],\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"string\":\"HelloWorld\"}"; // 如果json数据以形式保存在文件中,用FileReader进行流读取!! // path为json数据文件路径!! // JSONReader reader = new JSONReader(new FileReader(path)); // 为了直观,方便运行,就用StringReader做示例! JSONReader reader = new JSONReader(new StringReader(jsonString)); reader.startObject(); System.out.print("start fastjson"); while (reader.hasNext()) { String key = reader.readString(); System.out.print("key " + key); if (key.equals("array")) { reader.startArray(); System.out.print("start " + key); while (reader.hasNext()) { String item = reader.readString(); System.out.print(item); } reader.endArray(); System.out.print("end " + key); } else if (key.equals("arraylist")) { reader.startArray(); System.out.print("start " + key); while (reader.hasNext()) { reader.startObject(); System.out.print("start arraylist item"); while (reader.hasNext()) { String arrayListItemKey = reader.readString(); String arrayListItemValue = reader.readObject().toString(); System.out.print("key " + arrayListItemKey); System.out.print("value " + arrayListItemValue); } reader.endObject(); System.out.print("end arraylist item"); } reader.endArray(); System.out.print("end " + key); } else if (key.equals("object")) { reader.startObject(); System.out.print("start object item"); while (reader.hasNext()) { String objectKey = reader.readString(); String objectValue = reader.readObject().toString(); System.out.print("key " + objectKey); System.out.print("value " + objectValue); } reader.endObject(); System.out.print("end object item"); } else if (key.equals("string")) { System.out.print("start string"); String value = reader.readObject().toString(); System.out.print("value " + value); System.out.print("end string"); } } reader.endObject(); System.out.print("start fastjson"); }
通过JsonReader可以顺序读取。类似于剥橘子,一层一层解开json,不需要把json都读到内存中。
其中比较重要的几个方法为:
startArray(); 开始解析数组
endArray(); 结束解析数组
startObject(); 开始解析键值对
endObject(); 结束解析键值对
只要掌握了以上几个关键的方法,FastJson大数据读取导入就不难。
还有一些其他的json大数据读取导入技术,后续介绍。
相关文章推荐
- java解析dbf之通过javadbf包生成和读取dbf文件
- java通过ftp方式读取文件,并解析入库
- java通过ftp方式读取文件,并解析入库
- java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法
- Java fastjson 解析json配置文件
- java通过ftp方式读取文件,并解析入库
- java读取文件内容,解析Json格式数据
- java通过poi解析Excel文件,直接读取客户端文件
- java 解析jar 文件,读取并进行调用
- 通过java提供的URL类包读取网上的文件
- Java中解析相对路径下文件[按行读取文件]
- 通过java提供的URL类包读取网上的文件
- java解析xml文件读取本地dtd或者忽略dtd
- java通过判断文本的编码方式再读取文件
- 一个解析Json文件的java实现
- 通过java提供的URL类包读取网上的文件
- Java解析DBF文件,错误Failed to parse Number: For input string: "-.---" 或读取不出数据或乱码问题都在这里解决
- java解析json格式文件
- java类:读文件类, 读取、创建xml文件, 填充jsp文件,数据转换为json类型
- JAVA解析XML配置文件的读取操作