Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException
简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录。
环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Hadoop 1.2.1
改好的代码如下,在reduce阶段遇到了NullPointerException。
public class STjoinEx {
private static final String TIMES = "TIMES"; public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
configuration.setInt(TIMES, 1);
String[] remainingArgs = new GenericOptionsParser(configuration, args).getRemainingArgs();
if (remainingArgs.length != 2) {
System.err.println("STjoinEx <input> <output>");
System.exit(2);
} Job job = new Job(configuration, STjoinEx.class.getSimpleName());
job.setJarByClass(STjoinEx.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(KeyValueTextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path(remainingArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(remainingArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } public static class Map extends Mapper<Text, Text, Text, Text> {
final static Text LEFT_TABLE = new Text();
final static Text RIGHT_TABLE = new Text(); @Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
// left table
LEFT_TABLE.set("1 " + value);
context.write(key, LEFT_TABLE);
// right table
RIGHT_TABLE.set("2 " + key);
context.write(value, RIGHT_TABLE);
}
} public static class Reduce extends Reducer<Text, Text, Text, Text> {
private static final int INDENT = 2;
private static final Text GRAND_PARENT = new Text();
private static final Text GRAND_CHILD = new Text(); @Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
// output header
int times = context.getConfiguration().getInt(TIMES, 1);
if (times == 1) {
context.write(new Text("grandChild"), new Text("grandParent"));
context.getConfiguration().setInt(TIMES, ++times);
} // prepare matrix
int headChar = 0;
String[] grandChild = new String[10];
String[] grandParent = new String[10];
int grandChildNum = 0;
int grandParentNum = 0; for (Text value : values) {
headChar = value.charAt(0);
if (headChar == '1') {
grandParent[grandParentNum] = value.toString().substring(2);
grandParentNum++;
} else {
grandChild[grandChildNum] = value.toString().substring(2);
grandChildNum++;
}
} // multiply
if (grandChildNum != 0 && grandChildNum != 0) {
for (int i = 0; i < grandChildNum; i++) {
GRAND_CHILD.set(grandChild[i]);
for (int j = 0; j < grandParentNum; j++) {
GRAND_PARENT.set(grandParent[j]);
context.write(GRAND_CHILD, GRAND_PARENT);
}
}
}
}
}
}
执行输出为
14/10/07 11:12:51 INFO mapred.JobClient: map 0% reduce 0%
14/10/07 11:12:54 INFO mapred.JobClient: map 100% reduce 0%
14/10/07 11:13:01 INFO mapred.JobClient: map 100% reduce 33%
14/10/07 11:13:04 INFO mapred.JobClient: Task Id : attempt_201410021756_0048_r_000000_0, Status : FAILED
java.lang.NullPointerException
at org.apache.hadoop.io.Text.encode(Text.java:388)
at org.apache.hadoop.io.Text.set(Text.java:178)
at main.ch5.STjoinEx$Reduce.reduce(STjoinEx.java:96)
at main.ch5.STjoinEx$Reduce.reduce(STjoinEx.java:61)
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:177)
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:649)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:418)
at org.apache.hadoop.mapred.Child$4.run(Child.java:255)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1190)
at org.apache.hadoop.mapred.Child.main(Child.java:249)
从输出信息可发现,源码96行if (grandChildNum != 0 && grandChildNum != 0)为出错行。两个判断条件重复了,将其中一个改成grandParentNum即可。
执行结果
grandChild grandParent
Jone Alice
Jone Jesse
Tom Alice
Tom Jesse
Tom Mary
Tom Ben
Jone Mary
Jone Ben
Philip Alice
Philip Jesse
Mark Alice
Mark Jesse
Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException的更多相关文章
- Hadoop on Mac with IntelliJ IDEA - 7 解决failed to report status for 600 seconds. Killing!问题
本文讲述作业在Hadoop 1.2.1完成map后ruduce阶段遇到failed to report status for 600 seconds. Killing!问题的解决过程. 环境:Mac ...
- Hadoop 单表关联
前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础.单表关联这个实例要求从给出的数据中寻找到所关心的数据,它是对原始数据所包含信息的挖掘.下面进入这个实例. 1.实例描述 实例中给出chi ...
- MapReduce应用案例--单表关联
1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. ...
- MapRedece(单表关联)
源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...
- MR案例:单表关联查询
"单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘. 需求:实例中给出 child-parent(孩子—父母)表,要求输出 grandchild ...
- Hadoop on Mac with IntelliJ IDEA - 1 解决input path does not exist问题
本文讲述使用IntelliJ IDEA时遇到Hadoop提示input path does not exist(输入路径不存在)的解决过程. 环境:Mac OS X 10.9.5, IntelliJ ...
- Hadoop工程师面试题(1)--MapReduce实现单表汇总统计
数据源格式描述: 输入t1.txt源数据,数据文件分隔符"*&*",字段说明如下: 字段序号 字段英文名称 字段中文名称 字段类型 字段长度 1 TIME_ID 时间(到时 ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parentTom LucyTom JackJone LucyJone JackLucy MaryLucy Ben ...
- Hadoop on Mac with IntelliJ IDEA - 9 解决Type mismatch in value from map问题
修改陆喜恒. Hadoop实战(第2版)5.3排序的代码时遇到IO异常. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Hadoop 1.2.1 异常具体信息如下 ...
随机推荐
- 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API
Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...
- Linux下常用软件
一, vmtool安装, 进入桌面就后,如果没有看到VMware Tools光盘, 请点击VMware Station菜单栏上的“虚拟机”,然后选择“安装VMware Tools”,就可以在桌面上以看 ...
- Brush、Color、String相互转换
using System.Windows.Media; 1.String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...
- [偏微分方程教程习题参考解答]4.1Duhamel 原理
1. 如果已知下述常微分方程的特定初值问题 $$\bex \sedd{\ba{ll} -y''+y=0,&x>0,\\ y(0)=0,\quad y'(0)=1 \ea} \eex$$ ...
- Android 自定义dialogfragment
在用dialogfragment的时候我们可能会不喜欢系统自带的黑色边框,那怎么办呢? dialofragment提供可供修改样式的方法setStyle(style,R.style.MyTryUseD ...
- Android 最火框架XUtils之注解机制详解
http://blog.csdn.net/rain_butterfly/article/details/37931031
- cefSharp在XP下使得程序崩溃记录
前言:这是一个奇葩的问题,到现在自己还没有搞明白问题出现在哪里,但是从问题总算是解决了,希望看到此文章的大牛,如果知道问题出在什么地方,可以告知一下. [一个在XP系统下面应用程序崩溃问题] 资源: ...
- STL1-unordered_map
最近几天我要整理一下遇到的STL的函数,本来其实我是没有打算学的,认为用C就完全可以实现,干嘛要记那么多复杂的函数呢,所以我之前的做法都是将常用的C函数自己做了一个lib库,使用起来也是蛮方便的呢,但 ...
- Spring依赖注入 --- 模拟实现
Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...
- 约瑟夫环 --- 面向对象 --- java代码
约瑟夫环 的 面向对象 解法 罗马人占领乔塔帕特后,39个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个 ...