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

获取成绩表的最高记录

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

获取成绩表的最高记录

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{
        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{
        @Override
        protected void reduce(Text key, Iterable 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 ");
            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下即可查看结果

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

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

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