我编写了一个 MapReduce 代码,其中的键和值都是整数。我正在使用单个 Reducer。输出是这样的:
Key Value
1 78
128 12
174 26
2 44
2957 123
975 91
有没有办法让输出按升序按键排序?这样输出看起来像这样:
1 78
2 44
128 12
174 26
975 91
2957 123
我需要使用 conf.setComparator
吗?如果是,我该怎么做?
最佳答案
这需要一个
TotalOrderPartitioner
它在 M/R 管道中强制执行一个额外的阶段,以将元素划分到排序的桶中。
TreeMap 解决方案不会在全局范围内工作,而只能在每个 Reducer 中工作。
这是一个显示如何使用 TotalOrderPartioner 的要点(不是我的):https://gist.github.com/asimjalis/e5627dc2ff2b23dac70b
要点的关键要点是:
a) 你需要调用 reducer.setPartitionerClass to TotalOrderPartitioner:
// Use Total Order Partitioner.
reduceJob.setPartitionerClass(TotalOrderPartitioner.class);
b) 您需要生成一组拆分以用作 TOP 的“桶”
// Generate partition file from map-only job's output.
TotalOrderPartitioner.setPartitionFile(
reduceJob.getConfiguration(), partitionPath);
InputSampler.writePartitionFile(reduceJob, new InputSampler.RandomSampler(
1, 10000));
关于java - MapReduce 输出键升序排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528339/