四川住房和城乡建设九大员网站,深圳建站公司 企网站,丰台企业网站建设,dw网页制作教程视频简单第三期任务描述
知识点#xff1a;
使用Hive进行数据分析
重 点#xff1a;
掌握Hive基本语句熟练使用Hive对天气数据进行分析
内 容#xff1a;
使用Hive创建外部表使用Hive对数据进行统计分析
任务指导
1. 使用Hive创建基础表
将China_stn_city.csv文件上传到HDFS的/…任务描述
知识点
使用Hive进行数据分析
重 点
掌握Hive基本语句熟练使用Hive对天气数据进行分析
内 容
使用Hive创建外部表使用Hive对数据进行统计分析
任务指导
1. 使用Hive创建基础表
将China_stn_city.csv文件上传到HDFS的/china_stn目录中启动metastore后台运行进入Hive命令行模式创建Hive数据库china_all创建Hive外部表china_all并加载HDFS上/china_all的数据 创建基站与城市对应关系表stn_city 创建天气数据与各城市的对应表 创建2022年天气数据表tmp_city_2022基于tmp_city获取2022年全年的天气数据并将数据插入到tmp_city_2022表中 2. 使用Hive分析数据可视化数据支持
1统计2022年每个月各省份的平均气温及平均风速
创建china_map表表字段包含月份省份平均气温平均风速统计2022年每个月各省份的平均气温及平均风速由于气温与风速数据中存在缺失值-9999所以统计平均气温和平均风速时只统计不等于()-9999的数据 2统计2022年每个月平均降水量TOP10的城市
创建city_precipitation_top10表表字段包含月份城市平均降水量6小时统计2022年每个月平均降水量TOP10的城市。本次查询通过两次子查询进行统计通过第一次子查询获取2022年每个月各个城市的平均降水量原数据中关于降水量的有两个字段“precipitation_1 string”和“precipitation_6 string”分别为1小时内的降水量与6小时内的降水量此时我们统计各城市平均6小时内的降水量基于第一次子查询所得的结果使用row_number()函数对各城市的平均降水量进行排名进行第二次子查询通过两次子查询分别获取到平均降水量以及排名最后使用最外层查询根据排名取得前十的城市 3统计2022年每个月各个城市的平均气温
创建city_temp表表字段包含月份城市平均气温统计2022年每个月各个城市的平均气温 4统计2022年每个月各个省份的平均气温
创建province_temp表表字段包含省份月份平均气温(预留) 预测气温统计2022年每个月各个省份的平均气温。表中的forecast字段作为预留的气温预测字段用于写入后面的气温预测数据当前该字段填入0 5统计2022年每个月各省份的平均气压
创建province_pressure表表字段包含月份省份平均气压统计2022年每个月各省份的平均气压 3. 使用Hive分析数据气温预测数据支持
在后续任务中会使用时间序列模型分别山东省以及全国各省份的气温进行预测所以需要使用2000-2022年各个省份每个月的平均气温作为训练数据
创建2000-2022年各省份平均气温表province_temp_all表字段包含年份省份月份平均气温统计2000-2022年各省份每月的平均气温 任务实现
1. 使用Hive创建基础表
进入/home/data目录将China_stn_city.csv文件上传到HDFS中该文件中存储了各基站与省份、城市的对应关系
数据说明 基站编号 省份 城市 纬度 经度 58015 安徽 砀山 34.27 116.2
在HDFS创建/china_stn目录并将/home/data/China_stn_city.csv文件上传到/china_stn目录中
# hadoop fs -mkdir /china_stn
# hadoop fs -put /home/data/China_stn_city.csv /china_stn
启动metastore后台运行
# hive --service metastore
输入【hive】命令进入Hive命令行模式创建Hive数据库
hive create database china_all;
hive use china_all;
创建Hive外部表china_all并加载HDFS上/china_all的数据
hive create external table china_all.china_all(
stn string,
year string,
month string,
day string,
hour string,
temp string,
dew_point_temp string,
pressure string,
wind_direction string,
wind_speed string,
clouds string,
precipitation_1 string,
precipitation_6 string
)
row format delimited
fields terminated by , stored as textfile location /china_all;
检验china_all表是否存在数据
hive select * from china_all limit 10; 创建基站与城市对应关系表stn_city
hive create external table china_all.stn_city(
stn string,
province string,
city string,
latitude string,
longitude string
)
row format delimited
fields terminated by , stored as textfile location /china_stn;
检验stn_city是否存在数据
hive select * from stn_city limit 10; 创建天气数据与各城市的对应表
hive create table china_all.tmp_city(
stn string,
year string,
month string,
day string,
hour string,
temp string,
dew_point_temp string,
pressure string,
wind_direction string,
wind_speed string,
clouds string,
precipitation_1 string,
precipitation_6 string,
province string,
city string
)
row format delimited
fields terminated by , stored as textfile;
通过stn字段将china_all表与stn_city表进行表连接使得每条天气数据找到对应的省份和城市信息并将其全部插入到tmp_city表中由于在原数据中有部分基站无法与省份进行匹配从而产生null值所以需要使用where判断去除null值
hive insert overwrite table tmp_city
select c2.*,sc.province,sc.city from china_all as c2 left join stn_city as sc on c2.stn sc.stn where sc.province is not null and sc.city is not null;
检验tmp_city表是否存在数据
hive select * from tmp_city limit 10; 创建2022年天气数据表tmp_city_2022基于tmp_city获取2022年全年的天气数据并将数据插入到tmp_city_2022表中
hive create table china_all.tmp_city_2022 as
select * from tmp_city where year 2022;
检验tmp_city_2022表是否存在数据
hive select * from tmp_city_2022 limit 10; 2. 使用Hive分析数据可视化数据支持
1统计2022年每个月各省份的平均气温及平均风速
创建china_map表表字段包含月份省份平均气温平均风速
hive create table china_map(
month string,
province string,
temp string,
wind_speed string
)
row format delimited
fields terminated by , stored as textfile;
统计2022年每个月各省份的平均气温及平均风速由于气温与风速数据中存在缺失值-9999所以统计平均气温和平均风速时只统计不等于()-9999的数据
hive insert overwrite table china_map select month,province,avg(temp),avg(wind_speed) from tmp_city_2022 where temp -9999 and wind_speed -9999 group by month,province;
查看china_map表共408条数据
hive select * from china_map; 2统计2022年每个月平均降水量TOP10的城市
创建city_precipitation_top10表表字段包含月份城市平均降水量6小时
hive create table city_precipitation_top10(
month string,
city string,
precipitation_6 string
)
row format delimited
fields terminated by , stored as textfile;
统计2022年每个月平均降水量TOP10的城市。本次查询通过两次子查询进行统计通过第一次子查询获取2022年每个月各个城市的平均降水量原数据中关于降水量的有两个字段“precipitation_1 string”和“precipitation_6 string”分别为1小时内的降水量与6小时内的降水量此时我们统计各城市平均6小时内的降水量基于第一次子查询所得的结果使用row_number()函数对各城市的平均降水量进行排名进行第二次子查询通过两次子查询分别获取到平均降水量以及排名最后使用最外层查询根据排名取得前十的城市
hive insert overwrite table city_precipitation_top10
select t2.month,t2.city,t2.pre6 from
(select *,row_number() over(partition by t1.month order by t1.pre6 desc) as number from
(select month,city,avg(precipitation_6) as pre6 from tmp_city_2022 where precipitation_6-9999 and precipitation_60 group by month,city order by month,pre6 desc) as t1)as t2 where t2.number10;
查看city_precipitation_top10表共120条数据
hive select * from city_precipitation_top10; 3统计2022年每个月各个城市的平均气温
创建city_temp表表字段包含月份城市平均气温
hive create table city_temp(
month string,
city string,
temp string
)
row format delimited
fields terminated by , stored as textfile;
统计2022年每个月各个城市的平均气温
hive insert overwrite table city_temp
select month,city,avg(temp) as tmp from tmp_city_2022 where temp-9999 group by month,city;
查看city_temp表共3969条数据
hive select * from city_temp limit 30; 4统计2022年每个月各个省份的平均气温
创建province_temp表表字段包含省份月份平均气温(预留) 预测气温
hive create table province_temp(
province string,
month string,
temp string,
forecast string
)
row format delimited
fields terminated by , stored as textfile;
统计2022年每个月各个省份的平均气温。表中的forecast字段作为预留的气温预测字段用于写入后面的气温预测数据当前该字段填入0
hive insert overwrite table province_temp
select province,month,avg(temp),0 from tmp_city_2022 where temp-9999 group by province,month order by province,month;
查看province_temp表共408条数据
hive select * from province_temp; 5统计2022年每个月各省份的平均气压
创建province_pressure表表字段包含月份省份平均气压
hive create table province_pressure(
month string,
province string,
pressure string
)
row format delimited
fields terminated by , stored as textfile;
统计2022年每个月各省份的平均气压
hive insert overwrite table province_pressure
select month,province,avg(pressure) as pressure from tmp_city_2022 where pressure-9999 group by month,province;
查看province_pressure表共398条数据
hive select * from province_pressure; 3. 使用Hive分析数据气温预测数据支持
在后续任务中会使用时间序列模型分别山东省以及全国各省份的气温进行预测所以需要使用2000-2022年各个省份每个月的平均气温作为训练数据
创建2000-2022年各省份平均气温表province_temp_all表字段包含年份省份月份平均气温
hive create table province_temp_all(
year string,
province string,
month string,
temp string
)
row format delimited
fields terminated by , stored as textfile;
统计2000-2022年各省份每月的平均气温
hive insert overwrite table province_temp_all
select year,province,month,avg(temp) from tmp_city where temp-9999 group by year,province,month order by year,province,month;
查看province_temp_all表共9385条数据
hive select * from province_temp_all limit 30;