获取成绩表的最高记录

1.编写成绩表A.txt文本

A.txt:

语文 96
数学 102
英语 130
物理 19
化学 44
生物 44
语文 109
数学 118
英语 141
物理 72
化学 21
生物 7
语文 92
数学 103
英语 139
物理 20
化学 58
生物 12
语文 107
数学 112
英语 133
物理 88
化学 11
生物 22

2.编写FindMax.java代码

//mapper

public static class FindMaxMapper extends Mapper<LongWritable, Text,Text,IntWritable>{
        Text course = new Text();
        IntWritable score = new IntWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String [] values = value.toString().trim().split(" ");
            course.set(values[0]);
            score.set(Integer.parseInt(values[1]));
            context.write(course,score);

        }
    }

//reducer
    public static class FindMaxReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int maxScore = -1;
            Text course = new Text();
            for(IntWritable score:values){
                if (score.get()>maxScore){
                    maxScore = score.get();
                    course = key;
                }
            }
            context.write(course,new IntWritable(maxScore));
        }
    }

//driver
    public static void main(String [] args) throws Exception{
        if (args.length != 2){
            System.out.println("FindMax <input> <output>");
            System.exit(-1);
        }

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf,"findmax");
        job.setJarByClass(FindMax.class);
        job.setMapperClass(FindMaxMapper.class);
        job.setReducerClass(FindMaxReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setNumReduceTasks(1);
        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileSystem.get(conf).delete(new Path(args[1]),true);
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        System.out.println(job.waitForCompletion(true) ? 0 : 1);

    }

3.编译生成FindMax.jar包。
4.将FindMax.jar包和A.txt文本上传至Hadoop的/opt目录下。
5.(先打开集群)将hadoop中/opt目录下的A.txt文件上传到HDFS的"/user2/root/目录下。
        上传命令:
hdfs dfs -put /opt/A.txt /user2/root/
6.以hadoop jar提交任务给集群:
    命令:
 
hadoop jar FindMax.jar /user2/root/A.txt /user2/root/optput03
7.在HDFS中/user2/root/output03/part-r-00000下即可查看结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值