• 问题描述

需要连接的表如下:其中左边是child,右边是parent,我们要做的是找出grandchild和grandparent的对应关系,为此需要进行表的连接。


Tom Lucy
Tom Jim
Lucy David
Lucy Lili
Jim Lilei
Jim SuSan
Lily Green
Lily Bians
Green Well
Green MillShell
Havid James
James LiT
Richard Cheng
Cheng LiHua
 
  • 思路分析
  诚然,在写MR程序的时候要结合MR数据处理的一些特性。例如如果我们用默认的TextInputFormat来处理传入的文件数据,传入的格式是key为行号,value为这一行的值(如上例中的第一行,key为0,value为[Tom,Lucy]),在shuffle过程中,我们的值如果有相同的key,会merge到一起(这一点很重要!)。我们利用shuffle阶段的特性,merge到一组的数据够成一组关系,然后我们在这组关系中想办法区分晚辈和长辈,最后对merge里的value一一作处理,分离出grandchild和grandparent的关系。
       例如,Tom Lucy传入处理后我们将其反转,成为Lucy Tom输出。当然,输出的时候,为了达到join的效果,我们要输出两份,因为join要两个表,一个表为L1:child parent,一个表为L2:child parent,为了达到关联的目的和利用shuffle阶段的特性,我们需要将L1反转,把parent放在前面,这样L1表中的parent和L2表中的child如果字段是相同的那么在shuffle阶段就能merge到一起。还有,为了区分merge到一起后如何区分child和parent,我们把L1表中反转后的child(未来的 grandchild)字段后面加一个1,L2表中parent(未来的grandparent)字段后加2。
 package com.test.join;

 import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator; 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; public class STJoin { public static class STJoinMapper extends Mapper<Object, Text, Text, Text>{ @Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
String[] rela = value.toString().trim().split(" ",2);
if(rela.length!=2)
return;
String child = rela[0];
String parent = rela[1];
context.write(new Text(parent), new Text((child+"1")));
context.write(new Text(child), new Text((parent+"2"))); } }
public static class STJoinReducer extends Reducer<Text, Text, Text, Text>{ @Override
protected void reduce(Text arg0, Iterable<Text> arg1,Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
ArrayList<String> grandParent = new ArrayList<>();
ArrayList<String> grandChild = new ArrayList<>();
Iterator<Text> iterator = arg1.iterator();
while(iterator.hasNext()){
String text = iterator.next().toString();
if(text.endsWith("1"))
grandChild.add(text.substring(0, text.length()-1));
if(text.endsWith("2"))
grandParent.add(text.substring(0, text.length()-1));
} for(String grandparent:grandParent){
for(String grandchild:grandChild){
context.write(new Text(grandchild), new Text(grandparent));
}
}
}
} public static void main(String args[]) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = new Job(conf,"STJoin");
job.setMapperClass(STJoinMapper.class);
job.setReducerClass(STJoinReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/user/hadoop/STJoin/joinFile"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/user/hadoop/STJoin/joinResult")); System.exit(job.waitForCompletion(true)?0:1);
}
}
  • 结果显示

 

Richard    LiHua
Lily Well
Lily MillShell
Havid LiT
Tom Lilei
Tom SuSan
Tom Lili
Tom David

以上代码在hadoop1.0.3平台实现

【原创】MapReduce编程系列之表连接的更多相关文章

  1. Hadoop阅读笔记(三)——深入MapReduce排序和单表连接

    继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...

  2. 【SqlServer系列】表连接

    1   概述 1.1  已发布[SqlServer系列]文章 [SqlServer系列]MYSQL安装教程 [SqlServer系列]数据库三大范式 [SqlServer系列]表单查询 1.2  本篇 ...

  3. MapReduce编程系列 — 5:单表关联

    1.项目名称: 2.项目数据: chile    parentTom    LucyTom    JackJone    LucyJone    JackLucy    MaryLucy    Ben ...

  4. 【原创】MapReduce编程系列之二元排序

    普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...

  5. MapReduce编程系列 — 6:多表关联

    1.项目名称: 2.程序代码: 版本一(详细版): package com.mtjoin; import java.io.IOException; import java.util.Iterator; ...

  6. MapReduce编程系列 — 4:排序

    1.项目名称: 2.程序代码: package com.sort; import java.io.IOException; import org.apache.hadoop.conf.Configur ...

  7. MapReduce编程系列 — 3:数据去重

    1.项目名称: 2.程序代码: package com.dedup; import java.io.IOException; import org.apache.hadoop.conf.Configu ...

  8. MapReduce编程系列 — 2:计算平均分

    1.项目名称: 2.程序代码: package com.averagescorecount; import java.io.IOException; import java.util.Iterator ...

  9. MapReduce编程系列 — 1:计算单词

    1.代码: package com.mrdemo; import java.io.IOException; import java.util.StringTokenizer; import org.a ...

随机推荐

  1. 用shell查找某个目录下最大文件

    网上资料学习: 1.查找当前目录下最大文件(包括子目录里文件): find . -type f -exec stat -c "%s %n" {} \; | sort -nr | h ...

  2. asp.net mvc 事务处理:Transactions

    1.在控制器里引用using System.Transactions; 2.在你需要事务回滚的地方外面套一层using (TransactionScope sc = new TransactionSc ...

  3. android:Faild to install,你的主机中的软件终止了一个连接错误解决

    当在用真机调试android程序时出现Faild to install,你的主机中的软件终止了一个连接错误时可以这样解决: 在手机开启usb调试和安装未知来源软件的情况下: 1:先查进入任务管理器查看 ...

  4. windows store app Lifecycle

    1.Activated 2.Suspended 3.Resumed 4.Terminated 对应的 js代码: (function () { "use strict"; WinJ ...

  5. 史上最全github使用方法:github入门到精通--备用

    [初识Github] 首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理 ...

  6. BZOJ 3713: [PA2014]Iloczyn

    Description 斐波那契数列的定义为:k=0或1时,F[k]=k:k>1时,F[k]=F[k-1]+F[k-2].数列的开头几项为0,1,1,2,3,5,8,13,21,34,55,-你 ...

  7. Codeforces Round #208 (Div. 2)

    A - Dima and Continuous Line 水题:直接模拟: #include<cstdio> #define maxn 1005 using namespace std; ...

  8. Android之监测手机网络状态的广播

    Android之监测手机网络状态的广播 Android 监控网络状态 Android利用广播监听设备网络连接(断网)的变化情况

  9. 几种 Docker 监控工具对比

    轻量级虚拟化容器 Docker,自发布以来便广受业界关注,在开源界和企业界掀起了一阵风.Docker 容器相对于 VM 有以下几个优势:启动速度快:资源利用率高:性能开销小. 从图中可以看出 Dock ...

  10. session跨域共享解决方案

    要让session跨域共享,需要解决三个问题: 1.通过什么方法来传递session_id? 2.通过什么方法来保存session信息? 3.通过什么方法来进行跨域? 一.传递session_id有4 ...