1. 单表关联 实例描述
实例中给出child-parent (孩子——父母)表,要求输出grandchild-grandparent (孙子——爷奶)表。
样例输入 如下所示。
file:
child parent
Tom Lucy
Tom Jack
Jone Lucy
Jone Jack
Lucy Mary
Lucy Ben
Jack Alice
Jack Jesse
Terry Alice
Terry Jesse
Philip Terry
Philip Alma
Mark Terry
Mark Alma
样例输出如下所示。
file:
grandchild grandparent
Tom Alice
Tom Jesse
Jone Alice
Jone Jesse
Tom Mary
Tom Ben
Jone Mary
Jone Ben
Philip Alice
Philip Jesse
Mark Alice
Mark Jesse
设计思路
分析这个实例,显然需要进行单表连接,连接的是左表 的parent 列和右表 的child 列,且左表 和右表 是同一个表 。
连接结果 中除去 连接的两列就是所需要的结果——"grandchild--grandparent"表。要用MapReduce解决这个实例,首先 应该考虑如何实现表 的自连接 ;其次 就是连接列 的设置 ;最后 是结果 的整理 。
考虑到MapReduce的shuffle过程会将相同的key会连接在一起,所以可以将map结果的key 设置成待连接 的列 ,然后列中相同的值就自然会连接在一起了。再与最开始的分析联系起来:
要连接的是左表的parent列和右表的child列,且左表和右表是同一个表,所以在map阶段 将读入数据分割 成child 和parent 之后,会将parent 设置成key ,child 设置成value 进行输出,并作为左表 ; 再将同一对child 和parent 中的child 设置成key ,parent 设置成value 进行输出,作为右表 。为了区分 输出中的左右表 ,需要在输出的value 中再 加上左右表 的信息 ,比如在value的String最开始处加上字符1 表示左表 ,加上字符2 表示右表 。这样在map的结果中就形成了左表和右表,然后在shuffle过程中完成连接。reduce接收到连接的结果,其中每个key的value-list就包含了"grandchild--grandparent"关系。取出每个key的value-list进行解析,将左表 中的child 放入一个数组 ,右表 中的parent 放入一个数组 ,然后对两个数组求笛卡尔积 就是最后的结果了
程序代码
程序代码如下所示。
packagecom.hebut.mr;
importjava.io.IOException;
importjava.util.*;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.Path;
importorg.apache.hadoop.io.IntWritable ;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Job;
importorg.apache.hadoop.mapreduce.Mapper;
importorg.apache.hadoop.mapreduce.Reducer;
importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
importorg.apache.hadoop.util.GenericOptionsParser;
publicclassSTjoin{
publicstaticinttime=0;
/*
* map 将输出分割 child 和 parent ,然后正序输出一次作为右表,
* 反序输出一次作为左表,需要注意的是在输出的 value 中必须
* 加上左右表的区别标识。
*/
publicstaticclassMapextendsMapper<Object,Text, Text, Text> {
// 实现 map 函数
publicvoidmap(Objectkey, Text value, Context context)
throwsIOException,InterruptedException {
Stringline=value.toString();
String[] strs=line.split("\t");
context.write(new Text(strs[1]),newText("1+"+strs[0]));//输出左表
context.write(new Text(strs[0]),new Text("2+"+strs[1]));//输出右表
}
}
}
publicstaticclassReduceextendsReducer<Text,Text, Text, Text> {
// 实现 reduce 函数
publicvoidreduce(Textkey, Iterable<Text> values, Context context)
throwsIOException,InterruptedException {
// 输出表头
if(0==time){
context.write(newText("grandchild"),newText("grandparent"));
time++;
}
String[] grandchild = null;
int grandchildnum = 0;
String[] grandparent =null;
int grandparentnum = 0;
Iterator iter = values.iterator();
while (iter.hasNext()) {
String record = ite.next().toString();
String[] st=record.split("+");
if(st[0]==1){
grandchild[grandchildnum ] =st[1];
grandchildnum ++;
} else if(st[0]==2){
grandparent [grandparentnum ]=st[1];
grandparentnum ++;
}
}
//grandchild 和 grandparent 数组求笛卡尔儿积
if(0!= grandchildnum && 0 != grandparentnum) {
for(intm= 0; m < grandchildnum; m++) {
for(intn= 0; n < grandparentnum; n++) {
// 输出结果
context.write(newText(grandchild[m]),newText(grandparent[n]));
}
}
}
}
}
publicstaticvoidmain(String[]args)throwsException{
Configuration conf =newConfiguration();
// 这句话很关键
conf.set("mapred.job.tracker","192.168.1.2:9001");
String[] ioArgs =newString[]{"STjoin_in","STjoin_out"};
String[] otherArgs =newGenericOptionsParser(conf,ioArgs).getRemainingArgs();
if(otherArgs.length!=2) {
System.err.println("Usage:Single Table Join <in> <out>");
System.exit(2);
}
Job job =newJob(conf,"SingleTable Join");
job.setJarByClass(STjoin.class);
// 设置 Map 和 Reduce 处理类
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
// 设置输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// 设置输入和输出目录
FileInputFormat.addInputPath(job,newPath(otherArgs[0]));
FileOutputFormat.setOutputPath(job,newPath(otherArgs[1]));
System.exit(job.waitForCompletion(true)? 0 : 1);
}
}
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com