前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础。单表关联这个实例要求从给出的数据中寻找到所关心的数据,它是对原始数据所包含信息的挖掘。下面进入这个实例。

1.实例描述

  实例中给出child-parent表,要求输出grandchild-grandparent表。

  样例输入:

  file:

  child parent
  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

  样例输出:

  

2.设计思路

  分析这个实例,显然需要进行单表连接,连接的是左表的parent列和右表的child列,且左表和右表是同一个表。连接结果中除去连接的两列就是所需要的结果----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.程序代码:

  1. import java.io.IOException;
  2. import java.util.Iterator;
  3.  
  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. public class STjoin {
  15.  
  16. public static int time = 0;
  17. // Map 将输入分割成child和parent,然后正序输出一次作为右表,反序输出一次作为左表,需要
  18. // 注意的是在输出的value中必须加上左右表区别标志
  19. public static class Map extends Mapper<Object, Text, Text, Text>{
  20. @Override
  21. protected void map(Object key, Text value,Mapper<Object, Text, Text, Text>.Context context)
  22. throws IOException, InterruptedException {
  23. // super.map(key, value, context);
  24. String childname = new String();
  25. String parentname = new String();
  26. String relationtype = 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. relationtype = "1"; // 左右表区分标志
  37. context.write(new Text(values[1]), new Text(relationtype+"+"+childname+"+"+parentname)); // 左表
  38. relationtype = "2";
  39. context.write(new Text(values[0]), new Text(relationtype+"+"+childname+"+"+parentname)); // 右表
  40. }
  41. }
  42. }
  43.  
  44. public static class Reduce extends Reducer<Text, Text, Text, Text>{
  45. @Override
  46. protected void reduce(Text key, Iterable<Text> values,Reducer<Text, Text, Text, Text>.Context context)
  47. throws IOException, InterruptedException {
  48. // super.reduce(arg0, arg1, arg2);
  49. if(time==0){ // 输出表头
  50. context.write(new Text("grandchild"), new Text("grandparent"));
  51. time++;
  52. }
  53. int grandchildnum = 0;
  54. String grandchild[] = new String[10];
  55. int grandparentnum = 0;
  56. String grandparent[] = new String[10];
  57. Iterator ite = values.iterator();
  58. while(ite.hasNext()){
  59. String record = ite.next().toString();
  60. int len = record.length();
  61. int i = 2;
  62. if(len == 0) continue;
  63. char relationtype = record.charAt(0);
  64. String childname = new String();
  65. String parentname = new String();
  66. // 获取value-list中value的child
  67. while(record.charAt(i)!='+'){
  68. childname = childname + record.charAt(i);
  69. i++;
  70. }
  71. i = i+1;
  72. // 获取value-list中value的parent
  73. while(i<len){
  74. parentname = parentname+record.charAt(i);
  75. i++;
  76. }
  77. // 左表,取出child放入grandchild
  78. if(relationtype == '1'){
  79. grandchild[grandchildnum] = childname;
  80. grandchildnum++;
  81. }else { // 右表,取出parent放入grandparent
  82. grandparent[grandparentnum] = parentname;
  83. grandparentnum++;
  84. }
  85. }
  86. // grandchild 和 grandparent 数组求笛卡尔积
  87. if(grandparentnum !=0 && grandchildnum !=0){
  88. for(int m=0;m<grandchildnum;m++){
  89. for(int n=0; n<grandparentnum;n++){
  90. context.write(new Text(grandchild[m]), new Text(grandparent[n])); // 输出结果
  91. }
  92. }
  93. }
  94. }
  95. }
  96.  
  97. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  98. Configuration conf = new Configuration();
  99. String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
  100. if(otherArgs.length!=2){
  101. System.out.println("Usage:wordcount <in> <out>");
  102. System.exit(2);
  103. }
  104. Job job = new Job(conf,"sing table join");
  105. job.setJarByClass(STjoin.class);
  106. job.setMapperClass(Map.class);
  107. job.setReducerClass(Reduce.class);
  108. job.setOutputKeyClass(Text.class);
  109. job.setOutputValueClass(Text.class);
  110. FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
  111. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  112. System.exit(job.waitForCompletion(true)?0:1);
  113. }
  114.  
  115. }

  

Hadoop 单表关联的更多相关文章

  1. Hadoop 多表关联

    一.实例描述 多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息.下面进入这个实例. 输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列:另一个代表地址列,包含地 ...

  2. Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException

    简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...

  3. MapReduce应用案例--单表关联

    1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. ...

  4. MapRedece(单表关联)

    源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...

  5. MR案例:单表关联查询

    "单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘. 需求:实例中给出 child-parent(孩子—父母)表,要求输出 grandchild ...

  6. MapReduce编程系列 — 5:单表关联

    1.项目名称: 2.项目数据: chile    parentTom    LucyTom    JackJone    LucyJone    JackLucy    MaryLucy    Ben ...

  7. 利用hadoop来解决“单表关联”的问题

    已知 child parent a b a c d b d c b e b f c g c h x g x h m x m n o x o n 则 c 2+c+g 2+c+h 1+a+c 1+d+c ...

  8. MapReduce单表关联学习~

    首先考虑表的自连接,其次是列的设置,最后是结果的整理. 文件内容: import org.apache.hadoop.conf.Configuration; import org.apache.had ...

  9. mapreduce-实现单表关联

    //map类 package hadoop3; import java.io.IOException; import org.apache.hadoop.io.LongWritable;import ...

随机推荐

  1. [leetcode]149. Max Points on a Line多点共线

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  2. Unity正交模式摄像机与屏幕适配的方法

    public class CameraAuto : MonoBehaviour { float fDefaultRatio = 720.0f / 1280.0f;//预先设定屏幕大小1280*720 ...

  3. Python中的计时器对象

    计时器对象用于特定时间运行的操作.往往被安排到特定的单独的线程上运行, 但是计时器初始化的时间间隔可能不是解释器实际执行操作时的实际时刻, 因为线程调度程序负责实际调度与计时器对象相对应的线程. Ti ...

  4. Linux静态设置CentOS 7虚拟机的IP

    进入root ,输入命令:# vi /etc/sysconfig/network-scripts/ifcfg-ens33 .将DHCP协议获取IP,改为static静态,加上想要设置的IPADDR即可 ...

  5. Swoole 心跳检测

    Swoole的心跳检测特别简单,只需要配置 heartbeat_check_interval,heartbeat_idle_time就可以了. heartbeat_check_interval:表示服 ...

  6. [基础篇] 01_MySQL的安装与配置

  7. javaMail的使用以及trying to connect to host "1xxx@163.com", port 25, isSSL false异常

    最近项目用到邮件系统,开始了解javaMail...话不多说先上代码: pom依赖: <!--    邮件  https://mvnrepository.com/artifact/javax.m ...

  8. mysql too many connection 解决

    最近的项目用了动态切换数据源起初感觉还好,后来发现每次切换数据库都会创建一个新的连接,这样就导致大量的sleep线程.而mysql的默认sleep时间是28800秒....默认最大连接数为151,这就 ...

  9. JavaScript中的闭包永远都存储在内存中,除非关闭浏览器

    //閉包實現累加功能 function fn1() { var n = 1; add = function() { n += 1; } function fn2() { n += 1; console ...

  10. vb编程中的is是什么意思??

    在select case 语句中可以使用关系运算符大于>小于<等于=等关系运算符,需要用关键字IS和TO.用个例子来说明:Private Sub Command1_Click()Dim a ...