前提准备:

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. C# 实现将 PDF 转文本的功能

    更新 2014年2月27日: 这篇文章最初只描述使用 PDFBox 来解析PDF文件.现在它已经被扩展到包括使用 IFilter 和 iTextSharp 的例程了. 这篇文章和对应的Visual S ...

  2. phpstorm failed to create jvm:error code -6 解决办法 解决方法

    phpStorm 软件打开运行提示 failed to create JVM的解决办法. 修改文件 D:\Program Files (x86)\JetBrains\PhpStorm 7.1.3\bi ...

  3. OpenGL进阶(十四) - UVN Camera实现

    提要 3D游戏中最基本的一个功能就是3D漫游了,玩家可以通过键盘或者鼠标控制自己的视角. 之前我们也学习过一个相关的函数,glLookAt,用来制定摄像机的位置,摄像机观察目标位置,还有摄像机的放置方 ...

  4. [AngularJS] Angular 1.3 Anuglar hint

    Read More: http://www.linkplugapp.com/a/953215 https://docs.google.com/document/d/1XXMvReO8-Awi1EZXA ...

  5. Linux shell 提取文件名和目录名

    ${}用于字符串的读取,提取和替换功能,可以使用${} 提取字符串 1.提取文件名 [root@localhost log]# var=/dir1/dir2/file.txt [root@localh ...

  6. C++ 声明与引用

    任何一个技术或者知识点,都是为了解决某个问题.也就是都有他的使用场景,或者说为什么搞出这种技术. 1.解决的问题:现实生活中的例子,要使用一个工具,必要把工具放在这里,或者说我有这个工具.工具只能放在 ...

  7. Android研究之动态创建UI界面具体解释

     Android的基本UI界面一般都是在xml文件里定义好,然后通过activity的setContentView来显示在界面上.这是Android UI的最简单的构建方式.事实上,为了实现更加复 ...

  8. angularjs中的数据绑定

    这是一个最简单的angularjs的例子,关于数据绑定的,大家可以执行一下,看看效果 <html ng-app> <head> <title>angularjs-i ...

  9. CAS中的ABA问题

    http://coolshell.cn/articles/8239.html CAS的ABA问题 所谓ABA(见维基百科的ABA词条),问题基本是这个样子: 进程P1在共享变量中读到值为A P1被抢占 ...

  10. javascript forEach方法与jQuery each区别

    1.forEach方法 语法: array.forEach(function(currentValue, index, arr), thisValue) 参数: 示例: <!DOCTYPE ht ...