任务15:使用Hive进行全国气象数据分析

news2024/9/24 13:25:12

任务描述

知识点

  • 使用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_6>=0 group by month,city order by month,pre6 desc) as t1)as t2 where t2.number<=10;
  • 查看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;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1388035.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

广州市生物医药及高端医疗器械产业链大会暨联盟会员大会召开,天空卫士数据安全备受关注

12月20日&#xff0c;广州市生物医药及高端医疗器械产业链大会暨联盟会员大会在广州举办。在本次会议上&#xff0c;作为大会唯一受邀参加主题分享的技术供应商&#xff0c;天空卫士南区技术总监黄军发表《生物制药企业如何保护数据安全》的主题演讲。 做好承上启下“连心桥”…

概率论与数理统计————3.随机变量及其分布

一、随机变量 设E是一个随机试验&#xff0c;S为样本空间&#xff0c;样本空间的任意样本点e可以通过特定的对应法则X&#xff0c;使得每个样本点都有与之对应的数对应&#xff0c;则称XX&#xff08;e&#xff09;为随机变量 二、分布函数 分布函数&#xff1a;设X为随机变量…

使用Github + PicGo搭建个人图床,并使用CDN加速

文章目录 前言创建仓库配置PicGo如何使用 前言 在写博客的时候&#xff0c;常常需要为博客配图&#xff0c;于是一个好用稳定的图床的重要性不言而喻。本文主要介绍如何使用GitHub PicGo的方式快速搭建一个个人使用的图床。该方式方便快捷&#xff0c;还免费hh&#xff0c;唯…

git提交报错:remote: Please remove the file from history and try again.

1. 报错信息 remote: error: File: fba7046b22fd74b77425aa3e4eae0ea992d44998 500.28 MB, exceeds 100.00 MB. remote: Please remove the file from history and try again. git rev-list --objects --all | grep fba7046b22fd74b77425aa3e4eae0ea992d44998 2. 分析原因 e…

使用 Apache POI 更新/覆盖 特定的单元格

使用 Apache POI 更新特定的单元格 一. 需求二. 实现三. 效果 一. 需求 将以下表中第4行&#xff0c;第4列的单元格由“张宇”更新为“汤家凤”&#xff0c;并将更行后的结果写入新的Excel文件中&#xff1b; 二. 实现 使用Apache POI&#xff0c;可以精确定位到需要更改的单…

C#编程-自定义属性

命名自定义属性 让我们继续漏洞修复示例,在这个示例中新的自定义属性被命名为BugFixingAttribute。通常的约定是在属性名称后添加单词Attribute。编译器通过允许您调用具有短版名称的属性来支持附加。 因此,可以如以下代码段所示编写该属性: [ BugFixing ( 122,"Sara…

几款提高开发效率的Idea 插件

1、ignore 开发代码过程中经常会有一些需要提交到代码仓库的文件&#xff0c;比如java文件生成的.class、.jar 等&#xff0c;如果将编译后的文件都提交到代码库那么代码库会很大&#xff0c;关键是没有必要。 这款插件就可以很方便的解决某类文件或者某个文件夹不需要提交到…

BootStrap 实现轮播图

Bootstrap中文网 1、下载BootStrap 2、引入相关文件 在下载好的文件夹中找到下面的文件&#xff0c;复制到自己的项目中并引入 <link rel"stylesheet" href"bootstrap/css/bootstrap.min.css" /><script src"bootstrap/js/jquery.min.js…

单页面vite打包学习

前端工程化本人真的很发怵&#xff0c;一直也没有专心去突破一下&#xff0c;都是能用就用&#xff0c;所以今天小小学习一下打包&#xff0c;先从单页面应用的vite打包开始。本文主要是一些我的大白话和有限的经验&#xff0c;如有问题望指正。 一、问题 网页要从服务器请求…

动态规划:01背包问题(一)

本题力扣上没有&#xff0c;是刷的卡码网第46题感兴趣的小伙伴可以去刷一下&#xff0c;是ACM模式。本篇讲解二维dp数组来解决01背包问题&#xff0c;下篇博客将用一维dp数组来解决01背包问题。 题目&#xff1a; 46. 携带研究材料 时间限制&#xff1a;5.000S 空间限制&…

Java中的Socket你了解吗

☆* o(≧▽≦)o *☆嗨~我是小奥&#x1f379; &#x1f4c4;&#x1f4c4;&#x1f4c4;个人博客&#xff1a;小奥的博客 &#x1f4c4;&#x1f4c4;&#x1f4c4;CSDN&#xff1a;个人CSDN &#x1f4d9;&#x1f4d9;&#x1f4d9;Github&#xff1a;传送门 &#x1f4c5;&a…

stable diffusion使用相关

IP Adapter&#xff0c;我愿称之它为SD垫图 IP Adapter是腾讯lab发布的一个新的Stable Diffusion适配器&#xff0c;它的作用是将你输入的图像作为图像提示词&#xff0c;本质上就像MJ的垫图。 IP Adapter比reference的效果要好&#xff0c;而且会快很多&#xff0c;适配于各种…

任务14:使用MapReduce提取全国每年最低/最高气温

任务描述 知识点&#xff1a; 使用MapReduce提取数据 重 点&#xff1a; 开发MapReduce程序统计每年每个月的最低气温统计每年每个月的最高气温 内 容&#xff1a; 使用IDEA创建一个MapReduce项目开发MapReduce程序使用MapReduce统计每年每个月的最低气温使用MapReduce…

基于SSM的网上招聘系统的设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是 目录…

OpenCV-22高斯滤波

一、高斯函数的基础 要理解高斯滤波首先要直到什么是高斯函数&#xff0c;高斯函数是符合高斯分布的&#xff08;也叫正态分布&#xff09;的数据的概率密度函数。 高斯函数的特点是以x轴某一点&#xff08;这一点称为均值&#xff09;为对称轴&#xff0c;越靠近中心数据发生…

Ubuntu12.0安装g++过程及其报错

Ubuntu12.0安装g过程及其报错 https://blog.csdn.net/weixin_51286763/article/details/120703953 https://blog.csdn.net/dingd1234/article/details/124029945 2.报错二&#xff1a; [41/80] Building CXX object absl/synchronization/CMakeFiles/graphcycles_internal.di…

Java集合之LinkedList源码篇

☆* o(≧▽≦)o *☆嗨~我是小奥&#x1f379; &#x1f4c4;&#x1f4c4;&#x1f4c4;个人博客&#xff1a;小奥的博客 &#x1f4c4;&#x1f4c4;&#x1f4c4;CSDN&#xff1a;个人CSDN &#x1f4d9;&#x1f4d9;&#x1f4d9;Github&#xff1a;传送门 &#x1f4c5;&a…

微信小程序定义并获取日志/实时log信息

步骤一&#xff1a;开通实时日志 可以在开发者工具->详情->性能质量->实时日志&#xff0c;点击前往&#xff0c;在浏览器打开we分析界面&#xff1a; 也可登录小程序管理后台&#xff0c;点击统计进入we分析&#xff1a; 在we分析界面找到性能质量&#xff0c;打开实…

读书笔记——《未来简史》

前言 《未来简史》是以色列历史学家尤瓦尔赫拉利的人类简史三部曲之一。三部分别为《人类简史》《未来简史》《今日简史》。其中最为著名的当然是《人类简史》&#xff0c;非常宏大的一本关于人类文明历史的书籍&#xff0c;绝对可以刷新历史观&#xff0c;《人类简史》这本书…

Linux知识(未完成)

一、Linux 1.1 Linux 的应用领域 1.1.1 个人桌面领域的应用 此领域是 Linux 比较薄弱的环节但是随着发展&#xff0c;近几年 linux 在个人桌面领域的占有率在逐渐提高 1.1.2 服务器领域 linux 在服务器领域的应用是最高的 linux 免费、稳定、高效等特点在这里得到了很好的…