当我在 Hadoop 上运行 MapReduce 时,结果非常令人困惑。这是代码(见下文)。正如您所看到的,这是一个非常简单的 MapReduce 操作。输入是 1 个目录,其中包含 100 个 .lineperdoc 文件(维基百科文章表示为 1 行)。我们在一行文本(映射)中找到单词对,然后在Reduce函数中,我们总结相同单词对键的计数,并在
sum>=500
时输出该对。问题出现在Reducer中:如果我有条件sum>=100
,当我将阈值增加到500
、1000
等时,我会得到不同的结果(请参阅下面代码后的图像和描述)。
import java.io.IOException;
import java.util.TreeMap;
import javax.naming.Context;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class HadoopWordPairsb2 extends Configured implements Tool {
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text pair = new Text();
private Text lastWord = new Text();
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] splitLine = value.toString().split(" ");
for (String w : splitLine) {
if (lastWord.getLength() > 0) {
// only consider words
if (w.matches("[a-zA-Z]+")) {
pair.set(lastWord + ":" + w);
context.write(pair, one);
}
}
lastWord.set(w);
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
Integer sum = 0;
for (IntWritable value : values)
sum += value.get();
// output only words that occured more than 500 times
if (sum >= 500) {
context.write(key, new IntWritable(sum));
}
}
}
@Override
public int run(String[] args) throws Exception {
Job job = Job.getInstance(new Configuration(), "HadoopWordPairsb2");
job.setJarByClass(HadoopWordPairsb2.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job, args[0]);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
return 0;
}
public static void main(String[] args) throws Exception {
int ret = ToolRunner.run(new Configuration(), new HadoopWordPairsb2(), args);
System.exit(ret);
}
}
在上图中,顶部的输出适用于条件
sum>=100
。紧随其后的输出是 sum>=500
。正如您可以清楚地看到的,结果不一致,我想知道为什么会出现这种情况。如果我错过了一些非常明显的东西,请告诉我,否则我不知道......谢谢!
upd1:将地图功能更改为:
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString().toLowerCase();
String[] tokens = line.split("[^a-z0-9_.]+");
for (String token : tokens) {
if (lastWord.getLength() > 0) {
pair.set(lastWord + ":" + token);
context.write(pair, one);
}
lastWord.set(token);
}
}
并得到这些结果(分别为
sum>=1000
和 sum>=500
)。还是不一致...
解决方案:通过删除组合器类,减速器可以正常工作。我猜有什么问题,但我不能说具体是什么。