对下面一组气温数据进行处理,得到每个月份最高的两个气温值

2018-12-12 14:30 25c
2018-12-12 15:30 26c
2017-12-12 12:30 36c
2019-01-01 14:30 22c
2018-05-05 15:30 26c
2018-05-26 15:30 37c
2018-05-06 15:30 36c
2018-07-05 15:30 36c
2018-07-05 12:30 40c
2017-12-15 12:30 16c

输出格式如下:

2019-1 22
2018-12 26
2018-12 25
2018-7 40
2018-7 36
2018-5 37
2018-5 36
2017-12 36
2017-12 16

public class App {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration(true);
conf.set("fs.defaultFS","hdfs://hadoop01:9000");
     //windows下面运行添加一下两个配置
conf.set("mapreduce.app-submission.cross-platform","true");
conf.set("mapreduce.framework.name","local"); Job job = Job.getInstance(conf); //设置jobName
job.setJobName("myJob");
job.setJarByClass(App.class);
//配置map
//mapper类
job.setMapperClass(MyMapperClass.class);
//输出的key类型
job.setMapOutputKeyClass(TQ.class);
//输出的value类型
job.setMapOutputValueClass(IntWritable.class); //将输出的(K,V)=>(K,V,P)
//job.setPartitionerClass(MyPartitioner.class);
//数据在内存spill(溢写)之前先排序,注:继承WritableComparator
job.setSortComparatorClass(MySortComparator.class); //配置reduce
//根据需求确定分组的维度,继承自WritableComparator
job.setGroupingComparatorClass(MyGrouping.class);
//如map阶段根据年、月、温度三个维度排序,而reduce只根据年、月两个维度
job.setReducerClass(MyReduce.class); Path input=new Path("/input/weather.txt");
Path out=new Path("/output/weather");
if(out.getFileSystem(conf).exists(out)){
out.getFileSystem(conf).delete(out,true);
} //数据来源 HDFS路径
FileInputFormat.addInputPath(job,input);
//计算结果的输出目录
FileOutputFormat.setOutputPath(job,out);
//job.setNumReduceTasks(2); job.waitForCompletion(true);
}
}
public class TQ implements WritableComparable<TQ> {

    private int year;

    public int getYear() {
return year;
} public void setYear(int year) {
this.year = year;
} public int getMonth() {
return month;
} public void setMonth(int month) {
this.month = month;
} public int getDay() {
return day;
} public void setDay(int day) {
this.day = day;
} public int getTemp() {
return temp;
} public void setTemp(int temp) {
this.temp = temp;
} private int month;
private int day;
/**
温度
*/
private int temp; @Override
public int compareTo(TQ other) { int c1= Integer.compare(this.getYear(),other.getYear());
if(c1==){
return Integer.compare(this.getMonth(),other.getMonth());
}
return c1;
} @Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.year);
out.writeInt(this.month);
out.writeInt(this.day);
out.writeInt(this.temp);
} @Override
public void readFields(DataInput in) throws IOException {
this.year=in.readInt();
this.month=in.readInt();
this.day=in.readInt();
this.temp=in.readInt();
}
}
/**
* 根据年-月对map输出进行分组
*/
public class MyGrouping extends WritableComparator {
public MyGrouping(){
super(TQ.class,true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
TQ tq1 = (TQ) a;
TQ tq2 = (TQ) b;
if (tq1.getYear() == tq2.getYear() && tq1.getMonth() == tq2.getMonth()) {
return ;
}
return ;
}
}
public class MyMapperClass extends Mapper<LongWritable,Text,TQ, IntWritable> {
TQ tq=new TQ();
IntWritable outVal=new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[]splits= value.toString().split(" ");
String[]date=splits[].split("-");
tq.setYear(Integer.parseInt(date[]));
tq.setMonth(Integer.parseInt(date[]));
tq.setDay(Integer.parseInt(date[])); tq.setTemp(Integer.parseInt(splits[].replace("c","")));
outVal.set(tq.getTemp());
context.write(tq,outVal); }
}
public class MyReduce extends Reducer<TQ, IntWritable, Text,IntWritable> {
Text txtKey=new Text(); @Override
protected void reduce(TQ key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
Iterator<IntWritable> iterator = values.iterator();
int flag=;
while (iterator.hasNext()) {
if (flag == ) {
break;
}
txtKey.set(String.format("%s-%s",key.getYear(),key.getMonth())); IntWritable next = iterator.next();
context.write(txtKey,next);
flag++;
}
}
}
/**
数据在内存spill(溢写)之前先排序,根据年月温度
*/
public class MySortComparator extends WritableComparator {
public MySortComparator(){
super(TQ.class,true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
TQ tq1=(TQ)a;
TQ tq2=(TQ)b; int c1= Integer.compare(tq1.getYear(),tq2.getYear());
if(c1==){
int c2=Integer.compare(tq1.getMonth(),tq2.getMonth());
if (c2 == ) {
return -Integer.compare(tq1.getTemp(),tq2.getTemp());
}
return -c2;
}
return -c1;
}
}

hadoop 天气案例的更多相关文章

  1. hadoop经典案例

    hadoop经典案例http://blog.csdn.net/column/details/sparkhadoopdemo.html

  2. python + hadoop (案例)

    python如何链接hadoop,并且使用hadoop的资源,这篇文章介绍了一个简单的案例! 一.python的map/reduce代码 首先认为大家已经对haoop已经有了很多的了解,那么需要建立m ...

  3. Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况

    mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...

  4. jsonp全国天气案例

    案例1: 1.获取跨域数据 2.将数据按照下面的效果放到body里面     key: f49570d39b02b3c203526b5d8255aa61 079179afb105ce2bae9f5d0 ...

  5. Hadoop序列化案例实操

    需求 统计每一个手机号耗费的总上行流量.下行流量.总流量. 输入数据: 1 13736230513 192.196.100.1 www.atguigu.com 2481 24681 200 2 138 ...

  6. Hadoop经典案例(排序&Join&topk&小文件合并)

    ①自定义按某列排序,二次排序 writablecomparable中的compareto方法 ②topk a利用treemap,缺点:map中的key不允许重复:https://blog.csdn.n ...

  7. Hadoop ConnectException: Connection refused的一种解决办法

    跟着视频学习天气案例,把代码敲好,准备提交运行时才发现集群没启动.然后在node02.node03.node04使用zkServer.sh start启动ZooKeeper,然后在node01使用st ...

  8. about云资源汇总指引V1.4:包括hadoop,openstack,nosql,虚拟化

    hadoop资料 云端云计算2G基础课程 (Hadoop简介.安装与范例) 炼数成金3G视频分享下载 虚拟机三种网络模式该如何上网指导此为视频 Hadoop传智播客七天hadoop(3800元)视频, ...

  9. Hadoop基础概念介绍

    基于YARN的配置信息, 参见: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ hadoop入门 - 基础概念 ...

随机推荐

  1. NSRect 位置和尺寸

    前言 结构体 这个结构体用来表示事物的坐标点和宽高度. typedef CGRect NSRect; struct CGRect { CGPoint origin; CGSize size; }; t ...

  2. windows查看和杀死占用端口的进程

    1.首先使用 netstat -ano查看占用端口的进程号 2.然后使用 taskkill /PID (进程号)杀死进程

  3. 题解 P1614 【爱与愁的心痛】

    题目链接 前缀和. #重点在一个小小的常数优化 但是数据大了以后比楼下们跑的会快!!! 楼下用前缀和的题解都是跑了两遍循环. 而实际上一遍循环就可以呀. 就是加一段这个 if(i>=m) if( ...

  4. Pycharm关闭后Python.exe还是在后台运行

    pycharm运行程序关闭后会弹出一个对话框: 一定要选择第一个,不然python.exe一直会在后台运行.

  5. Scanner类的用法

    Scanner类,一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器. 用于扫描输入文本的实用程序.位于java.util包中. 使用Scanner接收键盘输入的字符,步骤: 1.导入Sca ...

  6. C语言数据结构-链式队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作

    1.数据结构-链式队列的实现-C语言 typedef struct QNode { int data; struct QNode *next; }QNode,*QueuePtr; typedef st ...

  7. ThinkCMF if 标签

    用法示例: <if condition="($name eq 1) OR ($name gt 100) "> value1 <elseif condition=& ...

  8. payload指安全测试数据

    payload,翻译过来是有效载荷 通常在传输数据时,为了使数据传输更可靠,要把原始数据分批传输,并且在每一批数据的头和尾都加上一定的辅助信息,比如数据量的大小.校验位等,这样就相当于给已经分批的原始 ...

  9. SQL数据库“单个用户”不能访问,设置为多个用户的解决方法

    USE master; GO DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@SQL+'; KILL '+RTRIM(SPID) FROM ma ...

  10. C - 思考使用差分简化区间操作

    FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positi ...