MapReduce实例----单表关联
1.源数据:
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
这是一个child-parent表,第一列是child,第二列是parent。现在要求输出"grandchild--grandparent"表。
2.思路分析:
前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础。"单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘。
连接结果中除去连接的两列就是所需要的结果——"grandchild--grandparent"表。要用MapReduce解决这个实例,首先应该考虑如何实现表的自连接;其次就是连接列的设置;最后是结果的整理。
考虑到MapReduce的shuffle过程会将相同的key会连接在一起,所以可以将map结果的key设置成待连接的列,然后列中相同的值就自然会连接在一起了。再与最开始的分析联系起来:要连接的是左表的parent列和右表的child列,且左表和右表是同一个表,所以在map阶段将读入数据分割成child和parent之后,会将parent设置成key,child设置成value进行输出,并作为左表;再将同一对child和parent中的child设置成key,parent设置成value进行输出,作为右表。为了区分输出中的左右表,需要在输出的value中再加上左右表的信息,比如在value的String最开始处加上字符1表示左表,加上字符2表示右表。这样在map的结果中就形成了左表和右表,然后在shuffle过程中完成连接。reduce接收到连接的结果,其中每个key的value-list就包含了"grandchild--grandparent"关系。取出每个key的value-list进行解析,将左表中的child放入一个数组,右表中的parent放入一个数组,然后对两个数组求笛卡尔积就是最后的结果了。
3.代码如下:
package gl;import java.io.IOException;import java.util.*;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;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;public class Gl {public static int time = 0;/** map将输出分割child和parent,然后正序输出一次作为右表,* 反序输出一次作为左表,需要注意的是在输出的value中必须* 加上左右表的区别标识。*/public static class Map extends Mapper<Object, Text, Text, Text> {// 实现map函数public void map(Object key, Text value, Context context)throws IOException, InterruptedException {String childname = new String();// 孩子名称String parentname = new String();// 父母名称String relationtype = new String();// 左右表标识// 输入的一行预处理文本String line=value.toString();String line1[]=line.split(" ");childname = line1[0];parentname = line1[1];// 输出左表relationtype = "1";context.write(new Text(line1[1]), new Text(relationtype +"+"+ childname + "+" + parentname));// System.out.println(line1[1]+" "+relationtype + "+"+ childname + "+" + parentname);// 输出右表relationtype = "2";context.write(new Text(line1[0]), new Text(relationtype +"+"+ childname + "+" + parentname));// System.out.println(line1[0]+" "+relationtype + "+"+ childname + "+" + parentname);}}public static class Reduce extends Reducer<Text, Text, Text, Text> {// 实现reduce函数public void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {// 输出表头if (0 == time) {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 ite = values.iterator();while (ite.hasNext()) {String record = ite.next().toString();int len = record.length();int i = 2;if (0 == len) {continue;}// 取得左右表标识char relationtype = record.charAt(0);// 定义孩子和父母变量String childname = new String();String parentname = new String();// 获取value-list中value的child,是源数据记录数的两倍while (record.charAt(i) != '+') {childname += record.charAt(i);i++;/** 将+号后面的childname存下来。如:* T* Te* Ter* Terr* Terry* */}// System.out.println(key+" "+childname);i = i + 1;// 获取value-list中value的parent,是源数据记录数的两倍while (i < len) {parentname += record.charAt(i);i++;}// System.out.println(key+" "+relationtype+" "+parentname);// 左表,取出child放入grandchildrenif ('1' == relationtype) {grandchild[grandchildnum] = childname;grandchildnum++;}// 右表,取出parent放入grandparentif ('2' == relationtype) {grandparent[grandparentnum] = parentname;grandparentnum++;}}/** grandchild和grandparent数组求笛卡尔儿积,1表的关系是孙--父,2表的关系是父--爷,为得到孙--爷表,条件是父相同,* 所以1表2表都需要 用父做key,这样map调用完后,每传来一个key,reduce就可以对这个key的一组value值进行处理,在表1里看看有没有孙(0 != grandchildnum)以及表2里有没有爷(0 != grandparentnum),两个都不为空就表示存在孙-爷关系。*/if (0 != grandchildnum && 0 != grandparentnum) {for (int m = 0; m < grandchildnum; m++) {for (int n = 0; n < grandparentnum; n++) {// 输出结果context.write(new Text(grandchild[m]), new Text(grandparent[n]));System.out.println(grandchild[m]+" "+grandparent[n]);}}}}}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: Single Table Join <in> <out>");System.exit(2);}Job job = new Job(conf, "Single Table Join");job.setJarByClass(Gl.class);// 设置Map和Reduce处理类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(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);}}
运行程序,控制台输出如下:
Tom Alice
Tom Jesse
Jone Alice
Jone Jesse
Tom Ben
Tom Mary
Jone Ben
Jone Mary
Philip Alice
Philip Jesse
Mark Alice
Mark Jesse
可是又出现了昨天的问题,输出文件夹里面仍然是空的。。。。。。真的不知道为什么
MapReduce实例----单表关联相关推荐
- Hadoop入门(十六)Mapreduce的单表关联程序
"单表关联"要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘 1 实例描述 给出child-parent(孩子--父母)表,要求输出grandchild-gran ...
- MapReduce编程(五) 单表关联
一.问题描述 下面给出一个child-parent的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格. 输入文件内容如下: child parent Steven Lucy Steven Jack ...
- Hadoop入门(十七)Mapreduce的多表关联程序
多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息 1 实例描述 输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列:另一个代表地址表,包含地址名列和地址编号列. ...
- MapRedece(单表关联)
源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...
- Spark入门(十七)之单表关联
一.单表关联 给出child-parent(孩子--父母)表,要求输出grandchild-grandparent(孙子--祖父母)表 二.maven设置 <?xml version=" ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parent Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Mary Lucy ...
- Hadoop案例之单表关联输出祖孙关系
1.案例描述 实例中给出child-parent(孩子--父母)表,要求输出grandchild-grandparent(孙子--爷奶)表. 样例输入如下所示. file: child p ...
- mysql 单表关联_MySQL 基础之 单表、多表联查
使用和不使用not null 的区别: 不使用: 查询时用'name is null' 作为条件 mysql>create table t8( -> id int auto_increme ...
- 案例3,mapreduce单表关联,根据child-parient表解析出grandchild-grandparient表
1.数据样例如下 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Mary Lucy Ben Jack Alice Jack Jesse Terry Alice ...
最新文章
- springMVC4(9)属性编辑器剖析入参类型转换原理
- 用javascript 禁止右键,禁止复制,禁止粘贴
- Reporting Service
- 牛客网 在线编程 局部最小值位置
- Android——最全的系统对话框(AlertDialog)详解
- 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数
- Elasticsearch 为什么能做到快速检索?秘密在这里!
- python md5加密_python中的md5加密
- 在指定文件类型中递归查找到目标字符串
- ArcPad 10 使用与同步 ArcGIS Server 的数据全攻略(一)
- java 16进制_JAVA 十六进制与字符串的转换
- 最小二乘法求解线性回归模型及求解
- sql注入攻击与防御java_注入攻击(SQL注入防御)
- Verilog 锁存器 触发器 寄存器区别
- 进程、线程、同步问题
- C++(11):多线程同步packaged_task
- 数据分析报告2:Superstore销售情况分析
- storm人偶_STORM TOYS 真人快打系列 MOTARO 茂太郎 可动人偶
- 2022 全球网络黑客常用攻击方法 Top10
- 守株待兔欧洲游-法国(9.30-10.5)(已完工)
热门文章
- 性能与可靠性:Java应用为何像一级方程式赛车
- 【java代理】动态代理
- 旁观ACM ICPC
- 用Rust取代C/C++?为时尚早!
- 四:调用部署在ganache的智能合约
- 最新有关于Android SDK manager 无法更新的问题
- 选用 Vala 的十个理由,用 Vala 开发了170个模块后的感悟
- JNI之C++调用Java类 ——java.lang.String
- unity菜单滑动插件_unity游戏清新风格列表滚动视图滑动界面插件Super ScrollView for UGUI 2.4.1...
- mmpose----AnimalKingdom数据集使用mmpose训练