--------------1、建库-------------------
--如果数据库已存在就删除
drop database if exists db_msg cascade;
--创建数据库
create database db_msg;
--切换数据库
use db_msg;
create table db_msg.tb_msg_source(
msg_time string comment "消息发送时间"
, sender_name string comment "发送人昵称"
, sender_account string comment "发送人账号"
, sender_sex string comment "发送人性别"
, sender_ip string comment "发送人ip地址"
, sender_os string comment "发送人操作系统"
, sender_phonetype string comment "发送人手机型号"
, sender_network string comment "发送人网络类型"
, sender_gps string comment "发送人的GPS定位"
, receiver_name string comment "接收人昵称"
, receiver_ip string comment "接收人IP"
, receiver_account string comment "接收人账号"
, receiver_os string comment "接收人操作系统"
, receiver_phonetype string comment "接收人手机型号"
, receiver_network string comment "接收人网络类型"
, receiver_gps string comment "接收人的GPS定位"
, receiver_sex string comment "接收人性别"
, msg_type string comment "消息类型"
, distance string comment "双方距离"
, message string comment "消息内容"
)
--指定分隔符为制表符
row format delimited fields terminated by '\t';
--------------3、加载数据-------------------
--上传数据文件到node1服务器本地文件系统(HS2服务所在机器)
--shell: mkdir -p /root/hivedata
--加载数据到表D
load data local inpath '/hiveData/data1.tsv' into table db_msg.tb_msg_source;
load data local inpath '/hiveData/data2.tsv' into table db_msg.tb_msg_source;
--查询表 验证数据文件是否映射成功
select * from tb_msg_source limit 10;
--统计行数
select count(*) as cnt from tb_msg_source;
抽取(extract)、转换(transform)、加载(load) 处理不合法的数据
按照具体要求处理ETL数据
-- 问题1:当前数据中,有一些数据的字段为空,不是合法数据
select
msg_time,
sender_name,
sender_gps
from db_msg.tb_msg_source
where length(sender_gps) = 0
limit 10;
-- 解决:用where&length 如果长度为0则排除
-- 问题2:需求中,需要统计每天、每个小时的消息量,但是数据中没有天和小时字段,只有整体时间字段,不好处理
select
msg_time
from db_msg.tb_msg_source
limit 10;
-- 解决:利用substring函数做截取,将需要的时间截取出来作为单独的字段
select msg_time,substr(msg_time,1,10) as dayinfo,substr(msg_time,12,2) as hourinfo
from tb_msg_source limit 2;
-- 问题3:需求中,需要对经度和维度构建地区的可视化地图,但是数据中GPS经纬度为一个字段,不好处理
select
sender_gps
from db_msg.tb_msg_source
limit 10;
-- 解决:使用切割函数split进行切割,将经度和纬度分为两个不同的字段
creat table as select : 将Select语句的结果保存到新表中
create table db_msg.tb_msg_etl as
select
*,
substr(msg_time,0,10) as dayinfo, --获取天
substr(msg_time,12,2) as hourinfo, --获取小时
split(sender_gps,",")[0] as sender_lng, --提取经度
split(sender_gps,",")[1] as sender_lat --提取纬度
from db_msg.tb_msg_source
--过滤字段为空的数据
where length(sender_gps) > 0 ;
create table if not exists tb_rs_total_msg_cnt
comment "今日消息总量"
as
select
dayinfo,
count(*) as total_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo;
select * from tb_rs_total_msg_cnt;--结果验证
create table if not exists tb_rs_hour_msg_cnt
comment "每小时消息量趋势"
as
select
dayinfo,
hourinfo,
count(*) as total_msg_cnt,
count(distinct sender_account) as sender_usr_cnt,
count(distinct receiver_account) as receiver_usr_cnt
from db_msg.tb_msg_etl
group by dayinfo,hourinfo;
create table if not exists tb_rs_loc_cnt
comment "今日各地区发送消息总量"
as
select
dayinfo,
sender_gps,
cast(sender_lng as double) as longitude, -- cast将string类型转化为double类型
cast(sender_lat as double) as latitude,
count(*) as total_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo,sender_gps,sender_lng,sender_lat;
select * from tb_rs_loc_cnt; --结果验证
create table if not exists tb_rs_usr_cnt
comment "今日发送消息人数、接受消息人数"
as
select
dayinfo,
count(distinct sender_account) as sender_usr_cnt,
count(distinct receiver_account) as receiver_usr_cnt
from db_msg.tb_msg_etl
group by dayinfo;
select * from tb_rs_usr_cnt; --结果验证
最:涉及排序问题 order by
create table if not exists tb_rs_susr_top10
comment "发送消息条数最多的Top10用户"
as
select
dayinfo,
sender_name as username,
count(*) as sender_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo,sender_name
order by sender_msg_cnt desc
limit 10;
select * from tb_rs_susr_top10; --结果验证
create table if not exists tb_rs_rusr_top10
comment "接受消息条数最多的Top10用户"
as
select
dayinfo,
receiver_name as username,
count(*) as receiver_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo,receiver_name
order by receiver_msg_cnt desc
limit 10;
select * from tb_rs_rusr_top10; --结果验证
create table if not exists tb_rs_sender_phone
comment "发送人的手机型号分布"
as
select
dayinfo,
sender_phonetype,
count(distinct sender_account) as cnt
from tb_msg_etl
group by dayinfo,sender_phonetype;
select * from tb_rs_sender_phone; --结果验证
create table if not exists tb_rs_sender_os
comment "发送人的OS分布"
as
select
dayinfo,
sender_os,
count(distinct sender_account) as cnt
from tb_msg_etl
group by dayinfo,sender_os;
select * from tb_rs_sender_os; --结果验证
业务包可进行自动或定时更新
大数据核心基础终于完结撒花了