zoukankan      html  css  js  c++  java
  • MapReduce编程实例5

    前提准备:

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

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

    传入参数:

    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~!欢迎同学们多多交流~~

  • 相关阅读:
    1065. [Nescafe19] 绿豆蛙的归宿(概率)
    SDOI 2016 Round1 Day2
    SDOI 2016 Round1 Day1
    Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
    串 2016Vijos省选集训 day3[AC自动机]
    java连接elastic search 9300
    maven安装,maven命令行使用
    eclipse/IDEA使用maven
    hadoop笔记 基础 归档
    tpot蜜罐平台搭建
  • 原文地址:https://www.cnblogs.com/gaochsh/p/7802892.html
Copyright © 2011-2022 走看看