【Hadoop学习之九】MapReduce案例分析一-天气
环境
虚拟机:VMware 10
Linux版本:CentOS-6.5-x86_64
客户端:Xshell4
FTP:Xftp4
jdk8
hadoop-3.1.1
找出每个月气温最高的2天
1949-10-01 14:21:02 34c
1949-10-01 19:21:02 38c
1949-10-02 14:01:02 36c
1950-01-01 11:21:02 32c
1950-10-01 12:21:02 37c
1951-12-01 12:21:02 23c
1950-10-02 12:21:02 41c
1950-10-03 12:21:02 27c
1951-07-01 12:21:02 45c
1951-07-02 12:21:02 46c
1951-07-03 12:21:03 47c
package test.mr.tq; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* @author Administrator
* 客户端
*/
public class MyTQ { /**
* 找出每个月气温最高的2天 * @param args
*/
public static void main(String[] args) {
//加载配置文件
Configuration conf = new Configuration(); try {
//创建客户端
Job job = Job.getInstance(conf,"tian qi");
job.setJarByClass(MyTQ.class); //Map
job.setMapperClass(TQMapper.class);
job.setOutputKeyClass(TQ.class);
job.setOutputValueClass(IntWritable.class);
//分区类 处理大数据量均衡并发处理
job.setPartitionerClass(TqPartitioner.class);
//用于buffer字节数组内的key排序的比较类 温度最高的2天 需要排序
job.setSortComparatorClass(TqSortComparator.class); //Reduce
job.setReducerClass(TqReducer.class);
job.setNumReduceTasks(2);
//用于分组的比较类 年月相同的被视为一组
job.setGroupingComparatorClass(TqGroupingComparator.class); //输入 输出
Path input = new Path("/root/input");
FileInputFormat.addInputPath(job, input);
Path output = new Path("/root/output");
if (output.getFileSystem(conf).exists(output))
{
output.getFileSystem(conf).delete(output, true);
}
FileOutputFormat.setOutputPath(job, output); //提交
System.exit(job.waitForCompletion(true) ? 0 : 1); } catch (Exception e) {
e.printStackTrace();
} } }
package test.mr.tq; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.WritableComparable; public class TQ implements WritableComparable<TQ>{ private int year;
private int month;
private int day;
private int wd;
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 getWd() {
return wd;
}
public void setWd(int wd) {
this.wd = wd;
} /**
* 反序列化进来
*/
@Override
public void readFields(DataInput in) throws IOException {
this.year = in.readInt();
this.month = in.readInt();
this.day = in.readInt();
this.wd = in.readInt();
} /**
* 序列化出去
*/
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(year);
out.writeInt(month);
out.writeInt(day);
out.writeInt(wd);
} @Override
public int compareTo(TQ that) {
//时间正序
int y = Integer.compare(this.year, that.getYear());
if (y == 0)
{
int m = Integer.compare(this.month, that.getMonth());
if (m == 0)
{
return Integer.compare(this.day, that.getDay());
}
return m;
}
return y;
} }
package test.mr.tq; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; public class TqGroupingComparator extends WritableComparator { public TqGroupingComparator()
{
super(TQ.class,true);
} /**
* 面向reduce 按照年月分组
* 年月不相同 就不属于同一组
* 返回0表示同一组
*/
@Override
public int compare(WritableComparable a, WritableComparable b) {
TQ t1 = (TQ)a;
TQ t2 = (TQ)b;
int y = Integer.compare(t1.getYear(), t2.getYear());
if (y==0)
{
return Integer.compare(t1.getMonth(), t2.getMonth());
}
return y;
}
}
package test.mr.tq; import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class TQMapper extends Mapper<LongWritable, Text, TQ, IntWritable> {
//k:v映射的设计
// K V
// 1949-10-01 14:21:02 34c
// 1949-10-01 19:21:02 38c
// 1949-10-02 14:01:02 36c
// 1950-01-01 11:21:02 32c
// 1950-10-01 12:21:02 37c
// 1951-12-01 12:21:02 23c
// 1950-10-02 12:21:02 41c
// 1950-10-03 12:21:02 27c
// 1951-07-01 12:21:02 45c
// 1951-07-02 12:21:02 46c
// 1951-07-03 12:21:03 47c TQ tq = new TQ();
IntWritable vwd = new IntWritable(); @Override
protected void map(LongWritable key, Text value,
Context context) throws IOException, InterruptedException
{
try
{
//1951-07-03 12:21:03 47c
String[] strs = StringUtils.split(value.toString(),"\t");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(strs[0]); Calendar cal = Calendar.getInstance();
cal.setTime(date); //key
tq.setYear(cal.get(Calendar.YEAR));
tq.setMonth(cal.get(Calendar.MONTH)+1);
tq.setDay(cal.get(Calendar.DAY_OF_MONTH));
int wd = Integer.parseInt(strs[1].substring(0, strs[1].length()-1));
tq.setWd(wd); //value
vwd.set(wd); //输出
context.write(tq, vwd);
}
catch (ParseException e)
{
e.printStackTrace();
} } }
package test.mr.tq; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Partitioner; /**
* @author wjy
* K.V==>K.V.P
* 分区规则设计 尽量使数据分区均衡 避免倾斜
*/
public class TqPartitioner extends Partitioner<TQ, IntWritable> { @Override
public int getPartition(TQ key, IntWritable value, int numPartitions) { return key.getYear() % numPartitions;
} }
package test.mr.tq; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class TqReducer extends Reducer<TQ, IntWritable, Text, IntWritable> { Text rkey = new Text();
IntWritable rval = new IntWritable(); @Override
protected void reduce(TQ key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException
{
//相同的key为一组
// 时间正序 温度倒序
// 1970 01 01 40
// 1970 01 02 38
//迭代values key会随着变化
int flg = 0;
int day = 0;
for (IntWritable wd : values) {
if (flg == 0)
{
day = key.getDay();
rkey.set(key.getYear()+"-"+key.getMonth()+"-"+key.getDay());
rval.set(key.getWd());//wd.get()
context.write(rkey, rval);
flg ++;
} if (flg != 0 && day != key.getDay())
{
rkey.set(key.getYear()+"-"+key.getMonth()+"-"+key.getDay());
rval.set(key.getWd());//wd.get()
context.write(rkey, rval);
break;
}
}
}
}
package test.mr.tq; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; public class TqSortComparator extends WritableComparator { //对字节数据中map进行排序 所以需要先将Key反序列化为对象 然后再进行比较
public TqSortComparator()
{
super(TQ.class,true);
} /**
* 按照时间正序 温度倒序对字节数组排序
*/
@Override
public int compare(WritableComparable a, WritableComparable b) {
TQ t1 = (TQ)a;
TQ t2 = (TQ)b;
int y = Integer.compare(t1.getYear(), t2.getYear());
if (y==0)
{
int m = Integer.compare(t1.getMonth(), t2.getMonth());
if (m == 0)
{
//前面加一个负号 就可以实现倒序的效果
return - Integer.compare(t1.getWd(), t2.getWd());
}
return m;
}
return y;
} }
【Hadoop学习之九】MapReduce案例分析一-天气的更多相关文章
- 【Hadoop学习之十二】MapReduce案例分析四-TF-IDF
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 概念TF-IDF(term fre ...
- 【Hadoop学习之十三】MapReduce案例分析五-ItemCF
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 推荐系统——协同过滤(Collab ...
- 【Hadoop学习之十】MapReduce案例分析二-好友推荐
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 最应该推荐的好友TopN,如何排名 ...
- 【Hadoop学习之十一】MapReduce案例分析三-PageRank
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 什么是pagerank?算法原理- ...
- Hadoop学习笔记—20.网站日志分析项目案例(一)项目介绍
网站日志分析项目案例(一)项目介绍:当前页面 网站日志分析项目案例(二)数据清洗:http://www.cnblogs.com/edisonchou/p/4458219.html 网站日志分析项目案例 ...
- Hadoop学习笔记—20.网站日志分析项目案例(二)数据清洗
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:当前页面 网站日志分析项目案例 ...
- Hadoop学习笔记—20.网站日志分析项目案例
1.1 项目来源 本次要实践的数据日志来源于国内某技术学习论坛,该论坛由某培训机构主办,汇聚了众多技术学习者,每天都有人发帖.回帖,如图1所示. 图1 项目来源网站-技术学习论坛 本次实践的目的就在于 ...
- Hadoop学习笔记—20.网站日志分析项目案例(三)统计分析
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:http://www.cnbl ...
- Hadoop学习笔记—12.MapReduce中的常见算法
一.MapReduce中有哪些常见算法 (1)经典之王:单词计数 这个是MapReduce的经典案例,经典的不能再经典了! (2)数据去重 "数据去重"主要是为了掌握和利用并行化思 ...
随机推荐
- Java如何编写Servlet程序
一:Servlet Servlet是Java服务器端编程,不同于一般的Java应用程序,Servlet程序是运行在服务器上的,服务器有很多种,Tomcat只是其中一种. 例子: 在Eclipse中新建 ...
- c语言指针应用
指针变量指向数组元素: #import <stdio.h> int main() { int a[10]={1,2,3,4,5,6,7,8,9,0}; int *p; p=a; for ( ...
- MySQL数据库查询操作进阶——多表查询
多表查询 在大部分情况下,我们用到的表都是彼此相关联的,所以我们会有相当大的需求用到跨表的查询,这个时候我们就需要将相关联的表连起来做多表查询. 多表查询分为连表查询和子查询,连表查询即将相关联的表连 ...
- Python3学习之路~3.2 递归、函数式编程、高阶函数、匿名函数、嵌套函数
1 递归 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. def calc(n): print(n) if int(n / 2) == 0: return n r ...
- php 代码复用机制
https://juejin.im/entry/5927ec4544d904006413f61d 提到 php 的代码复用,我们可能第一时间会想到继承,但是这种单继承语言一旦派生的子类过多,那么会产生 ...
- 微服务——RestTemplate
GET请求: 第一种:getForEntity: 此方法返回的是ResponseEntity,该对象是Spring对HTTP请求响应的封装. RestTemplate rt = new RestTem ...
- IOP开发数据库--20180105整理
http://10.110.22.12/cloud-web/#/login/tenant 数据库 代理节点 10.110.22.12 数据库 10.110.22.12 dev/ro ...
- Python几种数据结构内置方法的时间复杂度
参考:https://blog.csdn.net/baoli1008/article/details/48059623 注:下文中,’n’代表容器中元素的数量,’k’代表参数的值,或者参数的数量. 1 ...
- Angular1和Aangular4剖析
字面解析: 1.Angular1又名angularJs,从angular2,angular4都不带JS 2.变化:angular2跳转到angular4 架构: 1.angular1是基于MVC 2. ...
- jenkins 邮箱配置---腾讯企业邮箱
一,简单设置 1.登陆jenkins--> 系统管理 ---> 系统设置 2.邮箱就是发送者的邮箱,密码是登陆邮箱的密码 3.设置完以后,可以点击‘test configuration’, ...