JAVA - 解析巨大(超大)JSON文件的最佳方法

Dax*_*Dax 44 java json gson

我试图解析一些巨大的JSON文件(如http://eu.battle.net/auction-data/258993a3c6b974ef3e6f22ea6f822720/auctions.json使用GSON库()http://code.google.com/p/google- gson /)在JAVA中.

我想知道什么是解析这种大文件(大约80k行)的最佳approch,如果你可能知道可以帮助我处理这个的好API.

一些想法......

  1. 逐行阅读并摆脱JSON格式:但这是无稽之谈.
  2. 通过将此文件拆分为许多其他文件来减少JSON文件:但我没有找到任何好的Java API.
  3. 直接使用此文件作为nonSql数据库,保留文件并将其用作我的数据库.

我真的很感谢adices/help/messages/:-)谢谢.

vik*_*iii 39

我建议看看Jackson Api,很容易将流式和树型解析选项结合起来:您可以以流式方式整个文件移动,然后将单个对象读入树结构.

举个例子,我们采取以下输入:

{ 
  "records": [ 
    {"field1": "aaaaa", "bbbb": "ccccc"}, 
    {"field2": "aaa", "bbb": "ccc"} 
  ] ,
  "special message": "hello, world!" 
}
Run Code Online (Sandbox Code Playgroud)

想象一下,字段稀疏或记录结构更复杂.

以下代码段说明了如何使用流和树模型解析的组合读取此文件.每个单独的记录都以树形结构读取,但文件永远不会完整地读入内存,因此可以在使用最少内存的情况下处理大小为千兆字节的JSON文件.

import org.codehaus.jackson.map.*;
import org.codehaus.jackson.*;

import java.io.File;

public class ParseJsonSample {
    public static void main(String[] args) throws Exception {
        JsonFactory f = new MappingJsonFactory();
        JsonParser jp = f.createJsonParser(new File(args[0]));
        JsonToken current;
        current = jp.nextToken();
        if (current != JsonToken.START_OBJECT) {
            System.out.println("Error: root should be object: quiting.");
            return;
        }
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            // move from field name to field value
            current = jp.nextToken();
            if (fieldName.equals("records")) {
                if (current == JsonToken.START_ARRAY) {
                    // For each of the records in the array
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        // read the record into a tree model,
                        // this moves the parsing position to the end of it
                        JsonNode node = jp.readValueAsTree();
                        // And now we have random access to everything in the object
                        System.out.println("field1: " + node.get("field1").getValueAsText());
                        System.out.println("field2: " + node.get("field2").getValueAsText());
                    }
                } else {
                    System.out.println("Error: records should be an array: skipping.");
                    jp.skipChildren();
                }
            } else {
                System.out.println("Unprocessed property: " + fieldName);
                jp.skipChildren();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以猜到,每次nextToken()调用都会给出下一个解析事件:start object,start field,start array,start object,...,end object,...,end array,...

jp.readValueAsTree()调用允许将当前解析位置(JSON对象或数组)中的内容读入Jackson的通用JSON树模型中.一旦你有了这个,你可以随机访问数据,无论事物在文件中出现的顺序如何(在示例中field1和field2并不总是以相同的顺序).Jackson也支持映射到您自己的Java对象上.jp.skipChildren()很方便:它允许跳过一个完整的对象树或数组,而不必自己运行它包含的所有事件.


Jes*_*son 36

你不需要切换到杰克逊.Gson 2.1引入了一个新的TypeAdapter接口,允许混合树和流序列化和反序列化.

API高效灵活.有关组合树和绑定模式的示例,请参阅Gson的Streaming doc.这比混合流和树模式严格要好; 通过绑定,您不会浪费内存来构建您的值的中间表示.

像杰克逊一样,Gson拥有递归跳过不需要的值的API; Gson称之为skipValue().