java - 将 MapReduce 输出拆分为多个输出文件

标签 java hadoop mapreduce hdfs

是否可以将 mapreduce 作业的输出拆分为多个文件而不是单个“part-r-00000”文件?

我遇到过 MultipleOutputFormat 类,但从我读到的内容来看,它似乎只根据键将输出分解为文件。 MultipleOutputFormat

我正在寻找的是,以 WordCount 作业为例,将输出分成多个文件。

最佳答案

关于 Wordcount,我也有类似的问题。在我的例子中,我需要将以每个字母开头的单词写到单独的文件中。这里我使用了 MultipleOutputs

public class NameCountReducer extends Reducer<Text, NameCountTuple, Text, NameCountTuple> {
private NameCountTuple result = null;
private MultipleOutputs<Text,NameCountTuple> out;

 public void setup(Context context) {
   out = new MultipleOutputs<Text,NameCountTuple>(context);   
 }
public void reduce(Text key, Iterable<NameCountTuple> values, Context context)
        throws IOException, InterruptedException {
    int count = 0;
    for (HITuple val : values) {

        count += val.getCount();
    }
    result.setCount(count);
    out.write(key, result,"outputpath/"+key.getText().charAt(0));
}
public void cleanup(Context context) throws IOException,InterruptedException {
    out.close();        
 }

这里它给出了以下路径的输出

outputpath/a
          /b
          /c
 .......

为此,您应该使用 LazyOutputFormat.setOutputFormatClass() 而不是 FileOutputFormat。还需要将作业配置添加为 job.setOutputFormatClass(NullOutputFormat.class)

关于java - 将 MapReduce 输出拆分为多个输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21571069/

相关文章:

java - 如何制作匹配带定界符和分隔符的标记的正则表达式?

java - Web 应用程序与第三方应用程序的集成

java - 为什么 java.lang.Thread 在启动时不调用其显式 java.lang.Runnable 的 run() 方法?

hadoop - Hadoop TestDFSIO的度量单位是什么?

hadoop - Oozie工作的问题,需要多位 parent 的输出

hadoop - 使用 hadoop mapreduce 作业从日志文件分析时间范围内的总错误条目发生率

java - 对目录结构有点困惑

hadoop - 将 oozie 作业上次运行日期传播到最后一个值

arrays - 计算 Hive 数组中连续日期之间的差异

java - Hadoop 数据从两个文件加入 - 如何强制映射器读取特定文件