首先,我是java的初学者,但我必须尽快完成mapreduce任务。
我试图修改wordcount算法,因为问题非常相似。
我的输入是一个文本文件,其中包含如下数据列:
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:02User:000 0
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:02User:000 0
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:02User:000 0
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:02User:000 1
Date:2008-10-23Hour:03User:000 0
Date:2008-10-23Hour:03User:000 1
Date:2008-10-23Hour:03User:000 1
Date:2008-10-23Hour:03User:000 0
Date:2008-10-23Hour:03User:000 1
Date:2008-10-23Hour:03User:000 1
Date:2008-10-23Hour:03User:000 0
Date:2008-10-23Hour:04User:000 1
Date:2008-10-23Hour:04User:000 0
Date:2008-10-23Hour:04User:000 1
Date:2008-10-23Hour:04User:000 1
Date:2008-10-23Hour:04User:000 1
Date:2008-10-23Hour:04User:000 1
Date:2008-10-23Hour:04User:000 0
Date:2008-10-23Hour:04User:000 1
Date:2008-10-23Hour:04User:000 0
Date:2008-10-23Hour:04User:000 1
mapreduce作业必须将每行的第一个字符串设置为my key(date:2008-10-23hour:03user:001)数字1或0就是这个值。reducer的任务只是对同一个键的值(1+1+0+1+0…)求和…仅此而已。问题是,在结果中,我得到了巨大的数字(太大)像最终值,但我不知道绝对的原因。
算法如下:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MapReduce {
public static class KeyValueMapper
extends Mapper<Object, Object , Text, IntWritable>{
private IntWritable ValueDistanceFunction = new IntWritable();
private Text DateHourUser = new Text();
public void map(Object key, Object value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
DateHourUser.set(read.nextToken());
ValueDistanceFunction.set(Integer.parseInt(read.nextToken()));
context.write(DateHourUser,ValueDistanceFunction);
// I print the results only to check them
System.out.println(DateHourUser);
System.out.println(ValueDistanceFunction);
}
}
}
public static class IntSumReducer
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();
System.out.println(sum);
}
result.set(sum);
context.write(key,result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "KeyValue");
job.setJarByClass(MapReduce.class);
job.setMapperClass(KeyValueMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path("/home/ubuntu/workspace/FileGeneration/Input"));
FileOutputFormat.setOutputPath(job, new Path("/home/ubuntu/workspace/FileGeneration/Output"));
System.exit(job.waitForCompletion(true) ? 0:1);
}
}
而这些恰恰是错误的输出:
date:2008-10-23hour:02user:000 16
date:2008-10-23hour:03user:000 6
date:2008-10-23hour:04user:000 14
正确的输出应该是:
date:2008-10-23hour:02user:000 8
date:2008-10-23hour:03user:000 3
date:2008-10-23hour:04user:000 7
错误的结果正好是正确结果的两倍
此外,如果我在计算过程中用值(0或1)打印sum和key,我会得到:
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
0
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
0
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
0
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:03User:000
0
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
0
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
0
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
0
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
0
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
0
Date:2008-10-23Hour:04User:000
1
8 (this is correct)
3 (this is correct)
7 (this is correct)
Date:2008-10-23Hour:02User:000
0
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
0
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
0
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:02User:000
1
Date:2008-10-23Hour:03User:000
0
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
0
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
1
Date:2008-10-23Hour:03User:000
0
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
0
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
0
Date:2008-10-23Hour:04User:000
1
Date:2008-10-23Hour:04User:000
0
Date:2008-10-23Hour:04User:000
1
8
3
7
16 (wrong final value)
6 (wrong final value)
14 (wrong final value)
提前多谢了。
1条答案
按热度按时间2nbm6dog1#
问题在于Map程序代码。为什么要在Map器中读取输入?
以下行有问题:
您已经在driver类中指定了输入。
无需在Map器中再次读取此输入。框架读取这个文件并将每一行传递给Map器。该行包含在
value
.Map器代码应如下所示:
编辑:我拿了你的数据运行了程序。我得到了以下结果。我认为数据或代码都没有问题:
这个程序没有问题。