你好,游客 登录 注册 搜索
背景:
阅读新闻

如何在Hadoop的MapReduce程序中处理JSON文件

[日期:2014-01-25] 来源:Linux社区  作者:hanhuili [字体:大 中 小]

简介:

尽量在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑。但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包。这里选择json-simple实现我们的功能。

在Hadoop上执行Java程序的命令如下所示:

[hadoop@localhost]$ hadoop jar my-mapreduce.jar

my-mapreduce.jar是我们进行日志处理的MapReduce程序。现在假定我们需要在其中处理JSON格式的配置文件,这里忽略如何在Hadoop集群读取文件的细节,只关注如何使用JSON工具包。下面是简单的HelloWorld程序:

import org.json.simple.JSONObject;
public class HelloWorld{
    public static void main(String[] args){
        JSONObject obj=new JSONObject();
        obj.put("name","foo");
        obj.put("num",new Integer(100));
        obj.put("balance",new Double(1000.21));
        obj.put("is_vip",new Boolean(true));
        obj.put("nickname",null);
        System.out.print(obj);
    }
}

在HelloWorld程序中,只简单修改JSON对象,将其内容打印输出,从而验证解析修改JSON内容的过程。

编译:

由于MapReduce程序需提交到Hadoop集群执行,所以HelloWorld依赖的json-simple包必须存在于集群的classpath路径中,如果集群上没有对应的jar包。执行HelloWorld会出现如下异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject

简单的解决方法是将json-simple包直接和HelloWorld编译结果一起打包,然后即可使用命令hadoop jar HelloWorld.jar执行。需将json-simple的jar包解压再同HelloWorld打包。

编译命令如下所示:

[hadoop@localhost]$ jar tf json-simple-1.1.1.jar
META-INF/MANIFEST.MF
META-INF/
META-INF/maven/
META-INF/maven/com.googlecode.json-simple/
META-INF/maven/com.googlecode.json-simple/json-simple/
META-INF/maven/com.googlecode.json-simple/json-simple/pom.properties
META-INF/maven/com.googlecode.json-simple/json-simple/pom.xml
org/
org/json/
org/json/simple/
org/json/simple/ItemList.class
org/json/simple/JSONArray.class
org/json/simple/JSONAware.class
org/json/simple/JSONObject.class
org/json/simple/JSONStreamAware.class
org/json/simple/JSONValue.class
org/json/simple/parser/
org/json/simple/parser/ContainerFactory.class
org/json/simple/parser/ContentHandler.class
org/json/simple/parser/JSONParser.class
org/json/simple/parser/ParseException.class
org/json/simple/parser/Yylex.class
org/json/simple/parser/Yytoken.class

[hadoop@localhost]$ unzip json-simple-1.1.1.jar
[hadoop@localhost]$ javac -classpath ./json-simple-1.1.1.jar HelloWorld.java
[hadoop@localhost]$ jar -cfe HelloWorld.jar HelloWorld  HelloWorld.class ./org/

执行HelloWorld

[hadoop@localhost]$ hadoop jar HelloWorld.jar
{"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}

相关阅读:

Hadoop之MapReduce框架心跳机制分析 http://www.linuxidc.com/Linux/2014-01/95723.htm

Hadoop 新 MapReduce 框架 Yarn 详解 http://www.linuxidc.com/Linux/2013-09/90090.htm

Hadoop中HDFS和MapReduce节点基本简介 http://www.linuxidc.com/Linux/2013-09/89653.htm

MapReduce的自制Writable分组输出及组内排序 http://www.linuxidc.com/Linux/2013-09/89652.htm

MapReduce的一对多连接操作 http://www.linuxidc.com/Linux/2013-09/89647.htm

Hadoop--两个简单的MapReduce程序 http://www.linuxidc.com/Linux/2013-08/88631.htm

Hadoop 中利用 MapReduce 读写 MySQL 数据 http://www.linuxidc.com/Linux/2013-07/88117.htm

更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

linux
相关资讯       MapReduce程序  Hadoop程序 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款