Mapreduce《案例之内连接》
数据源:
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
输出结果为:
grandChildgrandParent
TomAlice
TomJesse
JoneAlice
JoneJesse
TomMary
TomBen
JoneMary
JoneBen
PhilipAlice
PhilipJesse
MarkAlice
MarkJesse
===================================JAVA CODE ======================
package gq;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
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;
/**
*
* Class Description:自连接,爷找孙子测试类
*
* Author:gaoqi
*
* Date:2015年6月5日 下午2:03:08
*
*/
public class OwnerJoin {
public static int TIME = 0;
public static class Map extends Mapper<Object, Text, Text, Text>{
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
String line = value.toString();
String childname = "";
String parentname="";
String flag = "";
StringTokenizer stk = new StringTokenizer(line);
String[] _values = new String[2];
int i= 0;
while(stk.hasMoreElements()){
_values[i]=stk.nextToken();
i++;
}
if(_values[0].compareTo("child") !=0){
childname = _values[0];
parentname = _values[1];
flag = "1";//left table
context.write(new Text(parentname), new Text(flag+"-"+childname+"-"+parentname));
flag="2";//right table
context.write(new Text(childname), new Text(flag+"-"+childname+"-"+parentname));
}
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
String[] pArray = new String[10];
String[] cArray = new String[10];
int pnum = 0;
int cnum = 0;
Iterator<Text> vals = values.iterator();
while(vals.hasNext()){
String recod = vals.next().toString();
String[] ss = recod.split("-");
String flag = ss[0];
String childname = ss[1];
String parentname = ss[2];
System.out.println(flag+"-"+childname+"--"+parentname);
if(flag.equals("1")){
cArray[cnum] = childname;
cnum++;
}elseif(flag.equals("2")){
pArray[pnum] = parentname;
pnum++;
}
}
if(TIME ==0){
context.write(new Text("grandChild"), new Text("grandParent"));
TIME++;
}
if(pnum != 0 && cnum !=0 ){
for(int j=0;j<cnum;j++){
for(int k=0;k<pnum;k++){
context.write(new Text(cArray[j]), new Text(pArray[k]));
}
}
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf,"OwnerJoin");
job.setJarByClass(OwnerJoin.class);
job.setMapperClass(Map.class);
//job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/inputOwnerJoin"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/outputOwnerJoin"));
System.exit(job.waitForCompletion(true)?0:1);
}
}
相关推荐
了解map和reduce工作原理,以及排序,分组,分区设置,有详细的注释,方便查看学习,适合入门初学者练手
1.社交网络综合评分案例 2.微博精准营销案例 3.物品推荐案例 4.QQ好友推荐案例
简单的在MapReduce中实现两个表的join连接简单的在MapReduce中实现两个表的join连接简单的在MapReduce中实现两个表的join连接
2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx
mapreduce案例测试数据
mapreduce案例测试数据
大数据mapreduce案例介绍,包括代码解释,详解MRS工作流程
mapreduce的Wordcount案例将main生成jar可直接在hdfs上运行
MapReduce求行平均值--标准差--迭代器处理--MapReduce案例
mapreduce案例数据:dept.csv,emp.csv,sales.csv
mapreduce案例测试数据
MapReduce求取行平均值 MapReduce小实例 数据有经过处理已经添加行号的 也有未添加的 行平均值的四种求法
mapreduce基本数据读取,通俗易懂。 此项目情景为,遗传关系族谱。 (爷爷 、父母 、孩子) 经典案例
整理了MapRduce的几个入门案例,提供源数据 案例分为: 1、统计单词的个数 2、设置分区 3、统计json数据 4、统计并排序 5、mapReducer找朋友的使用
前四节提供了几个小案例 下面详细介绍MapReduce中Map任务Reduce任务以及MapReduce的执行流程。 Map任务: 读取输入文件内容,解析成key,value对。对输入文件的每一行,解析成key,value对。每一个键值对调用一次map...
细细品味Hadoop_Hadoop集群(第9期)_MapReduce初级案例
https://blog.csdn.net/qq_39063526/article/details/105968494 本文案例中用到的文件
MapReduce字数统计案例,希望大家交流,最好自己写完后在对比交流,欢迎前来交流
08.mapreduce编程案例--流量统计求和--自定义数据类型.mp4
MapReduce之自定义 OutPutFormat,通过一个案例,实现自定义的一个OutPutFormat,来更加的深刻的理解MR的过程