gpt4 book ai didi

hadoop - MapReduce不会减少?

转载 作者:行者123 更新时间:2023-12-02 21:39:58 24 4
gpt4 key购买 nike

我正在http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html上学习教程,这是我的代码

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Iterator;

public class WordCount {
public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable> {

private Text word = new Text();
private final IntWritable one = new IntWritable(1);

@Override
public void map(Object key, Text val, Context context) throws IOException, InterruptedException {
String line = val.toString();
StringTokenizer tokenizer = new StringTokenizer(line.toLowerCase());
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}

public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

public void reduce(Text key, Iterator<IntWritable> value, Context context) throws IOException, InterruptedException {
int sum = 0;
while (value.hasNext()) {
IntWritable val = (IntWritable) value.next();
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
Configuration config = new Configuration();
Job job = Job.getInstance(config, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setCombinerClass(WordCountReducer.class);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path("/user/Icarus/words.txt"));
FileOutputFormat.setOutputPath(job, new Path("/user/Icarus/words.out"));
job.waitForCompletion(true);
}
}

但是当我运行它而不是计算单词频率时,我得到了:
bye 1
goodbye 1
hadoop 1
hadoop 1
hello 1
hello 1
hello 1
world 1

我必须错过一些非常琐碎的事情,但我不知道该怎么办。请帮忙..

最佳答案

造成此问题的根本原因是,您没有使用reduce()调用所需的确切Signature来调用Hadoop。签名应如下所示(引用here)

protected void reduce(KEYIN key, Iterable<VALUEIN> values, org.apache.hadoop.mapreduce.Reducer.Context context)
throws IOException, InterruptedException

由于您的 reduce()Signature不匹配,因此 Hadoop将调用默认的 IdentityReducer,该输出将输出相同的输入。
因此,只有您得到的Map输出与Reduce输出相同。

对于这个问题,我可以建议您2个解决方案,
首先:尝试以下代码
public static class WordCountReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}

第二个:第二个解决方案非常简单,
无需您手动定义reduce类,只需将Reducer类设置为 IntSumReducerLongSumReducer即可,它们的功能与上述代码相同。
因此,请勿定义 WordCountReducer类并添加以下代码,
job.setReducerClass(LongSumReducer.class); or  
job.setReducerClass(IntSumReducer.class);

根据您想要的计数类型。

希望能帮助到你!

关于hadoop - MapReduce不会减少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29504469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com