2014-03-13 32 views
0

我試圖從reducer寫入不同的文件(鍵和值),但我只有一個鍵和值的輸出文件。MultipleOutputs map reduce不起作用

public static class Reduce 
    extends Reducer<Text,Text,Text,Text> { 
    private MultipleOutputs <Text,Text>mos; 
@Override 
protected void setup(Context context) throws IOException,   InterruptedException {  
    super.setup(context); 
} 
public void reduce(Text key, Text values, Context context) throws IOException, InterruptedException {  
    mos.write(key.toString(),values, key); } 

@Override 
    protected void cleanup(Context context) throws IOException, 
      InterruptedException { 
     super.cleanup(context); 
    } 
}// end Reducer 

//驅動程序:它包含多個切口

job.setMapperClass(Map.class); 
    job.setReducerClass(Reduce.class); 
    job.setCombinerClass(Reduce.class); 
    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 
    // set output key type 
    job.setOutputKeyClass(Text.class); 
    // set output value type 
    job.setOutputValueClass(Text.class); 
    //set the HDFS path of the input data 
    FileInputFormat.addInputPath(job, new Path(otherArgs[0])); 
    // set the HDFS path for the output 
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); 
    //multiple output files 
    MultipleOutputs.addNamedOutput(job, "express", TextOutputFormat.class, Text.class, Text.class); 
+0

什麼是生成的輸出文件的名稱? –

回答

0

MultipleOutputs類的write方法具有三個簽名,如下所示。

write(KEYOUT key, VALUEOUT value, String baseOutputPath) 
      Write key value to an output file name. 

write(String namedOutput, K key, V value) 
      Write key and value to the namedOutput. 

write(String namedOutput, K key, V value, String baseOutputPath) 
      Write key and value to baseOutputPath using the namedOutput 

看來您正在使用第二個,但更改了鍵和值參數。此外,如果您傳遞整個值Iterable,我不知道此方法的行爲。

作爲一個建議,你可以使用第三個簽名,這是更簡單的,因爲你不需要撥打addNamedOutput,只能寫入任何baseOutputPath

例如:

mos.write(key, value, "express");