2012-05-03 58 views
2

我的Reduce操作产生的输出文件很大(Gzipping后1 GB)。我希望它产生中断输出到200 MB的较小文件。是否有一个属性/ Java类来按大小分割减少输出?线条? 我无法增加还原器的数量,因为这会对hadoop作业的性能产生负面影响。Hadoop中的分裂Reducer输出

回答

2

我很好奇,为什么你不能只是使用更多的减速,但我会相信你的话。

你可以做的一个选择是使用MultipleOutputs并从一个reducer写入多个文件。例如,假设每个reducer的输出文件都是1GB,而您想要256MB文件。这意味着您需要为每个Reducer写入4个文件,而不是一个文件。

在你的工作的驱动程序,这样做:

JobConf conf = ...; 

// You should probably pass this in as parameter rather than hardcoding 4. 
conf.setInt("outputs.per.reducer", 4); 

// This sets up the infrastructure to write multiple files per reducer. 
MultipleOutputs.addMultiNamedOutput(conf, "multi", YourOutputFormat.class, YourKey.class, YourValue.class); 

在你减速,这样做:

@Override 
public void configure(JobConf conf) { 
    numFiles = conf.getInt("outputs.per.reducer", 1); 
    multipleOutputs = new MultipleOutputs(conf); 

    // other init stuff 
    ... 
} 

@Override 
public void reduce(YourKey key 
        Iterator<YourValue> valuesIter, 
        OutputCollector<OutKey, OutVal> ignoreThis, 
        Reporter reporter) { 
    // Do your business logic just as you're doing currently. 
    OutKey outputKey = ...; 
    OutVal outputVal = ...; 

    // Now this is where it gets interesting. Hash the value to find 
    // which output file the data should be written to. Don't use the 
    // key since all the data will be written to one file if the number 
    // of reducers is a multiple of numFiles. 
    int fileIndex = (outputVal.hashCode() & Integer.MAX_VALUE) % numFiles; 

    // Now use multiple outputs to actually write the data. 
    // This will create output files named: multi_0-r-00000, multi_1-r-00000, 
    // multi_2-r-00000, multi_3-r-00000 for reducer 0. For reducer 1, the files 
    // will be multi_0-r-00001, multi_1-r-00001, multi_2-r-00001, multi_3-r-00001. 
    multipleOutputs.getCollector("multi", Integer.toString(fileIndex), reporter) 
     .collect(outputKey, outputValue); 
} 

@Overrider 
public void close() { 
    // You must do this!!!! 
    multipleOutputs.close(); 
} 

这个伪代码在头脑里的旧的MapReduce API编写的。尽管使用mapreduce api存在等价的apis,但无论如何,您应该全部设置好。

+0

我无法增加reducer的数量,因为它会让作业变慢,因为需要完成更多的shuffling数据。我已经在理论和实践中证实了这一点。不过我提议你的解决方案。我会试一试。 – hznut

0

没有财产做到这一点。您需要编写自己的输出格式&录音笔。

相关问题