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基础之第18章 性能优化
18.1.优化简介 SHOW STATUS LIKE ‘value’;connections 连接数uptime 启动 ...
- bootstrap-datetimepicker在经过GC(Google Closure Compiler)压缩后无法使用的解决方案
将压缩级别由simple改成whitespace 问题就是这样之后压缩后的文件大了很多 <?xml version="1.0"?> <project name=& ...
- RIA+REST架构实现完美WEB开发
记得第一次看到REST的身影,是在InfoQ上的一篇介绍,随后又翻阅了后面的参考文章和Developerwork上一些资料,甚至随手翻了翻Roy博士的论文.所幸,在不少人还在体会REST到底是何方神圣 ...
- [Everyday Mathematics]20150226
设 $z\in\bbC$ 适合 $|z+1|>2$. 试证: $$\bex |z^3+1|>1. \eex$$
- saltstack配置安装的一些关键步骤及安装时各种报错的分析
以下其他仅做参考,官方网址才是安装重点:http://docs.saltstack.cn/topics/installation/rhel.html 与安装相关的一些文档或资料: 一.linux服务器 ...
- JavaScript中的Function(函数)对象
1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...
- MapReduce 中job.setJarByClass()方法的疑惑
在调试mr实例的时候,遇到如下的情况,如图所示 说明:就是我的mr程序类名称和我设置的setJarByclass()中设置的不一样,但是程序竟然没有报错!!!!当时把我吓尿了 疑惑:如果这样设置的话, ...
- Package inputenc Error: Unicode char \u8: not set up for use with LaTeX.
用TexStudio编辑文档时,不知是多加了空格还是啥,总是提示如下错误: Package inputenc Error: Unicode char \u8: not set up for use w ...
- SQL和NOSQL有区别吗?
在大数据高速发展的今天,数据量在不断的增加,传统的数据库可能不能满足人们的需求了,这个时候新霸哥注意到了NOSQL出现了可以解决这个问题.我们知道sql数据库可以存储数据和处理数据,但是NOSQL最大 ...
- HDU-2888 Check Corners 二维RMQ
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题.解题思路如下(转载别人写的): dp[row][col][i][j] 表示[row,ro ...