MapReduce设计模式总结

来源:互联网 发布:淘宝广告自己跳出来 编辑:程序博客网 时间:2024/05/16 08:20

MapReduce中的两表join方案简介

1. 概述

在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的。而在Hadoop中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的技巧。本文首先介绍了Hadoop上通常的JOIN实现方法,然后给出了几种针对不同输入数据集的优化方法。  

2. 常见的join方法介绍(假设要进行join的数据分别来自File1和File2)

2.1reduce side join(常规模式:在reduce端进行join操作)

reduceside join是一种最简单的join方式,其主要思想如下:

在map阶段,map函数同时读取两个文件File1和File2,为了区分两种来源的key/value数据对,对每条数据打一个标签(tag),比如:tag=1表示来自文件File1,tag=2表示来自文件File2。即:map阶段的主要任务是对不同文件中的数据打标签。

在reduce阶段,reduce函数获取key相同的来自File1和File2文件的valuelist,然后对于同一个key,对File1和File2中的数据进行join(笛卡尔乘积)。即:reduce阶段进行实际的连接操作。

实例分析:假设我们有两个数据文件如下所示:


代码实现:

publicclass MyJoin{

    public static class MapClass extendsMapper<LongWritable, Text, Text, Text>{

        //最好在map方法外定义变量,以减少map计算时创建对象的个数

        private Text key = new Text();

        private Text value = new Text();

        private String[] keyValue = null;       

        @Override

        protected void map(LongWritable key,Text value, Context context)throws IOException, InterruptedException{

            //采用的数据输入格式是TextInputFormat,文件被分为一系列以换行或者制表符结束的行

            //key是每一行的位置(偏移量,LongWritable类型),

            //value是每一行的内容,Text类型,所有我们要把key从value中解析出来

            keyValue =value.toString().split(",", 2);

            this.key.set(keyValue[0]);

            this.value.set(keyValue[1]);

            context.write(this.key,this.value);

       }       

 }   

publicstatic class Reduce extends Reducer<Text, Text, Text, Text> {

        //最好在reduce方法外定义变量,以减少reduce计算时创建对象的个数

        private Text value = new Text();       

        @Override

         protected void reduce(Text key,Iterable<Text> values, Context context) throws IOException,InterruptedException{

            StringBuilder valueStr = newStringBuilder();           

            //values中的每一个值是不同数据文件中的具有相同key的值

            //即是map中输出的多个文件相同key的value值集合

            for(Text val : values){

                valueStr.append(val);

                valueStr.append(",");

            }           

           this.value.set(valueStr.deleteCharAt(valueStr.length()-1).toString());

            context.write(key, this.value);

        }       

    }   

    public static void main(String[] args)throws Exception{

        Configuration conf = newConfiguration();

        Job job = new Job(conf,"MyJoin");       

        job.setJarByClass(MyJoin.class);

        job.setMapperClass(MapClass.class);

        job.setReducerClass(Reduce.class);

        //job.setCombinerClass(Reduce.class);       

        job.setOutputKeyClass(Text.class);

       job.setOutputValueClass(Text.class);       

        //分别采用TextInputFormat和TextOutputFormat作为数据的输入和输出格式

        //如果不设置,这也是Hadoop默认的操作方式

        job.setInputFormatClass(TextInputFormat.class);

       job.setOutputFormatClass(TextOutputFormat.class);       

        FileInputFormat.addInputPath(job, newPath(args[0]));

        FileOutputFormat.setOutputPath(job, newPath(args[1]));       

        System.exit(job.waitForCompletion(true)? 0 : 1);

    }

}

2.2 mapside join(map端进行join操作,而无需reduce阶段。其中两个表规模相差较大,即大小表join操作)

之所以存在reduceside join,是因为在map阶段不能获取所有需要的join字段,即:同一个key对应的字段可能位于不同map中。Reduceside join是非常低效的,因为shuffle阶段要进行大量的数据传输。

Mapside join是针对以下场景进行的优化:两个待连接表中,有一个表非常大,而另一个表非常小,以至于小表可以直接存放到内存中。这样,我们可以将小表复制多份,让每个maptask内存中存在一份(比如存放到hashtable中),然后只扫描大表:对于大表中的每一条记录key/value,在hashtable中查找是否有相同的key的记录,如果有,则连接后输出即可。

为了支持文件的复制,Hadoop提供了一个类DistributedCache,使用该类的方法如下:(1)用户使用静态方法DistributedCache.addCacheFile()指定要复制的文件,它的参数是文件的URI(如果是HDFS上的文件,可以这样:hdfs://namenode:9000/home/XXX/file,其中9000是自己配置的NameNode端口号)。JobTracker在作业启动之前会获取这个URI列表,并将相应的文件拷贝到各个TaskTracker的本地磁盘上。(2)用户使用DistributedCache.getLocalCacheFiles()方法获取文件目录,并使用标准的文件读写API读取相应的文件。

在本例中,我们仍然采用上一例中的数据文件。本实例中的运行参数需要三个,加入在hdfs中有两个目录input和input2,其中input2存放user.csv,input存放order.csv,则运行命令格式如下:
hadoopjar xxx.jar JoinWithDistribute input2/user.csv input output

代码实现:

public class JoinWithDistribute extendsConfigured implements Tool{

    public static class MapClass extendsMapReduceBase implements Mapper<LongWritable, Text, Text, Text>{

       //用于缓存小表的数据,在这里我们缓存user.csv文件中的数据

       private Map<String, String> users = new HashMap<String,String>();

       private Text outKey = new Text();

       private Text outValue = new Text();

       //此方法会在map方法执行之前执行

       @Override

       public void configure(JobConf job){//将小表中的数据缓存到DistributedCache中,用于和大表join

            BufferedReader in = null;

            try{//从当前作业中获取要缓存的文件                

                Path[] paths =DistributedCache.getLocalCacheFiles(job);

                String user = null;

                String[] userInfo = null;

                for (Path path : paths){

                    if(path.toString().contains("user.csv")){

                        in = newBufferedReader(new FileReader(path.toString()));

                        while (null != (user =in.readLine())){

                            userInfo =user.split(",", 2);

                            //缓存文件中的数据

                           users.put(userInfo[0], userInfo[1]);

                        }

                    }

                }

            }catch (IOException e){

                e.printStackTrace();

            }finally{

                try{

                   in.close();

                } catch (IOException e) {               

                    e.printStackTrace();

                }

            }

       }

      public voidmap(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporterreporter) throws IOException{

     //首先获取order文件中每条记录的userId,再去缓存中取得相同userId的user记录,合并两记录并输出之。

            String[] order =value.toString().split(",");

            String user =users.get(order[0]);           

            if(user != null){

                outKey.set(user);

                outValue.set(order[1]);

                output.collect(outKey,outValue);

            }

       }

   }

   public int run(String[] args) throws Exception{

       JobConf job = new JobConf(getConf(), JoinWithDistribute.class);

       job.setJobName("JoinWithDistribute");

       job.setMapperClass(MapClass.class);

        job.setNumReduceTasks(0);//注意:Mapside join无需Reduce的参与!

       job.setInputFormat(TextInputFormat.class);

       job.setOutputFormat(TextOutputFormat.class);

       job.setMapOutputKeyClass(Text.class);

       job.setMapOutputValueClass(Text.class);       

       //我们把第一个参数的地址作为要缓存的文件路径

        DistributedCache.addCacheFile(newPath(args[0]).toUri(), job);

       FileInputFormat.setInputPaths(job, new Path(args[1]));

       FileOutputFormat.setOutputPath(job, new Path(args[2]));

       JobClient.runJob(job);

       return 0;

   }

   public static void main(String[] args) throws Exception{

       int res = ToolRunner.run(new Configuration(), new JoinWithDistribute(),args);

       System.exit(res);

   }

}

2.3SemiJoin(在Map阶段过滤掉大表中不参与join操作的数据,并在Reduce阶段进行join操作)

SemiJoin,也叫半连接,是从分布式数据库中借鉴过来的方法。它的产生动机是:对于reduceside join,跨机器的数据传输量非常大,这成了join操作的一个瓶颈,如果能够在map端过滤掉不会参加join操作的数据,则可以大大节省网络IO。

实现方法很简单:选取一个小表,假设是File1,将其参与join的key抽取出来,保存到文件File3中,File3文件一般很小,可以放到内存中。在map阶段,使用DistributedCache将File3复制到各个TaskTracker上,然后将File2中不在File3中的key对应的记录过滤掉,剩下的reduce阶段的工作与reduceside join相同。

此实例中,还是采用第一个实例中的数据,假如我们只过滤sex为1的user,并将key存于user_id文件中(注意:每行的数据一定要带上双引号啊),如下:


代码实现:

public class SemiJoin extends Configuredimplements Tool{

   public static class MapClass extends Mapper<LongWritable, Text, Text,Text>{

       // 用于缓存user_id文件中的数据

       private Set<String> userIds = new HashSet<String>();       

       private Text key = new Text();

       private Text value = new Text();

       private String[] keyValue;

       // 此方法会在map方法执行之前执行

       @Override

       protected void setup(Context context) throws IOException,InterruptedException {

            BufferedReader in = null;

            try{// 从当前作业中获取要缓存的文件               

                Path[] paths =DistributedCache.getLocalCacheFiles(context.getConfiguration());

                String userId = null;

                for (Path path : paths){

                    if(path.toString().contains("user_id")){

                        in = newBufferedReader(new FileReader(path.toString()));

                        while (null != (userId =in.readLine())){

                           userIds.add(userId);

                        }

                    }

                }

            }catch (IOException e){

                e.printStackTrace();

            }finally{

                try{

                    if(in != null){

                        in.close();

                    }

                }catch (IOException e){

                    e.printStackTrace();

                }

            }

       }

       public void map(LongWritable key, Text value,Context context)  throws IOException,InterruptedException{

            // 在map阶段过滤掉不需要的数据

            this.keyValue =value.toString().split(",");           

            if(userIds.contains(keyValue[0])){

                this.key.set(keyValue[0]);

                this.value.set(keyValue[1]);

                context.write(this.key,this.value);

            }

       }

   }

   public static class Reduce extends Reducer<Text, Text, Text,Text>{

       private Text value = new Text();

       private StringBuilder sb;       

         public void reduce(Text key,Iterable<Text> values, Context context)throws IOException,InterruptedException{           

            sb = new StringBuilder();

            for(Text val : values){

                sb.append(val.toString());

                sb.append(",");

            }           

           this.value.set(sb.deleteCharAt(sb.length()-1).toString());

            context.write(key, this.value);

       }       

   }   

   public int run(String[] args) throws Exception{

       Job job = new Job(getConf(), "SemiJoin");

       job.setJobName("SemiJoin");

       job.setJarByClass(SemiJoin.class);

       job.setMapperClass(MapClass.class);

       job.setReducerClass(Reduce.class);

       job.setInputFormatClass(TextInputFormat.class);

       job.setOutputFormatClass(TextOutputFormat.class);

       job.setOutputKeyClass(Text.class);

       job.setOutputValueClass(Text.class);

       String[] otherArgs = new GenericOptionsParser(job.getConfiguration(),args).getRemainingArgs();       

       // 我们把第一个参数的地址作为要缓存的文件路径

        DistributedCache.addCacheFile(newPath(otherArgs[0]).toUri(), job.getConfiguration());

       FileInputFormat.addInputPath(job, new Path(otherArgs[1]));

       FileOutputFormat.setOutputPath(job, new Path(otherArgs[2]));

       return job.waitForCompletion(true) ? 0 : 1;

   }

   public static void main(String[] args) throws Exception{

       int res = ToolRunner.run(new Configuration(), new SemiJoin(), args);

       System.exit(res);

   }

}

2.4reduce side join + BloomFilter(解决SemiJoin时抽取的小表规模过大的问题,可采用BloomFilter)

在某些情况下,SemiJoin抽取出来的小表的key集合在内存中仍然存放不下,这时候可以使用BloomFilter以节省空间。

BloomFilter最常见的作用是:判断某个元素是否在一个集合里面。它最重要的两个方法是:add()和contains()。最大的特点是不会存在falsenegative,即:如果contains()返回false,则该元素一定不在集合中,但会存在一定的truenegative,即:如果contains()返回true,则该元素一定可能在集合中。

因而可将小表中的key保存到BloomFilter中,在map阶段过滤大表,可能有一些不在小表中的记录没有过滤掉(但是在小表中的记录一定不会过滤掉),这没关系,只不过增加了少量的网络IO而已。

3. 二次排序

在Hadoop中,默认情况下是按照key进行排序,如果要按照value进行排序怎么办?即:对于同一个key,reduce函数接收到的valuelist是按照value排序的。这种应用需求在join操作中很常见,比如,希望相同的key中,小表对应的value排在前面。

有两种方法进行二次排序,分别为:bufferand in memory sort和value-to-key conversion。

对于bufferand in memory sort,主要思想是:reduce()函数中,将某个key对应的所有value保存下来,然后进行排序。 这种方法最大的缺点是:可能会造成outof memory(OOM错误)。

对于value-to-keyconversion,主要思想是:key和部分value拼接成一个组合key(实现WritableComparable接口或者调用setSortComparatorClass函数),这样reduce获取的结果便是先按key排序,后按value排序的结果,需要注意的是,用户需要自己实现Paritioner,以便只按照key进行数据划分。Hadoop显式的支持二次排序,在Configuration类中有个setGroupingComparatorClass()方法,可用于设置排序group的key值。



0 0