我是hadoop的新手。我已经完成了字数统计,现在我想做一个修改。
我想要获取文本文件中出现次数最多的单词。如果是,正常的字数统计程序给出输出:
a 1
b 4
c 2我想写一个只给我输出的程序
b 4下面是我的reducer函数::
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>
{
int max_sum=0;
Text max_occured_key;
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException
{
int sum = 0;
for (IntWritable val : values)
{
sum += val.get();
}
if(sum > max_sum)
{
max_sum = sum;
max_occured_key = key;
}
context.write(max_occured_key, new IntWritable(max_sum));
//context.write(key, new IntWritable(sum));
}}
但它没有提供正确的输出。有谁能帮上忙吗?
发布于 2013-01-14 20:17:14
到目前为止,您将在每个reduce函数的末尾写出最大值-因此您将在每个reduce函数中获得多个条目。在将键的引用复制到max_occured_key变量(而不是复制值)时,还会遇到引用重用问题。
您可能应该修改如下:
max_occured_key变量(为空文本)max_occured_key.set(key);而不是使用equals赋值-引用method进行修改,并将context.write调用移动到该方法-因此每个reducer只能获得一个K,V输出对。例如:
@Override
protected void cleanup(Context context) {
context.write(max_occured_key, new IntWritable(max_sum));
}一旦所有数据都通过map或reduce任务传递,就会调用cleanup方法(每个任务实例都会调用该方法(因此,如果您提供了10个reducers,则会为每个实例调用该方法)。
https://stackoverflow.com/questions/14317738
复制相似问题