我们如何控制特定的键应该进入特定的 reducer ?有人可以帮我解决这个问题吗?
最佳答案
partitioner是控制将哪些 key 发送到特定 reducer 的主要机制。
Partitioner controls the partitioning of the keys of the intermediate map-outputs. The key (or a subset of the key) is used to derive the partition, typically by a hash function. The total number of partitions is the same as the number of reduce tasks for the job. Hence this controls which of the m reduce tasks the intermediate key (and hence the record) is sent for reduction.
默认分区器是 HashPartitoner ,它使用 hashCode()
键对象以确定将给定键发送到哪个归约。
在您的驱动程序中,您可以使用以下方法设置不同的分区程序:
job.setPartitionerClass(Class<? extends Partitioner> cls)
自定义分区器实现了 Partitioner<K2, V2>
具有单一方法的接口(interface) getPartition(KEY key, VALUE value, int numPartitions)
需要实现。
例如 HashPartitioner
实现getPartition
看起来像:
public int getPartition(K2 key, V2 value,int numReduceTasks) {
return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
}
关于hadoop - 我们如何控制特定的键应该进入特定的 reducer ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38736118/