MapReduce Partitioner 二次分类

Partitioner:

  二次分类,我的理解是:如果不加Partitioner组件,Reduce会将输入的所有类型数据整合在一个文件,如果加了,可以让reduce根据类型再做一次分类,分出多个Reduce,输出多个文件

 

图:

MapReduce Partitioner 二次分类

 

代码:

/**
     * 自定义的Partitioner
     * 二次分类
     */
    public static class MyPartitioner extends Partitioner<Text, LongWritable> {

        public int getPartition(Text key, LongWritable value, int i) {
            if (key.toString().equals("xiaomi")) {
                //指定交由第几个Reduce处理,后面输出的文件名也是xxx0001
                return 0;
            } else if (key.toString().equals("huawei")) {
                //后面输出的文件名也是xxx0002
                return 1;
            } else if (key.toString().equals("sanxing")) {
                return 2;
            } else if (key.toString().equals("iphone")) {
                return 3;
            } else {
                return 4;
            }
        }
    }
//在作业提交之前设置Partitioner参数
        job.setPartitionerClass(MyPartitioner.class);
        //设置5个Reduce分区,每个类别一个Reduce,不设置这句,Partitioner不生效
        job.setNumReduceTasks(5);