【原创】MapReduce编程系列之表连接
- 问题描述
需要连接的表如下:其中左边是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
- 思路分析
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编程系列之表连接的更多相关文章
- Hadoop阅读笔记(三)——深入MapReduce排序和单表连接
继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...
- 【SqlServer系列】表连接
1 概述 1.1 已发布[SqlServer系列]文章 [SqlServer系列]MYSQL安装教程 [SqlServer系列]数据库三大范式 [SqlServer系列]表单查询 1.2 本篇 ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parentTom LucyTom JackJone LucyJone JackLucy MaryLucy Ben ...
- 【原创】MapReduce编程系列之二元排序
普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...
- MapReduce编程系列 — 6:多表关联
1.项目名称: 2.程序代码: 版本一(详细版): package com.mtjoin; import java.io.IOException; import java.util.Iterator; ...
- MapReduce编程系列 — 4:排序
1.项目名称: 2.程序代码: package com.sort; import java.io.IOException; import org.apache.hadoop.conf.Configur ...
- MapReduce编程系列 — 3:数据去重
1.项目名称: 2.程序代码: package com.dedup; import java.io.IOException; import org.apache.hadoop.conf.Configu ...
- MapReduce编程系列 — 2:计算平均分
1.项目名称: 2.程序代码: package com.averagescorecount; import java.io.IOException; import java.util.Iterator ...
- MapReduce编程系列 — 1:计算单词
1.代码: package com.mrdemo; import java.io.IOException; import java.util.StringTokenizer; import org.a ...
随机推荐
- 挑战Python-20160826
给你一字典a,如a={1:1,2:2,3:3},输出字典a的key,以','链接,如‘1,2,3'.
- 初探—KMP模式匹配算法
KMP算法思想: 普通的字符串匹配算法S主串必须要回溯.但回溯就影响了效率. 改进的地方也就是这里,我们从P 串本身出发,事先就找准了T自身前后部分匹配的位置,那就可以改进算法. next数组的含义: ...
- Excel导出-Epplus
首先引入EPPlus.dll到你的项目bin文件中. Epplus引用的命名空间为 OfficeOpenXml 下面是对epplus一些用法的总结 一.创建一个空excel表格 //导出EXCEL设置 ...
- Anagrams问题
#include<stdio.h> #include<string.h> int main() { int i; ],word2[]; //分别用于存储输入的两个单词 int ...
- Bootstrap优秀网站:乐窝网
Bootstrap优秀网站:乐窝网 调用谷歌在线地图的API和Bootstrap工具包实现了租房和出租的一个平台. 佩服之极,09年跟一个哥们聊天时,他就提议过这方面的应用,终于看到有人实现了,祝贺. ...
- 关于javac编译时出现“非法字符:\65279”的解决方法
一般用UE或记事本编辑过的UTF-8的文件头会加入BOM标识,该标识由3个char组成.在UTF-8的标准里该BOM标识是可有可无的,Sun 的javac 在编译带有BOM的UTF-8的格式的文件时会 ...
- jna 使用实例,
有与项目组需要用到C++的一个模块, 需要将一个2维数组传到dll 里面 ,返回一个字符串, 恶心了1天终于完成了, 记录一下,同时也希望能给你带来帮助. java 代码如下, package tes ...
- 解决在构造函数中使用Session,Session为null的问题
问题描述: public abstract class PageBase : System.Web.UI.Page 在PageBase中如何使用Session??? 我直接用 Session[&quo ...
- Firefly框架参考
在游戏服务器端,往往需要处理大量的各种各样的任务,每一项任务所需的系统资源也可能不同.而这些复杂的任务只用一个单独的服务器进程是很难支撑和管理起来的.所以,游戏服务器端的开发者往往需要花费大量的时间精 ...
- *[codility]Number-of-disc-intersections
http://codility.com/demo/take-sample-test/beta2010/ 这题以前做的时候是先排序再二分,现在觉得没有必要.首先圆可以看成线段,把线段的进入作为一个事件, ...