Hadoop中Combiner的使用

http://blog.csdn.net/ipolaris/article/details/8723782

  在MapReduce中,当map生成的数据过大时,带宽就成了瓶颈,怎样精简压缩传给Reduce的数据,有不影响最终的结果呢。有一种方法就是使用Combiner,Combiner号称本地的Reduce,Reduce最终的输入,是Combiner的输出。下面以《Hadoop in action》中的专利数据为例。我们打算统计每个国家的专利数目。代码如下(使用Combiner的代码注释掉):

package net.csdn.blog.ipolaris.hadoopdemo;  

import java.io.IOException;  

import net.scdn.blog.ipolaris.util.ArgsTool;  

import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.conf.Configured;  
import org.apache.hadoop.fs.Path;  
import org.apache.hadoop.io.IntWritable;  
import org.apache.hadoop.io.LongWritable;  
import org.apache.hadoop.io.Text;  
import org.apache.hadoop.mapreduce.Job;  
import org.apache.hadoop.mapreduce.Mapper;  
import org.apache.hadoop.mapreduce.Reducer;  
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
import org.apache.hadoop.util.Tool;  
import org.apache.hadoop.util.ToolRunner;  

public class Demo1 extends Configured implements Tool{  

    /** 
     * @param args 
     * @throws Exception  
     */  
    public static void main(String[] args) throws Exception {  
        System.exit(ToolRunner.run(new Demo1(), args));  

    }  

    public static class DemoMap extends Mapper<LongWritable, Text, Text, IntWritable>{  

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

            String line = value.toString();  
            String[] splitdata = line.split("\\,");  
            String contry = splitdata[4];  
            System.out.println("country:"+contry);  
            if (contry.trim().equals("\"COUNTRY\"")) {  
                return;  
            }else{  
                context.write(new Text(contry), new IntWritable(1));  
            }  
        }  

    }  

    public static class DemoReduce extends Reducer<Text, IntWritable, Text, IntWritable>{  

        @Override  
        protected void reduce(Text arg0, Iterable<IntWritable> arg1,Context context)  
                throws IOException, InterruptedException {  
            System.out.println("reduce");  
            int sum = 0;  
            for (IntWritable num : arg1) {  
                sum += num.get();  
            }  
            context.write(arg0, new IntWritable(sum));  
        }  

    }  
    @Override  
    public int run(String[] arg0) throws Exception {  
        Configuration conf = getConf();  


        Job job = new Job(conf, "demo1");  
        String inputPath = ArgsTool.getArg(arg0, "input");  
        String outputPath = ArgsTool.getArg(arg0, "output");  

        FileInputFormat.addInputPath(job, new Path(inputPath));  
        FileOutputFormat.setOutputPath(job, new Path(outputPath));  

        job.setJarByClass(Demo1.class);  
        job.setMapperClass(DemoMap.class);  
        job.setReducerClass(DemoReduce.class);  
        //job.setCombinerClass(DemoReduce.class);  
        job.setOutputKeyClass(Text.class);  
        job.setOutputValueClass(IntWritable.class);  
        return job.waitForCompletion(true)?0:1;  
    }  

}  

  可以看出,reduce的输入每个key所对应的value将是一大串1,但处理的文本很多时,这一串1已将占用很大的带宽,如果我们在map的输出给于reduce之前做一下合并或计算,那么传给reduce的数据就会少很多,减轻了网络压力。此时Combiner就排上用场了。我们现在本地把Map的输出做一个合并计算,把具有相同key的1做一个计算,然后再把此输出作为reduce的输入,这样传给reduce的数据就少了很多。Combiner是用reducer来定义的,多数的情况下Combiner和reduce处理的是同一种逻辑,所以job.setCombinerClass()的参数可以直接使用定义的reduce,当然也可以单独去定义一个有别于reduce的Combiner,继承Reducer,写法基本上定义reduce一样。让我们看一下,加入Combiner之前的处理结果
这里写图片描述
  我们看到Reduce input records的值为2923922(在map中删掉了一条数据),而Map input records值为2923923,也就是说每个map input record,对应了一个reduce input record。代表着我们要通过网络传输大量的值。最终的统计结果如下(只截取了一段)
这里写图片描述
我们在看看加上Combiner运行情况
这里写图片描述
Reduce input records**只有565**,大量的map输出已经在Combiner中进行了合并

在举一个完整的小例子(WordCount)

  Combiner是用reducer来定义的,多数的情况下Combiner和reduce处理的是同一种逻辑,所以job.setCombinerClass()的参数可以直接使用定义的reduce

// Combiner
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;


/**
 * combiner必须遵循reducer的规范
 * 可以把它看成一种在map任务本地运行的reducer
 * 使用combiner的时候要注意两点
 * 1、combiner的输入输出数据泛型类型要能跟mapper和reducer匹配
 * 2、combiner加入之后不能影响最终的业务逻辑运算结果
 *
 */
public class WCCombiner extends Reducer<Text, LongWritable, Text, LongWritable>{

}
// Mapper
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

//4个泛型中,前两个是指定mapper输入数据的类型,KEYIN是输入的key的类型,VALUEIN是输入的value的类型
//map 和 reduce 的数据输入输出都是以 key-value对的形式封装的
//默认情况下,框架传递给我们的mapper的输入数据中,key是要处理的文本中一行的起始偏移量,这一行的内容作为value
public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{

    //mapreduce框架每读一行数据就调用一次该方法
    @Override
    protected void map(LongWritable key, Text value,Context context)
            throws IOException, InterruptedException {
        //具体业务逻辑就写在这个方法体中,而且我们业务要处理的数据已经被框架传递进来,在方法的参数中 key-value
        //key 是这一行数据的起始偏移量     value 是这一行的文本内容

        //将这一行的内容转换成string类型
        String line = value.toString();

        //对这一行的文本按特定分隔符切分
        String[] words = StringUtils.split(line, " ");

        //遍历这个单词数组输出为kv形式  k:单词   v : 1
        for(String word : words){

            context.write(new Text(word), new LongWritable(1));

        }
    }
}
//Reducer
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable>{



    //框架在map处理完成之后,将所有kv对缓存起来,进行分组,然后传递一个组<key,valus{}>,调用一次reduce方法
    //<hello,{1,1,1,1,1,1.....}>
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values,Context context)
            throws IOException, InterruptedException {

        long count = 0;
        //遍历value的list,进行累加求和
        for(LongWritable value:values){

            count += value.get();
        }

        //输出这一个单词的统计结果

        context.write(key, new LongWritable(count));

    }
}
//Runner
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * 用来描述一个特定的作业
 * 比如,该作业使用哪个类作为逻辑处理中的map,哪个作为reduce
 * 还可以指定该作业要处理的数据所在的路径
 * 还可以指定改作业输出的结果放到哪个路径
 * ....
 *
 */

 //这是job描述和提交类的规范写法
public class WCRunner extends Configured implements Tool {

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

        Configuration conf = new Configuration();

        Job wcjob = Job.getInstance(conf);

        //设置整个job所用的那些类在哪个jar包
        wcjob.setJarByClass(WCRunner.class);


        //本job使用的mapper和reducer的类
        wcjob.setMapperClass(WCMapper.class);
        wcjob.setReducerClass(WCReducer.class);


        //指定本job使用combiner组件,组件所用的类为
        wcjob.setCombinerClass(WCReducer.class);


        //指定reduce的输出数据kv类型
        wcjob.setOutputKeyClass(Text.class);
        wcjob.setOutputValueClass(LongWritable.class);

        //指定mapper的输出数据kv类型
        wcjob.setMapOutputKeyClass(Text.class);
        wcjob.setMapOutputValueClass(LongWritable.class);


        //指定要处理的输入数据存放路径
        FileInputFormat.setInputPaths(wcjob, new Path("hdfs://XXX:9000/wc/srcdata/"));

        //指定处理结果的输出数据存放路径
        FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://XXX:9000/wc/output3/"));

        //将job提交给集群运行 
        wcjob.waitForCompletion(true);


    }

    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new FlowSumRunner(), args);
        System.exit(res);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值