【发布时间】:2013-12-18 02:40:28
【问题描述】:
我正在使用WordCount 示例,在Reduce 函数中,我需要获取文件名。
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
String filename = ((FileSplit)(.getContext()).getInputSplit()).getPath().getName();
// ----------------------------^ I need to get the context and filename!
key.set(key.toString() + " (" + filename + ")");
output.collect(key, new IntWritable(sum));
}
}
这是目前上面修改过的代码,我想在其中获取要为单词打印的文件名。我尝试关注Java Hadoop: How can I create mappers that take as input files and give an output which is the number of lines in each file?,但无法获取context 对象。
我是 hadoop 新手,需要帮助。有帮助吗?
【问题讨论】: