是否可以将 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/