前提准备:

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. 什么是vBlock

    Vblock产品是指一种建立系统的方式,Vblock提供的是一个集成包,是一种完整的IT基础架构,并不是一个具体的设备. 具体点就是:Vblock是一个融合了思科服务器与网络.EMC存储系统与管理软件 ...

  2. 文字尺寸、宽高的测量 Paint FontMetrics

    Paint.FontMetrics类简介 Google文档中的描述: ) throw new IndexOutOfBoundsException(); if (bounds == null) thro ...

  3. C#中 父类与子类相互强制转换之实验

    MSDN是很好,不过,有时需要自己动手实践一下,才能更好的理解和记住一些东西. 我看过很多技术文章,结果到用时,仍然是下不了手.似是而非的. 像上次写的“四舍六入五成双/四舍六入五留双/四舍六入五单双 ...

  4. (转) [Flash/Flex] 用柏林噪音和滤镜制作翻腾的火焰效果----Flash AS3效应

    下图展示的是通过柏林噪声和一些滤镜制作的火焰效果.这个效果是从舞台底部燃起的熊熊烈火.这个效果使用了BitmapData里的perlinNoise方法,以及ColorMatrixFilter和Disp ...

  5. 全民Scheme(2):来自星星的你

    一门编程语言,假设不能对你思考编程的方式产生影响.就不值得去学习.--  Alan Perlis (define rember*   (lambda (a list)     (cond       ...

  6. Scheme 4 Javaer-3.高阶函数

    1.3  Formulating Abstractions with Higher-Order Procedures 教材有时候依照学生的基础.从0讲起:有时候给出一个大图,然后具体地逐一介绍. 本文 ...

  7. Android系统源码学习步骤

    Android系统是基于Linux内核来开发的,在分析它在运行时库层的源代码时,我们会经常碰到诸如管道(pipe).套接字(socket)和虚拟文件系统(VFS)等知识. 此外,Android系统还在 ...

  8. vb.net 鼠标控制

    Public Class Form1 Public Declare Sub mouse_event Lib "user32" Alias "mouse_event&quo ...

  9. iOS 图片比例缩放

    方法 //Resize image - (UIImage *)resizeImage:(UIImage *)image withQuality:(CGInterpolationQuality)qual ...

  10. 用sql语句查出和sql相关的性能计数器

    一台服务器上,用性能监视器死活显示不出来一部分计数器,没办法,用sql语句查了 --所有和sql相关的计数器 select * from sys.dm_os_performance_counters ...