java - MapReduce 输出键升序排列

标签 java sorting hadoop mapreduce

我编写了一个 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

https://hadoop.apache.org/docs/r2.4.1/api/org/apache/hadoop/mapreduce/lib/partition/TotalOrderPartitioner.html

它在 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/

相关文章:

java - Jar 文件在 Windows 上完美运行,但在 Rasp Pi 内的 Ubuntu 上运行失败

sorting - sortMode ="multiple"和 <p :dataTable> clears multisort meta during pagination 中的惰性 ="true"

python - Pandas:如何在 python3 中对混合类型的多索引使用切片?

eclipse - Hadoop Mapreduce ClassNotFoundException

hadoop - 在 Spark 与 Spark 广播变量中处理 Hive Lookup 表

java - Spring MVC 相当于 .Net MVC LinkExtensions.ActionLink 方法

java - 自动监控HTTP资源

java - 如何创建 3 维动态数组?

javascript - 重用共享 Controller 的功能,为每个页面维护单独的状态

sql - 在 Hive 中加入查询