Mapreduce 统计各科成绩最高分

我的真正意义上 第一个 Mapreduce程序

首先,自己接触mapreduce的时间也不长。是 个真正意义上 的小白。今天通过自己钻研。终于写出了自己的第一个mapreduce程序。心里还是有点小激动的,话不多说,附上源代码

package com.sxzy;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    import java.io.File;
import java.io.IOException;

public class TextGrade {
    public static class Mymap extends Mapper<Object, Text,Text,IntWritable>{
        public void map(Object key,Text value,Context context)throws IOException, InterruptedException{
            String line = value.toString();
            String list[] = line.split("\t");
            String subj = list[0];
            int grade =Integer.parseInt(list[1]);//强制类型转换
            context.write(new Text(subj),new IntWritable(grade));
        }
    }
    public static class MyReduc extends Reducer<Text,IntWritable,Text,IntWritable>{
        public void reduce(Text key,Iterable<IntWritable> values,Context context)throws IOException,InterruptedException{
            int i  = 0;
            for (IntWritable val:values){
                if(i<val.get()){
                    i = val.get();
                }
            }
            context.write(key, new IntWritable(i));
        }
    }
    public static void main(String[] args)throws Exception{
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(TextGrade.class);
        job.setMapperClass(Mymap.class);
        job.setReducerClass(MyReduc.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        File file1 = new File("E:\\data\\grade");
        if (file1.exists()){
            FileUtils.deleteDirectory(file1);
        }
        FileInputFormat.setInputPaths(job,new Path("E:\\data\\grade.txt"));
        FileOutputFormat.setOutputPath(job,new Path("E:\\data\\grade"));
        Boolean b =job.waitForCompletion(true);
        System.out.println(b?0:1);


    }
}

首先的话我想说 的是map输入的键值对必须对应起来。不然的话就出不来结果。
另外我 现在也搞不懂类型具体该怎么用,说来有些惭愧。


版权声明:本文为jws123123123原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。