2019 01 01 06 -62 -114 10259 306 38 6 -9999 -9999
2019 01 01 07 -62 -114 10264 281 29 -9999 -9999 -9999
2019 01 01 08 -62 -113 10268 268 39 -9999 -9999 -9999
2019 01 01 09 -59 -116 10271 254 31 3 -9999 -9999
2019 01 01 10 -62 -115 10271 238 24 -9999 -9999 -9999
2019 01 01 11 -80 -122 10269 254 12 -9999 -9999 -9999
2019 01 01 12 -67 -103 10264 322 12 5 -9999 -9999
2019 01 01 13 -62 -100 10261 27 13 -9999 -9999 -9999
2019 01 01 14 -29 -72 10259 230 40 -9999 -9999 -9999
2019 01 01 15 -20 -67 10254 242 49 5 -9999 -9999
字段解释如下:
字段1:位置1-4,长度4:观测年份,四舍五入到最接近
字段2:位置6-7,长度2:观察月,四舍五入到最接近
字段3:位置9-11,长度2:观察日,四舍五入到最接近
字段4:位置12-13,长度2:观察时,四舍五入到最接近
字段5:位置14-19,长度6:空气温度,单位:摄氏度,比例因子:10,缺少值:-9999,
字段6:位置20-24,长度6:露点温度,为了达到饱和,必须在恒定的压力和水蒸气含量下
冷却给定的空气包的温度。单位:摄氏度,比例因子:10,缺少值:-9999
字段7:Pos 26-31,长度6:海平面压力,相对于平均海平面的气压。单位:公顷,比例因
子:10,缺少值:-9999
字段8:32-37号位置,长度6:风向。正北角在正北和风向之间以顺时针方向测量的角度。
单位:角度。比例因子:1,缺少值:-9999。*注:静风风向编码为0。
字段9:38-43位置,长度6:风速,空气通过一个固定点的水平运动速度。单位:米每秒。
比例因子:10。缺少值:-9999
字段10:位置44-49,长度6:天空状况总覆盖代码,表示被云层或其它遮蔽现象覆盖的整个穹
顶的一部分的代码。缺少值:-9999
域:
0:无,SKC或CLR
1: 一个okta-1/10或更小但不是零
2: 两个oktas-2/10-3/10,或几个
3: 三个oktas-4/10
4: 四个oktas-5/10,或SCT
5: 五个oktas-6/10
6: 六个oktas-7/10-8/10
7: 七个oktas-9/10或以上,但不是10/10或BKN
8: 八个oktas-10/10,或OVC
9: 天空模糊不清,或云量无法估计
10: 部分遮蔽
11: 稀散
12: 分散的
13: 暗散射
14: 薄断
15: 破碎的
16: 暗断
17: 薄阴
18: 阴天
19: 阴天
字段11:位置50-55,长度6:液体沉淀深度尺寸-持续一小时,在一个小时的积累期内测量
的液体沉淀的深度。单位:毫米,比例因子:10,缺少值:-9999。*注:痕量降水编码为-1
字段12:位置56-61,长度6:液体沉淀深度尺寸-持续6小时,在六小时的积累期内测量的液
体沉淀的深度。单位:毫米。比例因子:10。缺少值:-9999。*注:痕量降水编码为-1
**字段很多,但是这里我只用前5个字段,任务是统计每日最高温度、最低温度、平均温度,有时间的话顺便计算点儿静态统计值。其余字段应该是类似的,正所谓一通百通。**
### 1.3 合并数据
数据很分散,合并数据:
`zcat 2019/*.gz > data.txt`
到此数据获取完毕。
## 2 MapReduce处理数据
### 2.1 环境配置,启动集群
详情跳转到 : [大数据学习系列:Hadoop3.0苦命学习(一)](https://bbs.csdn.net/topics/618545628),本文不再赘述。
### 2.2 上传到HDFS
hdfs dfs -mkdir -p /usr/hadoop/in
hdfs dfs -ls /usr/hadoop/
hdfs dfs -put data.txt /usr/hadoop/in/
执行截图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200526233016269.png)
去控制台查看一下是否成功:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200526233219846.png)
### 2.2 编写MapReduce代码
#### 2.2.1 TemperatureMapper
public class TemperatureMapper extends Mapper<LongWritable, Text,
Text, LongWritable> {
private static final long MISSING = -9999;
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
Iterable<String> split = Splitter.on(" ").omitEmptyStrings().split(line);
ArrayList<String> arrayList = new ArrayList<>(16);
for (String s : split) {
arrayList.add(s);
}
// 过滤掉字段不足的数据
if (arrayList.size() >= 5) {
String month = arrayList.get(1);
String day = arrayList.get(2);
long temperature = Long.parseLong(arrayList.get(4));
// 过滤掉温度不存在的数据
if (Math.abs(temperature - MISSING) > 0.0001) {
context.write(new Text(month + "/" + day),
new LongWritable((temperature)));
}
}
}
}
主要是原数据进行了清洗,过滤了一些不合格的数据。
#### 2.2.2 TemperatureReducer
public class TemperatureReducer extends Reducer<Text, LongWritable,
Text, Temperature> {
@Override
protected void reduce(Text key, Iterable values,
Context context) throws IOException, InterruptedException {
long maxTemperature = Long.MIN_VALUE;
long minTemperature = Long.MAX_VALUE;
double avgTemperature = 0.0;
long temp;
int count = 0;
if (values!=null) {
for (LongWritable value: values) {
temp = value.get();
maxTemperature = Math.max(temp, maxTemperature);
minTemperature = Math.min(temp, minTemperature);
avgTemperature += temp;
count++;
}
Temperature temperature = new Temperature(maxTemperature,
minTemperature, avgTemperature/count);
context.write(key, temperature);
}
}
}
计算出每日温度的最大值、最小值和平均值,并放入`Temperature`对象中。
#### 2.2.3 JobMain
public class JobMain extends Configured implements Tool {
@Override
public int run(String[] strings) throws Exception {
// 创建一个任务对象
Job job = Job.getInstance(super.getConf(), “mapreduce_temperature”);
// 打包放在集群运行时,需要做一个配置
job.setJarByClass(JobMain.class);
// 第一步:设置读取文件的类:K1和V1
job.setInputFormatClass(TextInputFormat.class);
TextInputFormat.addInputPath(job,
new Path("hdfs://node01:8020/usr/hadoop/in"));
// 第二步:设置Mapper类
job.setMapperClass(TemperatureMapper.class);
// 设置Map阶段的输出类型:k2和v2的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
// 第三、四、五、六步采用默认方式(分区,排序,规约,分组)
// 第七步:设置Reducer类
job.setReducerClass(TemperatureReducer.class);
// 设置Reduce阶段的输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Temperature.class);
// 第八步:设置输出类
job.setOutputFormatClass(TextOutputFormat.class);
// 设置输出路径
TextOutputFormat.setOutputPath(job,
new Path("hdfs://node01:8020/usr/hadoop/temperature"));
boolean b = job.waitForCompletion(true);
return b?0:1;
}
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
// 启动一个任务
ToolRunner.run(configuration, new JobMain(), args);
}
}
### 2.3 执行
#### 2.3.1 打包、上传
老套路,不说了。
#### 2.3.2 运行
`hadoop jar temperature_test-1.0-SNAPSHOT.jar cn.sky.hadoop.JobMain`
执行结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527083242819.png)
在这里看一眼数据:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527090706697.png)
嗯,还行。
## 3 导入数据到Hive
Hive详情过程,请参考:[大数据学习系列:Hadoop3.0苦命学习(五)](https://bbs.csdn.net/topics/618545628)
有个问题,若直接从HDFS导入数据到Hive,HDFS上的数据会丢失。
所以我将数据下载下来,重命名为 `temperature_data` ,并上传到 `node03` 上
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527091432159.png)
数据有了,开始创建Hive表:
create external table temperature (t_date string, t_max double,
t_min double, t_avg double) row format delimited fields terminated by ‘\t’;
加载数据到hive:
load data local inpath ‘/export/services/temperature_data’ overwrite
into table temperature;
查前面5条数据,看一眼:
select * from temperature limit 5;
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527092327259.png)
## 4 Hive数据分析
弄得简单,就查几个静态数据吧。
1. 查询2019全年平均温度
`select avg(t_avg) from temperature;`
哇,太慢了,查了25秒,最终结果是3.46(因为数据是被放大了10倍)左右
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527092811353.png)
2. 查询2019全年高于平均温度的天数
`select count(1) from temperature where t_avg > 34.6;`
答案是:196天,很显然低于平均气温的天数是169天。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527093329607.png)
好了,差不多就行了。
## 5 使用Sqoop导入数据到Mysql
Sqoop详情过程,请参考:[大数据学习系列:Hadoop3.0苦命学习(七)](https://bbs.csdn.net/topics/618545628)
### 5.1 Mysql创建数据库
CREATE TABLE temperature
(
Tem_Date
varchar(10) NOT NULL,
Tem_Max
double DEFAULT NULL,
Tem_Min
double DEFAULT NULL,
Tem_Avg
double DEFAULT NULL,
PRIMARY KEY (Tem_Date
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
### 5.2 开始导入
bin/sqoop export --connect jdbc:mysql://192.168.0.102:3306/userdb
–username root --password 123456 --table temperature
–export-dir /usr/hadoop/temperature --input-fields-terminated-by “\t”
经过半分钟的等待,就可以在mysql中查到数据了,见下图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527095829348.png)
不错,数据很好。
## 6 展示数据
这里前端使用echart,jsp,后端使用Spring、SpringMVC、Mybatis。
代码较多,展示主要的。
### 6.1 前端代码
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200527155203652.png)
主要是这一段,使用Ajax向后台请求数据,然后将数据丢进eChart中。
### 6.2 后端代码
![img](https://img-blog.csdnimg.cn/img_convert/86be6b9117832d8eeed874d5d1856ae0.png)
![img](https://img-blog.csdnimg.cn/img_convert/efc056c722d8b03697e6c08cb7b96d43.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
主要是这一段,使用Ajax向后台请求数据,然后将数据丢进eChart中。
### 6.2 后端代码
[外链图片转存中...(img-kBhb3ZBH-1714285064131)]
[外链图片转存中...(img-6RGQnv1n-1714285064131)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**