[英]Hadoop Multiple Outputs
我寫了一些hadoop代碼來讀取映射文件並將其拆分成塊並將其寫入許多文件,如下所示:
public void map(LongWritable key, Text value, OutputCollector<IntWritable, Text>
output,Reporter reporter) throws IOException {
String line = value.toString();
int totalLines = 2000;
int lines = 0;
int fileNum = 1;
String[] linesinfile = line.split("\n");
while(lines<linesinfile.length) {
// I do something like, if lines = totalLines, {
output.collect(new IntWritable(fileNum), new
Text(linesinfile[lines].toString()));
fileNum++;
lines = 0;
}
lines++;
}
}
在減少,我做:
public void reduce(IntWritable key, Iterator<Text> values,
OutputCollector<IntWritable, Text> output, Reporter reporter) throws IOException {
while(values.hasNext()){
output.collect(key, values.next());
}
}
我的MultiFile類如下:
public class MultiFileOutput extends MultipleTextOutputFormat<IntWritable, Text> {
protected String generateFileNameForKeyValue(IntWritable key, Text content, String
fileName) {
return key.toString() + "-" + fileName;
}
}
主要是,我說:
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(MultiFileOutput.class);
除了設置OutKey / Value Class等。
我究竟做錯了什么 ? 我的輸出目錄總是空的。
謝謝
該計划看起來有點復雜。 如果目的是將文件拆分成多個文件,那么可以通過幾種方式完成。 沒有必要進行Map和Reduce工作,只需一個Map工作就足夠了。
使用oahmapred.lib.NLineInputFormat從輸入中一次讀取N行到映射器,然后將這N行寫入文件。
在上傳文件時將dfs.blocksize設置為所需的文件大小,然后每個映射器將處理一個可寫入文件的InputSplit。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.