Mapreduce--------学生成绩(1)

数据
computer,huangxiaoming,85
computer,xuzheng,54
computer,huangbo,86
computer,liutao,85
computer,huanglei,99
computer,liujialing,85
computer,liuyifei,75
computer,huangdatou,48
computer,huangjiaju,88
computer,huangzitao,85
english,zhaobenshan,57
english,liuyifei,85
english,liuyifei,76
english,huangdatou,48
english,zhouqi,85
english,huangbo,85
english,huangxiaoming,96
english,huanglei,85
english,liujialing,75
algorithm,liuyifei,75
algorithm,huanglei,76
algorithm,huangjiaju,85
algorithm,liutao,85
algorithm,huangdou,42
algorithm,huangzitao,81
math,wangbaoqiang,85
math,huanglei,76
math,huangjiaju,85
math,liutao,48
math,xuzheng,54
math,huangxiaoming,85
math,liujialing,85
需求
求每门课程的最高分,最低分及平均分
代码

**package com.mr2;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class CouseOne {
    public static class MyMapper extends Mapper
    {
    	protected void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException
    	{
    		//数据样式:computer,huangxiaoming,85
    		String s = value.toString();
    		String[] split =s.split(",");
    		//将分数由字符串类型转化为int类型
    		int m = Integer.parseInt(split[2]);
    		//将课程名作为key,每个人该课程分数作为value
    		context.write(new Text(split[0]),new IntWritable(m));
    	}
    }
    public static class MyReduce extends Reducer
    {
    	Text valueout = new Text();  
    	int sum = 0;
        int max = 0;
        //定义最低分为最大整数
        int min = (int) (Math.pow(2, 31)-1);
        //定义平均分为double类型
        double avg = 0;
        int count = 0 ;
    	protected void reduce(Text k2,Iterablev2,Context context) throws IOException,InterruptedException
    	{
    	 for(IntWritable v : v2)
    	 {
    		 //用get()方法取出v对象中的值
    		 int m = v.get();
    		 sum = sum + m;
    		 if(m>max)
    			 max=m;
    		 if(m

结果:
Mapreduce--------学生成绩(1)_第1张图片
说明
为了练习在这里借用了“中琦2513“的数据和题目需求

你可能感兴趣的:(MapReduce,Mapreduce练习)