hadoop程序MapReduce之SingletonTableJoin
需求:单表关联问题。从文件中孩子和父母的关系挖掘出孙子和爷奶关系
样板:child-parent.txt
xiaoming daxiong
daxiong alice
daxiong jack
输出:xiaoming alice
xiaoming jack
分析设计:
mapper部分设计:
1、<k1,k1>k1代表:一行数据的编号位置,v1代表:一行数据。
2、左表:<k2,v2>k2代表:parent名字,v2代表:(1,child名字),此处1:代表左表标志。
3、右表:<k3,v3>k3代表:child名字,v3代表:(2,parent名字),此处2:代表右表标志。
reduce部分设计:
4、<k4,v4>k4代表:相同的key,v4代表:list<String>
5、求笛卡尔积<k5,v5>:k5代表:grandChild名字,v5代表:grandParent名字。
程序部分:
SingletonTableJoinMapper类
package com.cn.singletonTableJoin; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class SingletonTableJoinMapper extends Mapper<Object, Text, Text, Text> {
@Override
protected void map(Object key, Text value, Mapper<Object, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String childName = new String();
String parentName = new String();
String relationType = new String();
String[] values=new String[2];
int i = 0;
StringTokenizer itr = new StringTokenizer(value.toString());
while(itr.hasMoreElements()){
values[i] = itr.nextToken();
i++;
}
if(values[0].compareTo("child") != 0){
childName = values[0];
parentName = values[1];
relationType = "1";
context.write(new Text(parentName), new Text(relationType+" "+childName));
relationType = "2";
context.write(new Text(childName), new Text(relationType+" "+parentName));
}
}
}
SingletonTableJoinReduce类:
package com.cn.singletonTableJoin; import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class SingletonTableJoinReduce extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
List<String> grandChild = new ArrayList<String>();
List<String> grandParent = new ArrayList<String>();
Iterator<Text> itr = values.iterator();
while(itr.hasNext()){
String[] record = itr.next().toString().split(" ");
if(0 == record[0].length()){
continue;
}
if("1".equals(record[0])){
grandChild.add(record[1]);
}else if("2".equals(record[0])){
grandParent.add(record[1]);
}
}
if(0 != grandChild.size() && 0 != grandParent.size()){
for(String grandchild : grandChild){
for(String grandparent : grandParent){
context.write(new Text(grandchild), new Text(grandparent));
}
}
}
}
}
SingletonTableJoin类
package com.cn.singletonTableJoin; 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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; /**
* 单表关联
* @author root
*
*/
public class SingletonTableJoin {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: SingletonTableJoin ");
System.exit(2);
}
//创建一个job
Job job = new Job(conf, "SingletonTableJoin");
job.setJarByClass(SingletonTableJoin.class); //设置文件的输入输出路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); //设置mapper和reduce处理类
job.setMapperClass(SingletonTableJoinMapper.class);
job.setReducerClass(SingletonTableJoinReduce.class); //设置输出key-value数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); //提交作业并等待它完成
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
把总结当成一种习惯。
hadoop程序MapReduce之SingletonTableJoin的更多相关文章
- hadoop程序MapReduce之average
需求:求多门课程的平均值. 样板:math.txt zhangsan 90 lisi 88 wanghua 80 china.txt zhangsan 80lisi 90wanghua 88 输出:z ...
- hadoop程序MapReduce之DataSort
需求:对文件中的数据进行排序. 样本:sort.log 10 13 10 20 输出:1 10 2 10 3 13 4 20 分析部分: mapper分析: 1.<k1,v1>k1代表:行 ...
- hadoop程序MapReduce之DataDeduplication
需求:去掉文件中重复的数据. 样板:data.log 2016-3-1 a 2016-3-2 b 2016-3-2 c 2016-3-2 b 输出结果: 2016-3-1 a 2016 ...
- hadoop程序MapReduce之MaxTemperature
需求:求每年当中最高的温度 样本:temp.log 2016080623 2016072330 2015030420 输出结果:2016 30 2015 20 MapReduce分析设计: Mappe ...
- hadoop程序MapReduce之WordCount
需求:统计一个文件中所有单词出现的个数. 样板:word.log文件中有hadoop hive hbase hadoop hive 输出:hadoop 2 hive 2 hbase 1 MapRedu ...
- 用PHP编写Hadoop的MapReduce程序
用PHP编写Hadoop的MapReduce程序 Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...
- Hadoop之MapReduce程序应用三
摘要:MapReduce程序进行数据去重. 关键词:MapReduce 数据去重 数据源:人工构造日志数据集log-file1.txt和log-file2.txt. log-file1.txt内容 ...
- 如何在Windows下面运行hadoop的MapReduce程序
在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...
- Hadoop之Mapreduce 程序
package com.gylhaut.hadoop.senior.mapreduce; import java.io.IOException; import java.util.StringToke ...
随机推荐
- css3实现立方体效果
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>&l ...
- 国际化的工具类ognl utils
package yycg.util; import java.io.Serializable;import java.text.MessageFormat;import java.util.Array ...
- linux环境变量设置 以及 source命令 Linux 之 /etc/profile、~/.bash_profile 等几个文件的执行过程 Linux 设置环境变量
定制环境变量 环境变量是和Shell紧密相关的,用户登录系统后就启动了一个Shell.对于Linux来说一般是bash,但也可以重新设定或切换到其它的Shell.环境变量文件:/etc/profil ...
- 快速排序——PowerShell版
继续读啊哈磊算法有感系列,继续升华.上一篇是冒泡排序,在结尾总结了一下冒泡排序的缺点——时间复杂度O(N*N)太大.这一篇来说一下快速排序,快速排序可以在多数情况下克服冒泡排序的缺点(最坏的情况下和冒 ...
- RTX——第17章 定时器组
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 RTX 支持的定时器组,或者叫软件定时器,或者叫用户定时器均可.软件定时器的功能比较简 ...
- Activiti - 新一代的开源BPM引擎
Activiti 背景简介.服务和功能介绍 背景介绍 Activiti 其核心是 BPMN 2.0 的流程引擎.BPMN 是目前被各 BPM 厂商广泛接受的 BPM 标准,全称为 Business P ...
- .Net应该学什么怎么学(一)
更新时间:2012年06月05日18时21分 来源:传智播客.Net 上篇<学了.Net做什么开发>中我讲到了目前.Net开发主要方向是Web开发,因此在本篇中我将主要讲解做Web开发要学 ...
- kernel headers
linux/delay延迟相关函数,长延时ssleep msleep(睡眠等待),短延时mdelay udelay(忙等待) linux/sched.h进程相关的头文件, struct task_st ...
- iOS边练边学--UIScrollView和xib文件实现简单分页+定时器初使用
一.xib文件构成 二.自定义控件类(xib文件与自定义控件类的文件名字相同,并且将xib文件中父类控件的类名改成自定义控件类的名称) ***********自定义控件类需要的属性********** ...
- GDI+(一):GDI+ 绘图基础
一.GDI+绘图基础 编写图形程序时需要使用GDI(Graphics Device Interface,图形设备接口),从程序设计的角度看,GDI包括两部分:一部分是GDI对象,另一部分是GDI函数. ...