我具有以下格式的特定班级学生分数的数据
StudentId@CourseId@Marks1@Marks2@Marks3@Marks4@Marks5
01001@104@13@18@25@86@23
01001@106@58@30@10@12@59
01001@108@60@81@97@83@26
01001@110@68@95@11@73@63
01001@112@55@22@74@71@22
01002@104@20@72@76@28@99
01002@106@52@17@20@67@91
01002@108@18@46@61@73@14
01002@110@86@59@50@35@65
01002@112@45@76@97@37@17
.......
我想计算学生在5种不同的考试中5种不同的课程(给出5种不同的课程ID)中获得的平均分数.
上面的“我的地图”和“简化类”如下:
public static class Map extends MapReduceBase implements Mapper<LongWritable,Text,Text,DoubleWritable>{
private Text SID=new Text();
public void map(LongWritable key, Text value, OutputCollector<Text,DoubleWritable> output,Reporter reporter)throws IOException{
String data=value.toString();
String arr[]=data.split("@");
int i=2;
double score=0;
while(i<arr.length){
score+=Integer.parseInt(arr[i]);
i++;
}
//Dividing The score to give the average score in a particular course
score=score/5;
SID.set(arr[0]);
output.collect(SID,new DoubleWritable(score));
}
}
和
public static class Reduce extends MapReduceBase implements Reducer<Text,DoubleWritable,Text,DoubleWritable>{
public void reduce(Text key,Iterator<DoubleWritable> values,OutputCollector<Text,DoubleWritable> output,Reporter reporter)throws IOException{
double Total=0.0;
while(values.hasNext()){
Total+=values.next().get();
}
//Dividing By 5 to obtain the average score for a particular student
output.collect(key,new DoubleWritable((Total/5)));
}
}
此外,在主类中,除了定义其他配置之外,我还将上述Reduce类设置为Reducer和Combiner类.
但是我获得的输出如下
01001 9.879999999999999
01002 10.568
01003 8.712
01004 10.68
01005 9.335999999999999
....
这表明学生的总分数除以125而不是25.
但是,在从Map类中删除语句score = score / 5时,
我得到正确的结果.现在,根据我的理解(并不太确定),这是因为在这种情况下,Reducer和Combiner类是相同的.是这样吗?
在这种情况下,Combiner类如何工作?
解决方法:
执行映射后,将在每个节点上执行组合器.
但是只需要在求和的最后除以5(不同课程的数量)即可.因此,您只能在减速器的末端进行分割,而不能在组合器的末端进行分割.
基本上,您可以:
>拆下组合器(但保留减速器)
>定义一个与减速器完全相同但末尾不除法的减速器
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。