统计每个年月下,温度最高的前两天

1.数据

2020-01-02 10:22:22  1c
2020-01-03 10:22:22 2c
2020-01-04 10:22:22 4c
2020-02-01 10:22:22 7c
2020-02-02 10:22:22 9c
2020-02-03 10:22:22 11c
2020-02-04 10:22:22 1c
2019-01-02 10:22:22 1c
2019-01-03 10:22:22 2c
2019-01-04 10:22:22 4c
2019-02-01 10:22:22 7c
2019-02-02 10:22:22 9c
2018-02-03 10:22:22 11c
2018-02-04 10:22:22 1c

2.需求分析

  • 按年月分组
  • 再按温度排序取前两个

3.Weather

import org.apache.hadoop.io.WritableComparable;import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;/*** bean*/
public class Weather implements WritableComparable<Weather> {private int year;private int month;private int day;private int degree;//温度@Override//按顺序读取public void readFields(DataInput dataInput) throws IOException {this.year = dataInput.readInt();this.month = dataInput.readInt();this.day = dataInput.readInt();this.degree = dataInput.readInt();}@Overridepublic void write(DataOutput dataOutput) throws IOException {dataOutput.writeInt(year);dataOutput.writeInt(month);dataOutput.writeInt(day);dataOutput.writeInt(degree);//java.lang.RuntimeException: java.io.EOFException ---序列化与反序列化不一致的问题}@Overridepublic int compareTo(Weather o) {int t1 = Integer.compare(this.year, o.getYear());if (t1 == 0) {int t2 = Integer.compare(this.month, o.getMonth());if (t2 == 0) {return -Integer.compare(this.degree, o.getDegree());}return t2;}return t1;}public void setYear(int year) {this.year = year;}public void setMonth(int month) {this.month = month;}public void setDay(int day) {this.day = day;}public void setDegree(int degree) {this.degree = degree;}public int getYear() {return year;}public int getMonth() {return month;}public int getDay() {return day;}public int getDegree() {return degree;}@Overridepublic String toString() {return "Weather{" +"year=" + year +", month=" + month +", day=" + day +", degree=" + degree +'}';}
}

4.WeatherMapper

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/*** 1.映射,每映射一行就到达分区*/
public class WeatherMapper extends Mapper<LongWritable, Text, Weather, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {//根据分割符分割String[] split = value.toString().trim().split("\t");if (split != null && split.length >= 2) {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Calendar calendar = Calendar.getInstance();try {Date date = format.parse(split[0]);calendar.setTime(date);Weather weather = new Weather();weather.setYear(calendar.get(Calendar.YEAR));weather.setMonth(calendar.get(Calendar.MONTH) + 1);//注意:月份从0开始计数的weather.setDay(calendar.get(Calendar.DAY_OF_MONTH));int degree = Integer.parseInt(split[1].substring(0, split[1].lastIndexOf("c")));weather.setDegree(degree);context.write(weather, new IntWritable(degree));} catch (ParseException e) {e.printStackTrace();}}}
}

5.WeatherPartition

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Partitioner;/*** 2.分区:按Weather.year的hash值来分区,使每个年份一个单独reduce,即每个年份一个单独的分区,有几个分区最终就有几个输出文件*/
public class WeatherPartition extends Partitioner<Weather, IntWritable> {@Overridepublic int getPartition(Weather weather, IntWritable intWritable, int numPartitions) {//numPartitions由job.setNumReduceTasks(3)决定//写一个算法计算hash,这个算法要满足业务需求,每一个键值都会调用此方法,所以该方法需要简洁System.out.println((weather.getYear() - 1929) % numPartitions);return (weather.getYear() - 1929) % numPartitions;}
}

6.WeatherGroup

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;/*** 3.分组:在分区中在进行分组,key中相同的年月被分到同一个组,默认相同的key分到同一个组* reduce前默认相同的key分到同一个组,但此时只需要比较年与月,显然需要重写分组方法* <p>* 不分组的话在同一个分区的数据一个一个的传入,达不到筛选前两个的效果* 分组的话,按照如下分组规则,<年月,温度>这样相同年月的为一组传入*/
public class WeatherGroup extends WritableComparator {public WeatherGroup() {super(Weather.class, true);}@Overridepublic int compare(WritableComparable a, WritableComparable b) {Weather weather1 = (Weather) a;Weather weather2 = (Weather) b;int c1 = Integer.compare(weather1.getYear(), weather2.getYear());if (c1 == 0) {int c2 = Integer.compare(weather1.getMonth(), weather2.getMonth());return c2;}return c1;}
}

7.WeatherReduce

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;/*** 4.规约*/
public class WeatherReduce extends Reducer<Weather, IntWritable, Text, NullWritable> {@Overrideprotected void reduce(Weather key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int count = 0;for (IntWritable i : values) {count++;if (count >= 3) {break;}String res = key.getYear() + "-" + key.getMonth() + "-" + key.getDay() + "\t" + i.get();context.write(new Text(res), NullWritable.get());}}
}

8.WeatherMain

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
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.IOException;/*** @program: Hadoop_MR* @description:* @author: 作者* @create: 2022-06-21 16:45*/
public class WeatherMain {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf = new Configuration();Job job = Job.getInstance();//创建job实例job.setJarByClass(WeatherMain.class);//1.mapjob.setMapperClass(WeatherMapper.class);//输出类型除了Text最好都设置一下,否则没有输出的job.setMapOutputKeyClass(Weather.class);job.setMapOutputValueClass(IntWritable.class);//2.分区job.setPartitionerClass(WeatherPartition.class);//设置reduce数目job.setNumReduceTasks(3);//output输出三个文件夹//3.排序//job.setSortComparatorClass(WeatherSort.class);//4.reduce内部分组job.setGroupingComparatorClass(WeatherGroup.class);//5.reducejob.setReducerClass(WeatherReduce.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(NullWritable.class);FileInputFormat.addInputPath(job, new Path("E:\\HadoopMRData\\input"));//输入目录Path outPath = new Path("E:\\HadoopMRData\\output");/*FileSystem fs = FileSystem.get(conf);if (fs.exists(outPath)) {fs.delete(outPath, true);}*/FileOutputFormat.setOutputPath(job, outPath);/*FileInputFormat.addInputPath(job, new Path(args[0]));//命令行运行时传入FileOutputFormat.setOutputPath(job, new Path(args[1]));*/System.exit(job.waitForCompletion(true) ? 0 : 1);//启动,0表示正常退出}
}

MapReduce项目案例3——温度统计相关推荐

  1. MapReduce项目案例4——乘用车辆和商用车辆销售数据分析

    项目介绍 1.数据概况 本数据为上牌汽车的销售数据,分为乘用车辆和商用车辆 数据包含销售相关数据与汽车具体参数 2.数据项包括 省0,月1,市2,区县3,年4,车辆型号5,制造商6,品牌7,车辆类型8 ...

  2. Hadoop学习笔记—20.网站日志分析项目案例(一)项目介绍

    Hadoop学习笔记-20.网站日志分析项目案例(一)项目介绍 网站日志分析项目案例(一)项目介绍:当前页面 网站日志分析项目案例(二)数据清洗:http://www.cnblogs.com/edis ...

  3. hadoop之mapreduce教程+案例学习(一)

    第1章 MapReduce概述 目录 第1章 MapReduce概述 1.1 MapReduce定义 MapReduce是一个分布式运算程序的编程框架,是用户开发"基于Hadoop的数据分析 ...

  4. 大数据项目开发案例_大数据分析技术——项目案例2(房价数据分析上)

    1 二手房房价分析简述 在现在这个社会,房子成为绝大多数人心中难以抹去的痛:不仅在于它的价格高不可攀,也在于我们多少有些囊中羞涩.若不是得益于亲朋好友相助.父母相帮,估计依靠着我们这点微薄的薪水去购房 ...

  5. 用两个使用Caffe的小项目案例演示迁移学习的实用性

    近年来随着深度学习的急剧升温,不管是学术界还是工业界都把大量资源投入了深度学习.作为一个普通的工程师或者程序员,也想对机器学习,尤其是深度学习有所了解,应当如何入手?最好的回答当然是"get ...

  6. 大数据项目开发案例_大数据分析技术——项目案例1(猫眼电影数据分析上)...

    壹 猫眼Top100电影数据分析概述 从这一节开始,我们就综合利用已学到的一些分析技术来尝试做一些比较复杂的实际数据分析项目.在这些实际的项目案例中,我们将会看到一个完整的数据分析流程:数据清理--数 ...

  7. 项目案例:qq数据库管理_2小时元项目:项目管理您的数据科学学习

    项目案例:qq数据库管理 Many of us are struggling to prioritize our learning as a working professional or aspir ...

  8. Hadoop学习笔记—20.网站日志分析项目案例(三)统计分析

    网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:http://www.cnbl ...

  9. linux中自动化日志分析,Shell项目案例7-应用日志分析

    2019年录制SHell新课地址 贴切企业脚本编写思路讲解,带你玩Shell脚本编程实战. 本套课程从实际项目案例出发,近100个Shell实例讲解,由浅入深,循序渐进,带你玩转Shell编程的方方面 ...

  10. vmware虚拟机上的centos安装Hadoop,以及在本地eclipse上运行mapReduce项目,并将文件输出到HDFS中...

    注意centos主机名不支持下划线,所以文章中若出现centos_02.com,请替换为 centos02.com vmware虚拟机上的centos安装Hadoop 安装vmware虚拟机 虚拟机上 ...

最新文章

  1. 4句话让你明白什么是AI
  2. P4127 [AHOI2009]同类分布
  3. java实验七输入输出流_实验六_Java的输入输出流
  4. 低秩矩阵表示(LRR)
  5. YOLT遥感图像检测算法详解
  6. win10 通过xrdp远程连接到ubuntu后,显示顶端快捷工具栏,显示最小化后的应用
  7. Mysql SQLyog 使用详解
  8. python列表元组字典集合实验心得_python心得 元组,字典,集合
  9. JSONP原理及使用
  10. 最新dex2jar下载,网上很多dex2jar是2015年的,有bug无法翻译部分代码,最新更新的dex2jar下载
  11. Android studio导入项目报错Please refer to the user guide chapter on the daemon at http://gradle.org/docs/2
  12. 温室大棚物联网系统方案
  13. 【网站】作为技术人可能要用到的IT技术网址清单,欢迎评论补充
  14. matlab设计高频滤波器
  15. 开源多云技术平台——Choerodon猪齿鱼发布0.24版本
  16. NGINX安装及操作笔记
  17. 被说了很多遍的设计模式---外观模式
  18. 听过闰年闰月,可你听过闰秒吗?
  19. C语言考试题库之填空题
  20. Presenting view controllers on detached view controllers is discouraged

热门文章

  1. c4d安装没有出现语言文字,关于C4D以及渲染器插件安装时遇到的问题以及解决方法...
  2. C#编程,使用 Roslyn引擎动态编译代码
  3. 强大的支持多文件上传的jQuery文件上传插件Uploadify
  4. r语言和python爬虫谁厉害_从事数据科学Python和R语言学哪个好?
  5. Ocr答题辅助神器 OcrAnswerer4.x,通过百度OCR识别手机文字,支持屏幕窗口截图和ADB安卓截图,支持四十个直播App,可保存题库...
  6. 傅里叶变换性质证明卷积_傅里叶变换2.系统属性和卷积公式的推导
  7. 清除Chrome浏览器的历史记录、缓存
  8. toshiba linux 打印机驱动的资料
  9. Android JNI开发笔记二:动态库和静态库
  10. python 通配符用法,python - 通配符