Hadoop多输出

时间:2011-12-01 00:48:07

标签: java hadoop

我写了一些hadoop代码来读取映射文件并将其拆分成块并将其写入多个文件,如下所示:

public void map(LongWritable key, Text value, OutputCollector<IntWritable, Text> 
output,Reporter reporter) throws IOException {
String line = value.toString();
    int totalLines = 2000;
int lines = 0;
    int fileNum = 1;
String[] linesinfile = line.split("\n");
    while(lines<linesinfile.length) {
        // I do something like, if lines = totalLines, {
        output.collect(new IntWritable(fileNum), new    
            Text(linesinfile[lines].toString()));
        fileNum++;
        lines = 0;
        }
    lines++;
   }
}

在减少中,我这样做:

public void reduce(IntWritable key, Iterator<Text> values,
OutputCollector<IntWritable, Text> output, Reporter reporter) throws IOException {
     while(values.hasNext()){
    output.collect(key, values.next());
}
}

我的MultiFile类如下:

public class MultiFileOutput extends MultipleTextOutputFormat<IntWritable, Text> {

protected String generateFileNameForKeyValue(IntWritable key, Text content, String 
            fileName) {
    return key.toString() + "-" + fileName;
}
}

主要是,我说:

    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(MultiFileOutput.class);

除了设置OutKey / Value Class等

我做错了什么?我的输出目录总是空的。

由于

1 个答案:

答案 0 :(得分:2)

程序看起来有点复杂。如果目的是将文件拆分成多个文件,那么可以通过几种方式完成。没有地图和减少工作的需要,只需一个地图工作就足够了。

  • 使用o.a.h.mapred.lib.NLineInputFormat从输入中一次读取N行到映射器,然后将这N行写入文件。

  • 在上传文件时将dfs.blocksize设置为所需的文件大小,然后每个映射器将处理一个可写入文件的InputSplit。