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 异常具体信息如下 ...
随机推荐
- mysql的text类型长度问题
在我的概念中,mysql中的text字段应该是没有长度限制的,但是今天事实告诉我,text类型的长度是有限制的.其中mysql的text类型有64K长度限制的,MEDIUMTEXT中型是2G,LONG ...
- input框内默认文字点击消失
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Excel 绘制图表,如何显示横轴的数据范围
右键点击X坐标轴,然后选中“设置图表区域格式”,然后在“坐标轴选项”--“区域”处设置X轴范围. 备注,这种方式仅使用与第一列时日期时间类型的数据. 应用场景 当,选择有两列数据,第一列为横轴数据,第 ...
- Php 笔记2-----手机端 与 php服务器的通信
对于 手机端 和 php服务器的通信,是不存在表单这一概念的 ,除非自己去实现, 所以通常情况下步骤是: 假定上传的是字符串. 1 手机端的流程是 把文件或者字符串,转化为 特定的流. 2 通过h ...
- 《Python 学习手册4th》 第十九章 函数的高级话题
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...
- VM虚拟机快照还原效果实现方式
===================================================默认"快照删除"行为:============================ ...
- STL-next permutation
过程: 从右往左,找到第一个A[i] < A[i+1]: 从右往左,找到第一个A[j] > A[i], j > i: 交换A[i] 与 A[j]: 将A[i + 1]之后的元素逆序( ...
- 用代码将Excel数据导入Sql Server
这里直接用小例子说明. 1.打开VS2010—>文件—>新建—>网站,选择ASP.NET空网站并设置存放路径以创建空网站.(我这里路径设置为D:\excelEduceToSql) 2 ...
- mybatis系列-15-查询缓存
15.1 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时需要 ...
- LeetCode(6) - ZigZag Conversion
这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigza ...