现在json的第三方的解析工具很多,我拿了3个比较常用的做了下简单的测试,发现jackson是大数据json的解析王者,也许这得力他基于流的方式。
测试用列,10000000的json文本文件,一行是一个复杂的json对象如下:
{"app":"yyhlweb","events":[{"type":"come","target":"com.youyuan.yyhl.activity.MainActivity","time":"2013-09-06 12:35:59"},{"type":"cbtn","target":"phoneBack","time":"2013-09-06 12:37:09"},{"type":"cbtn","target":"dialogExit","time":"2013-09-06 12:37:11"},{"type":"leave","target":"","time":"2013-09-06 12:37:11"}],"clientData":{"fid":"45001","platform":"android","os_ver":"4.2.2","imei":"356521051907413","userid":"117614041","ua":"GT-I9200","imsi":"460020394943982","resolution":"720x1280","net":"wifi","version":"40040003"}}
转成java对象,
1.首先是gson:
https://code.google.com/p/google-gson/ - //10000000,565,891s
- @Test
- public void testComplex() throws IOException {
- long start = System.currentTimeMillis();
- String content = "";
- Gson gson = new GsonBuilder().create();
- while ((content = reader.readLine()) != null) {
- //System.out.println(content);
- Behaviour behaviour = gson.fromJson(content, Behaviour.class);
- //System.out.println(behaviour.getApp());
- }
- reader.close();
- System.out.println((System.currentTimeMillis() - start) + "ms");
- }
gosn在处理一千万的的数据耗时
565.891秒;
2.然后是jackson,
http://jackson.codehaus.org/ - //10000000,57,963ms
- @Test
- public void testReadJsonToObject() throws JsonSyntaxException, IOException {
- long start = System.currentTimeMillis();
- String content = "";
- while ((content = reader.readLine()) != null) {
- //System.out.println(content);
- Behaviour behaviour = objectMapper.readValue(content, Behaviour.class);
- //System.out.println(behaviour.getApp());
- }
- System.out.println((System.currentTimeMillis() - start) + "ms");
- }
jackson处理花了
57.963秒,居然是gson的十倍,喔!
3.接下来是json-lib
: http://json-lib.sourceforge.net/ 这里面json-lib是比较老牌的解析技术了,使用起来也很方便,在使用上来说三者都很方便。
- //10000000,1498,733ms
- @Test
- public void testComplex() throws IOException {
- long start = System.currentTimeMillis();
- String content = "";
- JsonConfig config = new JsonConfig();
- config.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {
- @Override
- public String transformToJavaIdentifier(String str) {
- char[] chars = str.toCharArray();
- chars[0] = Character.toLowerCase(chars[0]);
- return new String(chars);
- }
- });
- config.setRootClass(Behaviour.class);
-
- while ((content = reader.readLine()) != null) {
- //System.out.println(content);
- Behaviour behaviour = (Behaviour)JSONObject.toBean(JSONObject.fromObject(content,config), Behaviour.class);
- //System.out.println(behaviour.getEvents().size());
- }
- System.out.println((System.currentTimeMillis() - start) + "ms");
- }
结果出来了,json-lib耗时1498.733秒,jackson在大数据的表现相当突出。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。