2013-05-08 121 views
0

在我的輸入文件中,我有一個列作爲國家。現在,我的任務是將某個國家的記錄放入與該國家名稱相同的單獨文件中。這可能在Map-reduce中做到。 請分享你的想法。是否可以爲map-reduce生成多個輸出文件?

+0

你試過 '三通' 克隆輸出流。 – Marichyasana 2013-05-08 10:50:03

+0

否@Marichyasana其實我不知道這個,你能詳細說明一下 – 2013-05-08 10:51:31

+0

你使用的是什麼版本的hadoop,並且你在尋找一箇舊的API('mapred')還是新的API('mapreduce')的解決方案? – 2013-05-08 11:00:32

回答

3

如果您使用的是新API,則應該查看MultipleOutputs類。這個班內有一個例子。作業提交

使用模式:

 



    Job job = new Job(); 

    FileInputFormat.setInputPath(job, inDir); 
    FileOutputFormat.setOutputPath(job, outDir); 

    job.setMapperClass(MOMap.class); 
    job.setReducerClass(MOReduce.class); 
    ... 

    // Defines additional single text based output 'text' for the job 
    MultipleOutputs.addNamedOutput(job, "text", TextOutputFormat.class, 
    LongWritable.class, Text.class); 

    // Defines additional sequence-file based output 'sequence' for the job 
    MultipleOutputs.addNamedOutput(job, "seq", 
     SequenceFileOutputFormat.class, 
     LongWritable.class, Text.class); 
    ... 

    job.waitForCompletion(true); 
    ... 

用法在減速:

 


    String generateFileName(K k, V v) { 
     return k.toString() + "_" + v.toString(); 
    } 

    public class MOReduce extends 
     Reducer { 
     private MultipleOutputs mos; 
     public void setup(Context context) { 
      ... 
       mos = new MultipleOutputs(context); 
      } 

     public void reduce(WritableComparable key, Iterator values, 
       Context context) 
       throws IOException { 
      ... 
    mos.write("text", , key, new Text("Hello")); 
    mos.write("seq", LongWritable(1), new Text("Bye"), "seq_a"); 
    mos.write("seq", LongWritable(2), key, new Text("Chau"), "seq_b"); 
    mos.write(key, new Text("value"), generateFileName(key, new Text("value"))); 
    ... 
     } 

    public void cleanup(Context) throws IOException { 
     mos.close(); 
    ... 
    } 
    } 

+4

小心解釋上面的代碼??每個人都可以訪問股票文檔。人們來這裏解釋。 – 2016-05-09 14:19:16

相關問題