单表连接
单表关联这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘。典型的为找祖孙关系
本帖为Reduce端Join来实现单表连接
思想
在map端将来源于不同的数据或者是有不同用处的数据打上标签,以便在reduce端接收并连接查找关系。
场景
无论大表小表(无论文件的大小)
优点
解决的业务范围广
缺点
map端到reduce的传输量比较大(且大量为无效数据),经历shuffle更加耗时,reduce端的join效率低下
举例
cp.txt
child parrent
张三 王阿姨
张三 张大叔
张胖子 王阿姨
张胖子 张大叔
王阿姨 王奶奶
王阿姨 王大爷
张大叔 张奶奶
张大叔 张大爷
要求找出 对应的孙子辈和爷爷辈
样例输出
过程分析:
从sql语句的角度考虑:
select cp1.child,cp2.parrent from cp cp1 join cp cp2 on cp1.parrent = cp2.child;
所以需要两个表,即两个key、value顺序不一样的键值对
Map端:
表1:child作key,parrent作value
<张三,1:王阿姨>
<张三,1:张大叔>
<张胖子,1:王阿姨>
<张胖子,1:张大叔>
<王阿姨,1:王奶奶>
<王阿姨,1:王大爷>
<张大叔,1:张奶奶>
<张大叔,1:张大爷>
表2:parrent作key,child作value
<王阿姨,2:张三>
<张大叔,2:张三>
<王阿姨,2:张胖子>
<张大叔,2:张胖子>
<王奶奶,2:王阿姨>
<王大爷,2:王阿姨>
<张奶奶,2:张大叔>
<张大爷,2:张大叔>
Shuffle:
将所有的键值对,shuffle聚集后的结果为
<张三,<1:王阿姨,1:张大叔>>
<张胖子,<1:王阿姨,1:张大叔>>
<王阿姨,<1:王奶奶,1:王大爷,2:张三,2:张胖子>>
<张大叔,<1:张奶奶,1:张大爷,2:张三,2:张胖子>>
<王奶奶,<2:王阿姨>>
<王大爷,<2:王阿姨>>
<张奶奶,<2:张大叔>>
<张大爷,<2:张大叔>>
非常明显的可以看出,在表中,既有孩子又有父母的parrent(即表中的王阿姨和张大叔),在聚集完的结果中,归并项中既有1:开头的爷爷辈,也有2:开头的孙子辈。而其他归并项,要么只有1:开头的爷爷辈,要么只有2:开头的爷爷辈。
所以,当values中,既有爷爷辈,也有孙子辈,这个parrent就起到了纽带的作用,将它的孩子和它的父母找了出来。
代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReduceSileJoin {
public static class MyMapper extends Mapper<Object, Text, Text, Text>{
public Text k = new Text();
public Text v = new Text();
@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String row = value.toString();
String child = row.split("\t")[0];
String parrent = row.split("\t")[1];
//添加左表
k.set(child);
v.set("1:"+parrent);
context.write(k,v);
//添加右表
k.set(parrent);
v.set("2:"+child);
context.write(k,v);
}
}
public static class MyReducer extends Reducer<Text, Text,Text, Text>{
public Text k = new Text();
public Text v = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
//定义一个容器存储孙子辈
List<String> grandChildList = new ArrayList<>();
//定义一个容器存储爷爷辈
List<String> grandParentList = new ArrayList<>();
for (Text value : values) {
String[] cps = value.toString().split(":");
String name = cps[1];
if(cps[0].equals("1")){
grandParentList.add(name);
}else if(cps[0].equals("2")){
grandChildList.add(name);
}
}
//values里既有爷爷辈也是孙子辈
//笛卡尔积输出
if(grandChildList.size()>0 && grandParentList.size()>0){
for (String gc : grandChildList) {
for (String gp : grandParentList) {
k.set(gc);
v.set(gp);
context.write(k,v);
}
}
}
}
}
public static void main(String[] args) throws IOException {
try {
Job job = Job.getInstance(new Configuration(),"myReduceSideJoin");
job.setJarByClass(MapSideJoin.class);
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job,new Path("/input/reducejoin/cp.txt"));
FileOutputFormat.setOutputPath(job,new Path("/output/mr/reducejoin/test1/"));
int success = job.waitForCompletion(true) ? 0 : 1;
System.exit(success);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}