Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6276866
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: Java

2013-09-11 12:10:56

        现在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:
       

点击(此处)折叠或打开

  1.     //10000000,565,891s
  2.     @Test
  3.     public void testComplex() throws IOException {
  4.         long start = System.currentTimeMillis();
  5.         String content = "";
  6.         Gson gson = new GsonBuilder().create();
  7.         while ((content = reader.readLine()) != null) {
  8.             //System.out.println(content);
  9.             Behaviour behaviour = gson.fromJson(content, Behaviour.class);
  10.             //System.out.println(behaviour.getApp());
  11.         }
  12.         reader.close();
  13.         System.out.println((System.currentTimeMillis() - start) + "ms");
  14.     }
        gosn在处理一千万的的数据耗时565.891秒;

        2.然后是jackson,

        

点击(此处)折叠或打开

  1.     //10000000,57,963ms
  2.     @Test
  3.     public void testReadJsonToObject() throws JsonSyntaxException, IOException {
  4.         long start = System.currentTimeMillis();
  5.         String content = "";
  6.         while ((content = reader.readLine()) != null) {
  7.             //System.out.println(content);
  8.             Behaviour behaviour = objectMapper.readValue(content, Behaviour.class);
  9.             //System.out.println(behaviour.getApp());
  10.         }
  11.         System.out.println((System.currentTimeMillis() - start) + "ms");
  12.     }

        jackson处理花了57.963秒,居然是gson的十倍,喔!


        3.接下来是json-lib
        这里面json-lib是比较老牌的解析技术了,使用起来也很方便,在使用上来说三者都很方便。

        

点击(此处)折叠或打开

  1.     //10000000,1498,733ms
  2.     @Test
  3.     public void testComplex() throws IOException {
  4.         long start = System.currentTimeMillis();
  5.         String content = "";
  6.         JsonConfig config = new JsonConfig();
  7.         config.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {
  8.           @Override
  9.           public String transformToJavaIdentifier(String str) {
  10.             char[] chars = str.toCharArray();
  11.             chars[0] = Character.toLowerCase(chars[0]);
  12.             return new String(chars);
  13.           }
  14.         });
  15.         config.setRootClass(Behaviour.class);
  16.         
  17.         while ((content = reader.readLine()) != null) {
  18.             //System.out.println(content);
  19.             Behaviour behaviour = (Behaviour)JSONObject.toBean(JSONObject.fromObject(content,config), Behaviour.class);
  20.             //System.out.println(behaviour.getEvents().size());
  21.         }
  22.         System.out.println((System.currentTimeMillis() - start) + "ms");
  23.     }

        结果出来了,json-lib耗时1498.733秒,jackson在大数据的表现相当突出。

阅读(1087) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~