前提准备:

1.hadoop安装运行正常。Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装

2.集成开发环境正常。集成开发环境配置请参考 :Ubuntu 搭建Hadoop源码阅读环境

MapReduce编程实例:

MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析

MapReduce编程实例(二),计算学生平均成绩

MapReduce编程实例(三),数据去重

MapReduce编程实例(四),排序

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之后很自然的,左右就连接在一起了,有木有!然后通过对左右表进行求迪卡尔积便得到所需的数据。

  1. package com.t.hadoop;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. import org.apache.hadoop.util.GenericOptionsParser;
  13. /**
  14. * 单表关联
  15. * @author daT dev.tao@gmail.com
  16. *
  17. */
  18. public class STJoin {
  19. public static int time = 0;
  20. public static class STJoinMapper extends Mapper<Object, Text, Text, Text>{
  21. @Override
  22. protected void map(Object key, Text value, Context context)
  23. throws IOException, InterruptedException {
  24. String childName = new String();
  25. String parentName = new String();
  26. String relation = new String();
  27. String line = value.toString();
  28. int i =0;
  29. while(line.charAt(i)!=' '){
  30. i++;
  31. }
  32. String[] values = {line.substring(0,i),line.substring(i+1)};
  33. if(values[0].compareTo("child") != 0){
  34. childName = values[0];
  35. parentName = values[1];
  36. relation = "1";//左右表分区标志
  37. context.write(new Text(parentName),new Text(relation+"+"+childName));//左表
  38. relation = "2";
  39. context.write(new Text(childName), new Text(relation+"+"+parentName));//右表
  40. }
  41. }
  42. }
  43. public static class STJoinReduce extends Reducer<Text, Text, Text, Text>{
  44. @Override
  45. protected void reduce(Text key, Iterable<Text> values,Context context)
  46. throws IOException, InterruptedException {
  47. if(time ==0){//输出表头
  48. context.write(new Text("grandChild"), new Text("grandParent"));
  49. time ++;
  50. }
  51. int grandChildNum = 0;
  52. String[] grandChild = new String[10];
  53. int grandParentNum = 0;
  54. String[] grandParent = new String[10];
  55. Iterator<Text> ite = values.iterator();
  56. while(ite.hasNext()){
  57. String record = ite.next().toString();
  58. int len = record.length();
  59. int i = 2;
  60. if(len ==0)  continue;
  61. char relation = record.charAt(0);
  62. if(relation == '1'){//是左表拿child
  63. String childName = new String();
  64. while(i < len){//解析name
  65. childName = childName + record.charAt(i);
  66. i++;
  67. }
  68. grandChild[grandChildNum] = childName;
  69. grandChildNum++;
  70. }else{//是右表拿parent
  71. String parentName = new String();
  72. while(i < len){//解析name
  73. parentName = parentName + record.charAt(i);
  74. i++;
  75. }
  76. grandParent[grandParentNum] = parentName;
  77. grandParentNum++;
  78. }
  79. }
  80. //左右两表求迪卡尔积
  81. if(grandChildNum!=0&&grandParentNum!=0){
  82. for(int m=0;m<grandChildNum;m++){
  83. for(int n=0;n<grandParentNum;n++){
  84. System.out.println("grandChild "+grandChild[m] +" grandParent "+ grandParent[n]);
  85. context.write(new Text(grandChild[m]),new Text(grandParent[n]));
  86. }
  87. }
  88. }
  89. }
  90. }
  91. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
  92. Configuration conf = new Configuration();
  93. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  94. if(otherArgs.length<2){
  95. System.out.println("parameter error");
  96. System.exit(2);
  97. }
  98. Job job = new Job(conf);
  99. job.setJarByClass(STJoin.class);
  100. job.setMapperClass(STJoinMapper.class);
  101. job.setReducerClass(STJoinReduce.class);
  102. job.setOutputKeyClass(Text.class);
  103. job.setOutputValueClass(Text.class);
  104. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  105. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  106. System.exit(job.waitForCompletion(true)?0:1);
  107. }
  108. }

传入参数:

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的更多相关文章

  1. MapReduce编程实例6

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  2. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  3. MapReduce编程实例3

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  4. MapReduce编程实例2

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  5. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  6. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  7. MapReduce编程实例

    MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...

  8. hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)

    刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...

  9. Hadoop--mapreduce编程实例1

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

随机推荐

  1. grafana-zabbix图形简单配置

    连接zabbix数据库 加入dashboard Home--Add--加入dashboad 设置dashboad 设置名字,和标签tag,tag可在输入后回车加入多个 加入简单的一张图,測试能否获取到 ...

  2. Hive配置与操作实践

    Hive配置与操作实践 @(Hadoop) 安装hive hive的安装十分简单,只需要在一台服务器上部署即可. 上传hive安装包,解压缩,将其配入环境变量. mysql的设置 在要作为元数据库的m ...

  3. vi命令整理

    vi命令整理 u 撤销上一次操作 ctrl+r 恢复上一次操作 : 跳转至第1行 :$ 跳转至最后一行 ctrl+f 向文章末尾翻页 ctrl+b 向文章开始翻页 yy 复制一行 p 粘贴刚刚复制第一 ...

  4. [笔记][Java7并发编程实战手冊]3.8 并发任务间的数据交换Exchanger

    [笔记][Java7并发编程实战手冊]系列文件夹 简单介绍 Exchanger 是一个同步辅助类.用于两个并发线程之间在一个同步点进行数据交换. 同意两个线程在某一个点进行数据交换. 本章exchan ...

  5. apache服务器多端口支持

    本示例支持80,82两端口 修改conf/httpd.conf文件: Listen 改为 Listen Listen # Virtual hosts #Include conf/extra/httpd ...

  6. android中listview点击事件失效的灵异事件

    首先说明一下我想实现的功能: 点击某个item之后,让其颜色发生变化.如果变化网上有很多例子,我就不班门弄斧了.Listview之所以点击没有反应是因为上图中绿色部分(自己定义的一个继承BaseAda ...

  7. IOS客户端Coding项目记录(二)

    9:第三方插件整理 JSON转实体:jsonModel https://github.com/icanzilb/JSONModel/ 美化按键:BButton https://github.com/m ...

  8. Java之深入JVM(6) - 字节码执行引擎(转)

    本文为转载,来自 前面我们不止一次的提到,Java是一种跨平台的语言,为什么可以跨平台,因为我们编译的结果是中间代码—字节码,而不是机器码,那字节码在整个Java平台扮演着什么样的角色的呢?JDK1. ...

  9. Jni中图片传递的3种方式(转)

    java层的图片如何传递到c/c+层处理,处理完之后如何传回java层,下面总结了一下用到的三种方法. 1.将Bitmap转为int[]数组对象,将数组作为参数传递到C/C++层,处理完之后再以int ...

  10. jQuery特性

      CreateTime--2017年7月17日11:45:27Author:Marydon jQuery特性 1.jQuery取值 使用jQuery进行页面取值时,获取到的值的类型有两种:strin ...