• Mapreduce实例——单表join


    原理

    以本实验的buyer1(buyer_id,friends_id)表为例来阐述单表连接的实验原理。单表连接,连接的是左表的buyer_id列和右表的friends_id列,且左表和右表是同一个表。因此,在map阶段将读入数据分割成buyer_idfriends_id之后,会将buyer_id设置成keyfriends_id设置成value,直接输出并将其作为左表;再将同一对buyer_idfriends_id中的friends_id设置成keybuyer_id设置成value进行输出,作为右表。为了区分输出中的左右表,需要在输出的value中再加上左右表的信息,比如在valueString最开始处加上字符1表示左表,加上字符2表示右表。这样在map的结果中就形成了左表和右表,然后在shuffle过程中完成连接。reduce接收到连接的结果,其中每个keyvalue-list就包含了"buyer_idfriends_id--friends_idbuyer_id"关系。取出每个keyvalue-list进行解析,将左表中的buyer_id放入一个数组,右表中的friends_id放入一个数组,然后对两个数组求笛卡尔积就是最后的结果了。

    实验环境

    Linux Ubuntu 14.04

    jdk-7u75-linux-x64

    hadoop-2.6.0-cdh5.4.5

    hadoop-2.6.0-eclipse-cdh5.4.5.jar

    eclipse-java-juno-SR2-linux-gtk-x86_64

     

    实验内容

    现有某电商的用户好友数据文件,名为 buyer1buyer1中包含(buyer_id,friends_id)两个字段,内容是以" "分隔,编写MapReduce进行单表连接,查询出用户的间接好友关系。例如:10001的好友是10002,而10002的好友是10005,那么1000110005就是间接好友关系。

    buyer1(buyer_id,friends_id)

    1. 10001   10002  
    2. 10002   10005  
    3. 10003   10002  
    4. 10004   10006  
    5. 10005   10007  
    6. 10006   10022  
    7. 10007   10032  
    8. 10009   10006  
    9. 10010   10005  
    10. 10011   10013  

      统计结果数据如下:

    11. 好友id  用户id  
    12. 10005   10001  
    13. 10005   10003  
    14. 10007   10010  
    15. 10007   10002  
    16. 10022   10004  
    17. 10022   10009  
    18. 10032   10005  

      实验步骤

      1.切换到/apps/hadoop/sbin目录下,开启hadoop

    19. cd /apps/hadoop/sbin  
    20. ./start-all.sh  

      2.Linux本地新建/data/mapreduce7目录。

    21. mkdir -p /data/mapreduce7  

      3.Linux中切换到/data/mapreduce7目录下,用wget命令从http://192.168.1.100:60000/allfiles/mapreduce7/buyer1网址上下载文本文件buyer1

    22. cd /data/mapreduce7  
    23. wget http://192.168.1.100:60000/allfiles/mapreduce7/buyer1  

      然后在当前目录下用wget命令从http://192.168.1.100:60000/allfiles/mapreduce7/hadoop2lib.tar.gz网址上下载项目用到的依赖包。

    24. wget http://192.168.1.100:60000/allfiles/mapreduce7/hadoop2lib.tar.gz  

      hadoop2lib.tar.gz解压到当前目录下。

    25. tar zxvf hadoop2lib.tar.gz  

      4.首先在hdfs上新建/mymapreduce7/in目录,然后将Linux本地/data/mapreduce7目录下的buyer1文件导入到hdfs/mymapreduce7/in目录中。

    26. hadoop fs -mkdir -p /mymapreduce7/in  
    27. hadoop fs -put /data/mapreduce7/buyer1 /mymapreduce7/in  

      5.新建Java Project项目,项目名为mapreduce7

      mapreduce7项目里新建包,包名为mapreduce

      mapreduce包下新建类,类名为DanJoin

      6.添加项目所需依赖的jar包,右键单击mapreduce7,新建一个文件夹,用于存放项目所需的jar包。

      /data/mapreduce7目录下,hadoop2lib目录中的jar包,拷贝到eclipsemapreduce7项目的hadoop2lib目录下。

      选中所有项目hadoop2lib目录下所有jar包,并添加到Build Path中。

      7.编写Java代码,并描述其设计思路

      Map代码

    28. public static class Map extends Mapper<Object,Text,Text,Text>{  
    29.    //实现map函数  
    30. public void map(Object key,Text value,Context context)  
    31.                 throws IOException,InterruptedException{  
    32.                 String line = value.toString();  
    33.                 String[] arr = line.split(" ");   //按行截取  
    34.                 String mapkey=arr[0];  
    35.                 String mapvalue=arr[1];  
    36.                 String relationtype=new String();  //左右表标识  
    37.                 relationtype="1";  //输出左表  
    38.                 context.write(new Text(mapkey),new Text(relationtype+"+"+mapvalue));  
    39.                 //System.out.println(relationtype+"+"+mapvalue);  
    40.                 relationtype="2";  //输出右表  
    41.                 context.write(new Text(mapvalue),new Text(relationtype+"+"+mapkey));  
    42.                 //System.out.println(relationtype+"+"+mapvalue);  
    43.     
    44.         }  
    45.     }  

      Map处理的是一个纯文本文件,Mapper处理的数据是由InputFormat将数据集切分成小的数据集InputSplit,并用RecordReader解析成<key/value>对提供给map函数使用。map函数中用split(" ")方法把每行数据进行截取,并把数据存入到数组arr[],把arr[0]赋值给mapkeyarr[1]赋值给mapvalue。用两个contextwrite()方法把数据输出两份,再通过标识符relationtype12对两份输出数据的value打标记。

      Reduce代码

    46. public static class Reduce extends Reducer<Text, Text, Text, Text>{  
    47.  //实现reduce函数  
    48. public void reduce(Text key,Iterable<Text> values,Context context)  
    49.     throws IOException,InterruptedException{  
    50.     int buyernum=0;  
    51.     String[] buyer=new String[20];  
    52.     int friendsnum=0;  
    53.     String[] friends=new String[20];  
    54.     Iterator ite=values.iterator();  
    55.     while(ite.hasNext()){  
    56.     String record=ite.next().toString();  
    57.     int len=record.length();  
    58.     int i=2;  
    59.     if(0==len){  
    60.     continue;  
    61.     }  
    62.     //取得左右表标识  
    63.     char relationtype=record.charAt(0);  
    64.     //取出record,放入buyer  
    65.     if('1'==relationtype){  
    66.     buyer [buyernum]=record.substring(i);  
    67.     buyernum++;  
    68.     }  
    69.     //取出record,放入friends  
    70.     if('2'==relationtype){  
    71.     friends[friensnum]=record.substring(i);  
    72.     friendsnum++;  
    73.     }  
    74.     }  
    75.     buyernumfriendsnum数组求笛卡尔积  
    76.     if(0!=buyernum&&0!=friendsnum){  
    77.     for(int m=0;m<buyernum;m++){  
    78.     for(int n=0;n<friendsnum;n++){  
    79.     if(buyer[m]!=friends[n]){  
    80.     //输出结果  
    81.     context.write(new Text(buyer[m]),new Text(frinds[n]));  
    82.     }  
    83.     }  
    84.     }  
    85.     }  
    86.     }  

      reduce端在接收map端传来的数据时已经把相同key的所有value都放到一个Iterator容器中valuesreduce函数中,首先新建两数组buyer[]friends[]用来存放map端的两份输出数据。然后Iterator迭代中hasNext()Next()方法加while循环遍历输出values的值并赋值给record,用charAt(0)方法获取record第一个字符赋值给relationtype,用if判断如果relationtype1则把用substring(2)方法从下标为2开始截取record将其存放到buyer[]中,如果relationtype2时将截取的数据放到frindes[]数组中。然后用三个for循环嵌套遍历输出<key,value>,其中key=buyer[m]value=friends[n]

      完整代码

    87. package mapreduce;  
    88. import java.io.IOException;  
    89. import java.util.Iterator;  
    90. import org.apache.hadoop.conf.Configuration;  
    91. import org.apache.hadoop.fs.Path;  
    92. import org.apache.hadoop.io.Text;  
    93. import org.apache.hadoop.mapreduce.Job;  
    94. import org.apache.hadoop.mapreduce.Mapper;  
    95. import org.apache.hadoop.mapreduce.Reducer;  
    96. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
    97. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
    98. public class DanJoin {  
    99.     public static class Map extends Mapper<Object,Text,Text,Text>{  
    100.         public void map(Object key,Text value,Context context)  
    101.                 throws IOException,InterruptedException{  
    102.                 String line = value.toString();  
    103.                 String[] arr = line.split(" ");  
    104.                 String mapkey=arr[0];  
    105.                 String mapvalue=arr[1];  
    106.                 String relationtype=new String();  
    107.                 relationtype="1";  
    108.                 context.write(new Text(mapkey),new Text(relationtype+"+"+mapvalue));  
    109.                 //System.out.println(relationtype+"+"+mapvalue);  
    110.                 relationtype="2";  
    111.                 context.write(new Text(mapvalue),new Text(relationtype+"+"+mapkey));  
    112.                 //System.out.println(relationtype+"+"+mapvalue);  
    113.         }  
    114.     }  
    115.     public static class Reduce extends Reducer<Text, Text, Text, Text>{  
    116.         public void reduce(Text key,Iterable<Text> values,Context context)  
    117.     throws IOException,InterruptedException{  
    118.     int buyernum=0;  
    119.     String[] buyer=new String[20];  
    120.     int friendsnum=0;  
    121.     String[] friends=new String[20];  
    122.     Iterator ite=values.iterator();  
    123.     while(ite.hasNext()){  
    124.     String record=ite.next().toString();  
    125.     int len=record.length();  
    126.     int i=2;  
    127.     if(0==len){  
    128.     continue;  
    129.     }  
    130.     char relationtype=record.charAt(0);  
    131.     if('1'==relationtype){  
    132.     buyer [buyernum]=record.substring(i);  
    133.     buyernum++;  
    134.     }  
    135.     if('2'==relationtype){  
    136.     friends[friendsnum]=record.substring(i);  
    137.     friendsnum++;  
    138.     }  
    139.     }  
    140.     if(0!=buyernum&&0!=friendsnum){  
    141.     for(int m=0;m<buyernum;m++){  
    142.     for(int n=0;n<friendsnum;n++){  
    143.     if(buyer[m]!=friends[n]){  
    144.     context.write(new Text(buyer[m]),new Text(friends[n]));  
    145.     }  
    146.     }  
    147.     }  
    148.     }  
    149.     }  
    150.     }  
    151.     public static void main(String[] args) throws Exception{  
    152.     
    153.     Configuration conf=new Configuration();  
    154.     String[] otherArgs=new String[2];  
    155.     otherArgs[0]="hdfs://localhost:9000/mymapreduce7/in/buyer1";  
    156.     otherArgs[1]="hdfs://localhost:9000/mymapreduce7/out";  
    157.     Job job=new Job(conf," Table join");  
    158.     job.setJarByClass(DanJoin.class);  
    159.     job.setMapperClass(Map.class);  
    160.     job.setReducerClass(Reduce.class);  
    161.     job.setOutputKeyClass(Text.class);  
    162.     job.setOutputValueClass(Text.class);  
    163.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
    164.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
    165.     System.exit(job.waitForCompletion(true)?0:1);  
    166.     
    167.     }  
    168.     }  

      8.DanJoin类文件中,右键并点击=>Run As=>Run on Hadoop选项,将MapReduce任务提交到Hadoop中。

      9.待执行完毕后,进入命令模式下,在hdfs上从Java代码指定的输出路径中查看实验结果。

    169. hadoop fs -ls /mymapreduce7/out  
    170. hadoop fs -cat /mymapreduce7/out/part-r-00000  

  • 相关阅读:
    EF关联
    nopcommerce v3.9中文包
    Android Activity切换与Activity间数据交互
    C#多线程的用法9-Semaphore
    C#多线程的用法8-线程间的协作AutoResetEvent
    C#多线程的用法7-线程间的协作ManualResetEvent
    C#多线程的用法6-线程间的协作Mutex
    C#多线程的用法5-线程间的协作Monitor
    C#多线程的用法4-线程间的协作lock快捷方式
    C#多线程的用法3-线程间的协作Join
  • 原文地址:https://www.cnblogs.com/aishanyishi/p/10304829.html
Copyright © 2020-2023  润新知