拿到的数据部分如下:
4月27日,黑龙江,境外输入-不详,0,45,0,黑龙江卫健委,https://m.thepaper.cn/newsDetail_forward_7160075,
4月27日,内蒙古,境外输入-不详,0,8,0,央视新闻,http://m.news.cctv.com/2020/04/27/ARTIxjwZSGhg1SXUn7Fv1ZsL200427.shtml,
4月27日,广东,境外输入-英国,1,0,0,央视新闻,http://m.news.cctv.com/2020/04/27/ARTIm0wxg5x059nT757vufcQ200427.shtml,
4月27日,浙江,境外输入-不详,0,1,0,央视新闻,http://m.news.cctv.com/2020/04/27/ARTIvZvXpkUgAEj09kd1a5tY200427.shtml,
4月27日,山东,境外输入-不详,0,1,0,央视新闻,http://m.news.cctv.com/2020/04/27/ARTIoCVnQVvaCRqP6GYBLmHU200427.shtml,
4月27日,香港,0,19,0,国家卫健委,http://m.news.cctv.com/2020/04/27/ARTIeHOtUgep4DL4b6e9XHI2200427.shtml,
1、在hdfs根目录下中创建一个目录将以上疫情数据上传到这个目录中:
hadoop fs -mkdir /Hive_Data
hadoop fs -put /usr/local/Hive_Data/yiqing.csv /Hive_Data
2、在hive中创建数据库名为: ods_yiqing_data
create database ods_yiqing_data;
use ods_yiqing_data;
3、创建一个hive外部表,字段为以上疫情数据全部字段 从hdfs 上加载数据:
create EXTERNAL table Hive_Data_EX
(
date_tine string,
shengfen string,
city string,
addpa int,
addout int,
adddeath int,
ins string,
ins1 string,
ins2 string,
ins3 string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
location '/Hive_Data';
load data inpath '/Hive_Data/yiqing.csv' into table Hive_Data_EX;
结果截图:
4、统计湖北省各市2月新增确诊病例总数,按照总数降序排列
select sum(addpa) as addsum,city from
Hive_Data_EX where shengfen = '湖北' and date_tine like '2月%'
group by city order by addsum desc ;
5、统计文件中安徽省合肥市每月新增确诊病例总数,按照降序排列,
select sum(addpa) as asum,substr(date_tine,0,2) as m
from Hive_Data_EX
where city= '合肥市'
group by substr(date_tine,0,2) order by asum desc;
结果截图:
6、统计文件中湖北每月新增出院病例总数最多的前2个城市
SQL排序函数ROW_NUMBER() over() 晚于where group by order by执行 在我看来就是一个打标记的功能
select * from
(select substr(date_tine,0,2),city,sum(addout) as sout,ROW_NUMBER() over(partition by substr(date_tine,0,2) order by sum(addout) desc) as rk
from Hive_Data_EX
where shengfen = '湖北'
group by substr(date_tine,0,2),city) as a
where a.rk<3;