2012-05-03 54 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

沒有財產做到這一點。您需要編寫自己的輸出格式&錄音筆。

相關問題