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

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. 【转】分析.net中的object sender与EventArgs e

    源地址:http://blog.csdn.net/feihu19851111/article/details/7523118

  2. P4854 MloVtry的咸鱼树 状压+最短路

    $ \color{#0066ff}{ 题目描述 }$ 俗话说种瓜得瓜,种豆得豆,MloVtry把自己砍掉一半埋进了土里,于是它得到了一颗n个点的咸鱼树. 但是问题是由于MloVtry只舍得埋下一半的自 ...

  3. SDUT OJ 数据结构实验之串二:字符串匹配

    数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  4. Android蓝牙连接自动测试工具

    蓝牙连接自动测试工具 1.需求产生 开发不按着需求走都是耍流氓且浪费时间.此工具的需求产生是研发人员在开发产品时涉及到蓝牙驱动和安卓蓝牙两个东西.但是呢,蓝牙不太稳定,那么工作来了.就需要研发人员一边 ...

  5. 一款给力的一键复制js插件-clipboard.js

    一款没有依赖的.给力的一键复制的JS插件   点我前往github 案例demo见下载包内demo文件夹. 这里晒出最常用的几种方式,以供不时之需. <!DOCTYPE html> < ...

  6. C#调用存储过程的ADO.Net

    using System.Data.SqlClient; //如果存储过程没有输入和输出参数,而且不返回查询结果 SqlCommand cmd = new SqlCommand("存储过程名 ...

  7. ansible基本模块-cron

    ansible   XXX   -m   cron   -a   "name=‘XXX ’   job=‘执行的命令’   minute=XXX "                ...

  8. Qt 学习之路 2(56):使用模型操作数据库

    Qt 学习之路 2(56):使用模型操作数据库 (okgogo: skip) 豆子 2013年6月20日 Qt 学习之路 2 13条评论 前一章我们使用 SQL 语句完成了对数据库的常规操作,包括简单 ...

  9. SOAP XML报文解析

    import java.util.HashMap;import java.util.List;import java.util.Map; import org.dom4j.Document;impor ...

  10. 【原创】nginx入门

    一.简介 Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点( ...