1、题目描述:
某校统计了在校学生的性别和身高数据,现要求对这些数据进行处理以分别计算出男生身高的最大值与男生平均身高的差,女生身高的最小值与女生平均身高的差。
输入格式:数据保存在文件中,文件的每行由学生的序号、性别以及身高信息(单位为cm)组成。信息之间用逗号分隔。
1,F,170
2,M,178
3,M,174
4,F,165
输出: 请输出性别和对应差值,中间用制表符分隔。
F -2.5
M 2.0
请在 DSPPCode.mapreduce.student_info.impl 中创建 StudentInfoMapperImpl 和 StudentInfoReducerImpl, 分别继承 StudentInfoMapper 和 StudentInfoReudcer, 实现抽象方法。
计算平均值时使用double 类型变量进行计算。 输出结果的小数位数无需处理
2、代码
StudentInfoMapperImpl.java
package DSPPCode.mapreduce.student_info.impl;
import DSPPCode.mapreduce.student_info.question.StudentInfoMapper;
import org.antlr.v4.runtime.atn.ParseInfo;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
public class StudentInfoMapperImpl extends StudentInfoMapper {
@Override
public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String[] values=value.toString().split(",");
context.write(new Text(values[1]), new IntWritable(Integer.parseInt(values[2])));
}
}
StudentInfoReducerImpl.java
package DSPPCode.mapreduce.student_info.impl;
import DSPPCode.mapreduce.student_info.question.StudentInfoReducer;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class StudentInfoReducerImpl extends StudentInfoReducer {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, DoubleWritable>.Context context)
throws IOException, InterruptedException
{
double male_sum = 0, male_count = 0,female_sum = 0, female_count = 0, MIN = 100000, MAX = -100000;
if (key.toString().equals("M"))
{
for (IntWritable x : values)
{
double height = x.get();
male_sum += height;
male_count++;
if (height > MAX)
{
MAX = height;
}
}
}
else
{
for (IntWritable x : values) {
double height = x.get();
female_count++;
female_sum += height;
if (height < MIN)
{
MIN = height;
}
}
}
double male_avg = male_sum / male_count;
double female_avg = female_sum / female_count;
if (key.toString().equals("M"))
{
context.write(key, new DoubleWritable(MAX-male_avg));
}
else
{
context.write(key,new DoubleWritable(MIN - female_avg));
}
}
}