mapreduce编程模型之partitioner

来源:互联网 发布:淘宝买家秀图片不显示 编辑:程序博客网 时间:2024/05/16 12:14

Partitioner是一个mapreduce的顶级抽象类 用于决定map输出的kv键值对 如何按照job.setnumberruce(n)中设置的n个数进行分区 每个kv落在哪个分区之中 以及如何保证相同的key落在同一个分区中 只有相同的key落在同一个分区中 才能保证 这个相同的key可以被同一个reducer处理

上代码

 public static class MyPartitioner extends Partitioner<Text,Text>{        @Override        public int getPartition(Text key, Text value, int numPartitions) {            if (key.toString().equals("hello"))                return 0;            else                return 1;        }    }

这是定义了一个partitoner 这个partitioner定义了只有key为hello的时候是落在0分区 其余的都落在1分区 运行测试结果却是如此

hdfs@yksp005206:/home/jumpserver$ hadoop fs -cat /test/wc/output/part-r-00000
hello value[] hello,hello,hello,
hdfs@yksp005206:/home/jumpserver$ hadoop fs -cat /test/wc/output/part-r-00001
hellp value[] hellp,
hive value[] hive,
kylin value[] kylin,
spark value[] spark,
world value[] world,

partitioner的默认实现是hashpartitioner

(key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
这个的意思是对key进行取hashcode 然后对n进行取余





原创粉丝点击