栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MapReduce Student Info

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.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 values, Reducer.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));
    }
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992823.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号