2013-10-11 47 views
6

我想從我的減速器輸出分離到不同的文件夾..寫入hadoop中的多個文件夾?

My dirver has the following code: 
FileOutputFormat.setOutputPath(job, new Path(output)); 
      //MultipleOutputs.addNamedOutput(job, namedOutput, outputFormatClass, keyClass, valueClass) 
      //MultipleOutputs.addNamedOutput(job, namedOutput, outputFormatClass, keyClass, valueClass) 
      MultipleOutputs.addNamedOutput(job, "foo", TextOutputFormat.class, NullWritable.class, Text.class); 
      MultipleOutputs.addNamedOutput(job, "bar", TextOutputFormat.class, Text.class,NullWritable.class); 
      MultipleOutputs.addNamedOutput(job, "foobar", TextOutputFormat.class, Text.class, NullWritable.class); 

And then my reducer has the following code: 
mos.write("foo",NullWritable.get(),new Text(jsn.toString())); 
mos.write("bar", key,NullWritable.get()); 
mos.write("foobar", key,NullWritable.get()); 

But in the output, I see: 

output/foo-r-0001 
output/foo-r-0002 
output/foobar-r-0001 
output/bar-r-0001 


But what I am trying is : 

output/foo/part-r-0001 
output/foo/part-r-0002 
output/bar/part-r-0001 

輸出/ foobar的/部分-R-0001

我該怎麼辦呢? 感謝

+1

什麼版本的Hadoop是這樣的? –

回答

4

如果你的意思是這個MultipleOutputs,最簡單的方法是做從你減速機下列之一 -

  1. 使用命名輸出與基地輸出路徑。 See this function
  2. 沒有命名的輸出,並且僅使用基本輸出路徑,See this function

在你的情況,這是第1點,所以,請更改以下 -

mos.write("foo",NullWritable.get(),new Text(jsn.toString())); 
mos.write("bar", key,NullWritable.get()); 
mos.write("foobar", key,NullWritable.get()); 

到,

mos.write("foo",NullWritable.get(),new Text(jsn.toString()), "foo/part"); 
mos.write("bar", key,NullWritable.get(), "bar/part"); 
mos.write("foobar", key,NullWritable.get(), "foobar/part"); 

其中,「foo/part」,「bar/part」 and 「foobar/part」對應於baseOutputPath。 因此,將創建目錄foo,bar和foobar,並在其中創建part-r-xxxxx文件。

你也可以嘗試上面的第2點,它實際上不需要任何命名輸出。

如果需要,請回復我以獲得進一步說明。