我的一期快用完了。
我正在尝试从 Reducer 获取不同 key 的不同文件。
分区器
public class customPartitioner extends Partitioner<Text, NullWritable> implements
Configurable {
private Configuration configuration;
@Override
public Configuration getConf() {
return configuration;
}
public int getPartition(Text key, NullWritable value, int numPartitions) {
return Math.abs(key.hashCode()) % numPartitions;
}
}
然后我在我的驱动程序类中设置了以下内容
job0.setPartitionerClass(customPartitioner.class);
job0.setNumReduceTasks(5);
对于 reducer 我有 5 个键
[3, 0, 5, 8, 12]
所以我需要获取 5 个不同的文件。
但是一旦我运行这段代码,我得到了 5 个部分文件,但结果不是预期的。
输出
Found 6 items
-rw-r--r-- 3 sreeveni root 0 2015-12-09 11:44 /OUT/Part/OUT/_SUCCESS
-rw-r--r-- 3 sreeveni root 0 2015-12-09 11:44 /OUT/Part/OUT/part-r-00000
-rw-r--r-- 3 sreeveni root 4 2015-12-09 11:44 /OUT/Part/OUT/part-r-00001
-rw-r--r-- 3 sreeveni root 0 2015-12-09 11:44 /OUT/Part/OUT/part-r-00002
-rw-r--r-- 3 sreeveni root 4 2015-12-09 11:44 /OUT/Part/OUT/part-r-00003
-rw-r--r-- 3 sreeveni root 3 2015-12-09 11:44 /OUT/Part/OUT/part-r-00004
其中2个文件是空的,另一个包含
sreeveni@machine10:~$ hadoop fs -cat /OUT/Part/OUT/part-r-00001
3
8
sreeveni@machine10:~$ hadoop fs -cat /OUT/Part/OUT/part-r-00003
0
5
sreeveni@machine10:~$ hadoop fs -cat /OUT/Part/OUT/part-r-00004
12
为什么 2 个 key 在一个文件下?
我的代码有错吗? 请帮忙
最佳答案
您的分区程序正在做正确的事情,所以我会尝试解释原因。让我们将您的每个输入传递到您的分区代码中,看看会产生什么。 numPartitions
是 5
,因为它是您设置的 reducer 的数量。
int hash = new Text("3").hashCode(); // = 82
hash % numPartitions; // = 2
hash = new Text("0").hashCode(); // = 79
hash % numPartitions; // = 4
hash = new Text("5").hashCode(); // = 84
hash % numPartitions; // = 4
hash = new Text("8").hashCode(); // = 87
hash % numPartitions; // = 2
hash = new Text("12").hashCode(); // = 2530
hash % numPartitions; // = 0
正如我们所见,手动运行得到的结果相同。两个键位于一个文件下,因为分区程序将它们分配给同一个 reducer。分区会在更大的数据集过程中均匀分布,但您不能期望该代码自动且均匀地分布所有输入。
关于hadoop - 即使使用 Hadoop 自定义分区程序,不同的 key 也会进入 1 个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34172101/