一. 事件起因
我在学习Hadoop相关知识时做了一道极为简单的练习题,然而在解决问题的过程中,却引发了一个对我而言,极为困难,一个礼拜才勉强解决的惊天大BUG。(小弟才疏学浅,各位的独到见解和批评讨论都可以留言,虚心接受)
二.练习原题以及数据准备
问题题目
(两表 inner join)
在 hdfs 目录/tmp/table/student 中存在 stu.txt 文件,按 tab分隔,字段名为(学号,姓名,课程号,班级名称),
hdfs 目录/tmp/table/student_location 中存在 stu_location.txt 文件,按 tab 分隔,字段名为(学号,省份,城市,区名),
对两个 hdfs目录的按学号求交集,输出结果结构按 tab 分隔后的四个字段为(学号,姓名,课程号,班级名称)
文件内容
stu.txt 文件内容:
stu_location.txt 文件内容:
三.初次实现代码
(略长可选择式查看,或只看实现思路)
实现思路:
(1)在map阶段读取参数文件,即stu.txt和stu_location.txt,如果这个文件的记录切割完有4个元素,说明他是学生信息,有5个元素说明他是地址信息,将两个信息都按照学号分组向reduce输出。
(2)在reduce阶段按学号分组,接收分组信息,并存入"inflist"集合中,如果inflist中的这个元素的size==2,说明他既有学生信息,也有地址信息,那么就是需要的元素,拼接成字符串输出即可。
public class DIY15 {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
Job job=Job.getInstance(conf);
job.setJarByClass(DIY15.class);
job.setMapperClass(JoinMapper.class);
job.setReducerClass(JoinReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(SinfoWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileInputFormat.addInputPath(job, new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2]));
System.exit(job.waitForCompletion(true)?0:1);
}
//Mapper
public static class JoinMapper extends Mapper<LongWritable, Text, IntWritable, SinfoWritable>{
SinfoWritable sin=new SinfoWritable();
@Override
protected void map(
LongWritable key,Text value,Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
String[] fileds=value.toString().split("\\t");
if(fileds!=null&&fileds.length==4){
sin.set(Integer.parseInt(fileds[0]),fileds[1], Integer.parseInt(fileds[2]), fileds[3],
"", "", "", "");
}else if (fileds!=null&&fileds.length==5) {
sin.set(Integer.parseInt(fileds[0]), "", 0, "",
fileds[1], fileds[2],fileds[3],fileds[4]);
}else {
return;
}
context.write(new IntWritable(Integer.parseInt