MapReduce的分区与ReduceTask的数量

MapReduce的分区与ReduceTask的数量

在MapReduce中,通过指定分区,会将同一个分区的数据发送到同一个reduce中,例如为了数据的统计,可以把一批类似的数据发 送到同一个reduce当中去,在同一个reduce中统计相同类型的数据,就可以实现类似数据的分区,统计等
直观的说就是相同类型的数据,送到一起去处理,在reduce当中默认分区只有1个。
MapReduce当中的分区类图
在这里插入图片描述

Map的输出到Partition

计算逻辑:对Map输出的key去哈希值,用这个哈希值与reducetask的值取余,余几,就将这个key,value放在对应的分区编号里(分区有多个编号)
reducetask的设置: job.setNumReduceTasks(2);

自定义Partition分区案例

1.需求:将以下数据进行分开处理

我会把partition.txt 放在我的下载中免费下载,上传上去之后还是收费有需要的私信我我发给你
详细数据参见partition.txt 这个文本文件,其中第六个字段表示开奖结果数值,现在以15为分界点,将15以上的结果保存到一个文件,15以下的结果保存到一个文件。
在这里插入图片描述
注意:分区的案例,只能打成jar包发布到集群上面去运行,本地模式已经不能正常运行了

第一步:定义mapper

这里的mapper程序不做任何逻辑,也不对key,与value做任何改变,只是接收数据,然后往下发送

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;


public class MyMapper extends Mapper<LongWritable,Text,Text, NullWritable>{
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        context.write(value,NullWritable.get());
    }
}

第二步:定义reducer逻辑

reducer也不做任何处理,将数据原封不动的输出即可

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;

public class MyReducer extends Reducer<Text, NullWritable,Text,NullWritable> {
    @Override
    protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        context.write(key,NullWritable.get());
    }
}

第三步:自定义partitioner

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
/**
 * 这里的输入类型与map阶段的输出类型相同
 */
public class MyPartitioner extends Partitioner<Text,NullWritable> {
    /**
     * 返回值表示数据要去到哪个分区
     * 返回值只是一个分区的标记,标记所有相同的数据去到指定的分区
     */
    @Override
    public int getPartition(Text text, NullWritable nullWritable, int i) {
        String result = text.toString().split("\t")[5];
        System.out.println(result);
        if (Integer.parseInt(result) > 15){
            return 1;
        }else{
            return 0;
        }
    }
}

第四步:程序main函数入口

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;


public class PartitionMain  extends Configured implements Tool {
    public static void main(String[] args) throws  Exception{
        int run = ToolRunner.run(new Configuration(), new PartitionMain(), args);
        System.exit(run);
    }
    @Override
    public int run(String[] args) throws Exception {
        Job job = Job.getInstance(super.getConf(), PartitionMain.class.getSimpleName());
        job.setJarByClass(PartitionMain.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        TextInputFormat.addInputPath(job,new Path("hdfs://192.168.100.129:8020/partitioner"));
        TextOutputFormat.setOutputPath(job,new Path("hdfs://192.168.100.129:8020/outpartition"));
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setReducerClass(MyReducer.class);
        /**
         * 设置分区类,以及reducetask的个数,注意reduceTask的个数一定要与
         * 分区数保持一致
         */
        job.setPartitionerClass(MyPartitioner.class);
        job.setNumReduceTasks(2);
        boolean b = job.waitForCompletion(true);
        return b?0:1;
    }
}

运行结果

在这里插入图片描述在这里插入图片描述

更好资源:大萝卜博客网 新人建站 求支持

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值