MapReduce编程实例5
前提准备:
1.hadoop安装运行正常。Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装
2.集成开发环境正常。集成开发环境配置请参考 :Ubuntu 搭建Hadoop源码阅读环境
MapReduce编程实例:
MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析
MapReduce编程实例(五),MapReduce实现单表关联
单表关联:
描述:
单表的自连接求解问题。如下表,根据child-parent表列出grandchild-grandparent表的值。
child parent
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
问题分析:
显然需要分解为左右两张表来进行自连接,而左右两张表其实都是child-parent表,通过parent字段做key值进行连接。结合MapReduce的特性,MapReduce会在shuffle过程把相同的key放在一起传到Reduce进行处理。OK,这下有思路了,将左表的parent作为key输出,将右表的child做为key输出,这样shuffle之后很自然的,左右就连接在一起了,有木有!然后通过对左右表进行求迪卡尔积便得到所需的数据。
- package com.t.hadoop;
- import java.io.IOException;
- 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;
- import org.apache.hadoop.util.GenericOptionsParser;
- /**
- * 单表关联
- * @author daT dev.tao@gmail.com
- *
- */
- public class STJoin {
- public static int time = 0;
- public static class STJoinMapper extends Mapper<Object, Text, Text, Text>{
- @Override
- protected void map(Object key, Text value, Context context)
- throws IOException, InterruptedException {
- String childName = new String();
- String parentName = new String();
- String relation = new String();
- String line = value.toString();
- int i =0;
- while(line.charAt(i)!=' '){
- i++;
- }
- String[] values = {line.substring(0,i),line.substring(i+1)};
- if(values[0].compareTo("child") != 0){
- childName = values[0];
- parentName = values[1];
- relation = "1";//左右表分区标志
- context.write(new Text(parentName),new Text(relation+"+"+childName));//左表
- relation = "2";
- context.write(new Text(childName), new Text(relation+"+"+parentName));//右表
- }
- }
- }
- public static class STJoinReduce extends Reducer<Text, Text, Text, Text>{
- @Override
- protected void reduce(Text key, Iterable<Text> values,Context context)
- throws IOException, InterruptedException {
- if(time ==0){//输出表头
- context.write(new Text("grandChild"), new Text("grandParent"));
- time ++;
- }
- int grandChildNum = 0;
- String[] grandChild = new String[10];
- int grandParentNum = 0;
- String[] grandParent = new String[10];
- Iterator<Text> ite = values.iterator();
- while(ite.hasNext()){
- String record = ite.next().toString();
- int len = record.length();
- int i = 2;
- if(len ==0) continue;
- char relation = record.charAt(0);
- if(relation == '1'){//是左表拿child
- String childName = new String();
- while(i < len){//解析name
- childName = childName + record.charAt(i);
- i++;
- }
- grandChild[grandChildNum] = childName;
- grandChildNum++;
- }else{//是右表拿parent
- String parentName = new String();
- while(i < len){//解析name
- parentName = parentName + record.charAt(i);
- i++;
- }
- grandParent[grandParentNum] = parentName;
- grandParentNum++;
- }
- }
- //左右两表求迪卡尔积
- if(grandChildNum!=0&&grandParentNum!=0){
- for(int m=0;m<grandChildNum;m++){
- for(int n=0;n<grandParentNum;n++){
- System.out.println("grandChild "+grandChild[m] +" grandParent "+ grandParent[n]);
- context.write(new Text(grandChild[m]),new Text(grandParent[n]));
- }
- }
- }
- }
- }
- public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
- Configuration conf = new Configuration();
- String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
- if(otherArgs.length<2){
- System.out.println("parameter error");
- System.exit(2);
- }
- Job job = new Job(conf);
- job.setJarByClass(STJoin.class);
- job.setMapperClass(STJoinMapper.class);
- job.setReducerClass(STJoinReduce.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
- FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
- System.exit(job.waitForCompletion(true)?0:1);
- }
- }
传入参数:
hdfs://localhost:9000/user/dat/stjon_input hdfs://localhost:9000/user/dat/stjon_output
输出结果:
grandChild grandParent
Richard LiHua
Lily Well
Lily MillShell
Havid LiT
Tom Lilei
Tom SuSan
Tom Lili
Tom David
OK~!欢迎同学们多多交流~~
MapReduce编程实例5的更多相关文章
- MapReduce编程实例6
前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...
- MapReduce编程实例4
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- MapReduce编程实例3
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- MapReduce编程实例2
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- 三、MapReduce编程实例
前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...
- hadoop2.2编程:使用MapReduce编程实例(转)
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...
- MapReduce编程实例
MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...
- hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)
刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...
- Hadoop--mapreduce编程实例1
前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...
随机推荐
- 如何查看ESXi的网卡的MAC地址?
直接上图 图一, 物理网卡 图二, vmkernel虚拟网卡 参考资料 ============================ How To Determine Vmkernel Inter ...
- SQL 服务没有及时响应启动或控制请求”的解决方法
解决方法很简单:卸载删除跟IIS相关的几个漏洞安全更新补丁即可,包括KB939373.KB2290570和KB2124261.卸载微软安全更新补丁的方法:打开控制面板的“添加删除程序”面板,勾 ...
- C#基础视频教程7.2 如何编写简单游戏
前面一小节我们实现了简单的碰撞检测,但是实际上游戏的对象并不是一个标准的矩形(小鸟是一个不规则的物体,其实碰撞的管道也是不规则物体),所以如果真的要做的比较完美,我们自己要写一个方法,能够导入一个图像 ...
- MFC用代码加入对话框背景图片和button图片
执行环境:VS2013 一.加入对话框背景图片 ①插入位图,把生成的空白位图进行替换(xxx.bmp图片的名称和格式与生成的空白位图保持一致) ②查看属性,得到位图ID ③编写代码: void CMF ...
- 算法笔记_150:图论之双连通及桥的应用(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Description In order to get from one of the F (1 <= F <= 5,000) graz ...
- 阿里云服务器 centos7 ftp安装
昨天租了阿里云服务器一个月,想玩一下linux系统,结果想用ftp上传本地文件的时候,发现用不了,结果在安装配置的时候折腾了几个小时,在网上查了无数的资料,有的说要改配置文件,有的说要关防火墙,说啥的 ...
- TP模板中如何使用函数
TP模板中如何使用函数 TP中如何将时间戳转换为时间显示 我们往往需要对模板输出变量使用函数,可以使用: {$data.name|md5} 编译后的结果是: <?php echo (md5($d ...
- openerp创建动态视图-fields_view_get
openerp的视图结构是以XML的格式存放于ir.ui.view表中,属于静态格式,设计之后就固定, 但可以通过在model中重写fields_view_get函数,在视图加载时修改arch属性,动 ...
- 如何知道TSQL语句已经运行了多久
如何知道TSQL语句已经运行了多久 ,) --millisecond per tick --如果datediff 函数导致溢出 把下面的millisecond改为second 毫秒改为秒 SELECT ...
- Java之字节码(1) - 深入解析
转载地址 一:Java字节代码的组织形式 类文件{ OxCAFEBABE,小版本号,大版本号,常量池大小,常量池数组,访问控制标记,当前类信息,父类信息,实现的接口个数,实现的接口信息数组,域个数,域 ...