MapReduce Student Info

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));
    }
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值