mapreduce学习笔记2

您所在的位置:网站首页 考研分数最高纪录排名 mapreduce学习笔记2

mapreduce学习笔记2

2024-07-14 17:25:52| 来源: 网络整理| 查看: 265

文章目录 需求说明数据准备实现思路编写代码集群运行实现结果

需求说明

有一个样例文件 subject_score,即成绩表 A。文件中的每一行数据包含两个字段:科目和分数。要求获取成绩列表中每个科目成绩最高的记录,并将结果输出到最高成绩表 B。

数据准备 语文 96 数学 102 英语 130 物理 19 化学 44 生物 44 语文 109 数学 118 英语 141 物理 72 化学 21 生物 7

完整数据:https://download.csdn.net/download/weixin_44018458/14953973

实现思路 在 Mapper 类中,map 函数读取成绩表 A 中的数据,直接将读取的数据以空格分隔,组成键值对,即设置输出键值对类型为。在 Reduce 中,由于 map 函数输出键值对类型是,所以 Reducer 接收的键值对是。针对相同的键(即科目),遍历比较它的值(即成绩),找出最高值(即最高成绩),最后输出键值对。 编写代码

FindMax.java

package test; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; 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; import java.io.IOException; public class FindMax { 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); } } 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)); } } 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); } } 集群运行 将 MapReduce 程序提交给 Yarn 集群,分发到很多的节点上并发执行处理的数据和输出结果应该位于 HDFS 文件系统提交集群的实现步骤:装程序打包成 JAR 包,并上传,然后在集群上用 hadoop 命令启动hadoop jar findMax-1.0-SNAPSHOT.jar test.FindMax findmax findmax_out 实现结果

在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭