其他分享
首页 > 其他分享> > MapReduce当中Combiner的用法

MapReduce当中Combiner的用法

作者:互联网

马克-to-win @ 马克java社区:防盗版实名手机尾号:73203。在上一章的helloworld例子中,每一个map都可能会产生大量的本地输出,这些输出会通过网络到达reducer端,这样会非常浪费带宽。解决这个问题可以通过Combiner。Combiner的作用就是对map端的输出先做一次合并,是MapReduce的一种优化手段之一。

package com;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
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.GenericOptionsParser;
 
public class WordCountMark_to_win {

    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
        private IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            System.out.println("key is 马克-to-win @ 马克java社区:防盗版实名手机尾号:73203"+key.toString()+" value is "+value.toString());
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class PartitionClass extends Partitioner<Text, IntWritable>
    {
/*
int com.WordCount.PartitionClass.getPartition(Text key, IntWritable value, int numPartitions)
Get the partition number for a given key (hence record) given the total number of partitions i.e. number of reduce-tasks for the job.
Parameters:key the key to be partioned.value the entry value.numPartitions the total number of partitions.
Returns:the partition number for the key.
*/       

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44594249/article/details/96327542

标签:Combiner,MapReduce,value,用法,key,org,apache,import,hadoop
来源: https://www.cnblogs.com/xiaolongxia1922/p/15488985.html