hadoop - MapReduce:将 Reducer 的结果分组为固定大小的 block

标签 hadoop mapreduce output reduce reducers

我正在使用 Map Reduce 框架。

假设这是输入列表 [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P ,Q, R, S, T, U, V, W, X, Y, Z] 我的 Mapper 产生以下输出:

<"Key 1" : A>
<"Key 2" : B>
<"Key 3" : C>
<"Key 1" : D>
<"Key 2" : E>
<"Key 3" : F>
<"Key 1" : G>
<"Key 2" : H>
<"Key 3" : I>
<"Key 1" : J>
<"Key 2" : K>
<"Key 3" : L>
<"Key 1" : M>
<"Key 2" : N>
<"Key 3" : O>
<"Key 1" : P>
<"Key 2" : Q>
<"Key 3" : R>
<"Key 1" : S>
<"Key 2" : T>
<"Key 3" : U>
<"Key 1" : V>
<"Key 2" : W>
<"Key 3" : X>
<"Key 1" : Y>
<"Key 2" : Z>

现在 Reducer 的输出通常是这样的:

<"Key 1" : A, D, G, J, M, P, S, V, Y>
<"Key 2" : B, E, H, K, N, Q, T, W, Z>
<"Key 3" : C, F, I, L, O, R, U, X>

但是我想做的是这样的:

我想将每个键的输出组合成 3 个 block ,然后生成最终的 Reducer 输出。

所以我希望我的 Reducer 输出看起来像这样:

<"Key 1" : [A, D, G], [J, M, P], [S, V, Y]>
<"Key 2" : [B, E, H], [K, N, Q], [T, W, Z]>
<"Key 3" : [C, F, I], [L, O, R], [U, X]>

任何帮助将不胜感激,因为两天以来我一直被困在这个问题上。我无法弄清楚最后一部分,即如何将输出分组为 3 个 block 。

P.S. 如果 block 大小小于 3(就像在最后一个键的示例中一样)那么它很好,但不应超过 3。

最佳答案

我觉得,做起来很简单:

  1. 在你的 reducer 中,一次只需将 3 个值放入一个 for 循环中。
  2. 将这三个与您选择的分隔符连接起来并写入上下文

    context.write(键, 值)

Please note that you can write to context as many time you wish, i.e. for each chunk of 3 output simply write to context and then take next set of 3 values.

如果您发现任何困难,请告诉我。

一个更复杂的解决方案可能是使用 MultiOutputs .您甚至可以使用它写入不同的文件。

一个很好的例子是here使用 hadoop 1.0.2

下面是取自 javadocs 的示例:

Usage in Reducer:

 <K, V> String generateFileName(K k, V v) {
   return k.toString() + "_" + v.toString();
 }

 public class MOReduce extends
   Reducer<WritableComparable, Writable,WritableComparable, Writable> {
 private MultipleOutputs mos;
 public void setup(Context context) {
 ...
 mos = new MultipleOutputs(context);
 }

 public void reduce(WritableComparable key, Iterator<Writable> values,
 Context context)
 throws IOException {
 ...
 mos.write("text", , key, new Text("Hello"));
 mos.write("seq", LongWritable(1), new Text("Bye"), "seq_a");
 mos.write("seq", LongWritable(2), key, new Text("Chau"), "seq_b");
 mos.write(key, new Text("value"), generateFileName(key, new Text("value")));
 ...
 }

 public void cleanup(Context) throws IOException {
 mos.close();
 ...
 }

 }

关于hadoop - MapReduce:将 Reducer 的结果分组为固定大小的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32436451/

相关文章:

hadoop - Hadoop 中的 DFSOutputStream ResponseProcessor 异常

hadoop - 运行 WordCount v1.0 示例时 part-00000 中没有输出

c - 关于C中的文件输入和输出

SQL Developer 假脱机 : how to get rid of the first, 空行

java - 从映射列表中输出键/值对

java - Hadoop HDFS 部署

hadoop - 不用时可以关闭Azure HDInsight群集以避免收费吗

sql - Impala 中的日期/字符串比较不起作用(总是返回 false)

hadoop - Cassandra Hadoop map reduce with wide rows ignores slice predicate

hadoop - 使用 FileInputFormat 获取 map 方法中的行号