一: 增加案例需求:
统计 pv , uv , 登录人数 , 游客人数 , 平均访问时长 , 二跳率 , 独立IP
用一张表去处理
1.1 查看track_log的分区
show partitions track_log ;
![hive 的数据案例 统计网站的数据信息 hive 的数据案例 统计网站的数据信息](https://ctyun-collect-0531.gdoss.xstore.ctyun.cn/files/2023/03/11/f36df2c7d2d74c30a47221fc89ee85ea.png)
1.2 建立一张会话信息表(session):
create table session_info(
session_id string,
guid string,
trackerU string,
landing_url string,
landing_url_ref string,
user_id string,
pv string,
stay_time string,
min_trackTime string,
ip string,
provinceId string
)
partitioned by (date string)
row format delimited fields terminated by '\t' ;
![hive 的数据案例 统计网站的数据信息 hive 的数据案例 统计网站的数据信息](https://ctyun-collect-0531.gdoss.xstore.ctyun.cn/files/2023/03/11/ea391f67614c4ae284c1ea7ed08a94d5.png)
1.3 创建临时表 session_info_tmp1
create table session_info_tmp1 as select
sessionId session_id,
max(guid) guid,
--trackerU string,
--landing_url string,
--landing_url_ref string,
max(endUserId) user_id,
count(url) pv,
(max(unix_timestamp(trackTime)) - min(unix_timestamp(trackTime))) stay_time,
min(trackTime) min_trackTime ,
max(ip) ip,
max(provinceId) provinceId
from track_log where date='20150828'
group by sessionId ;
![hive 的数据案例 统计网站的数据信息 hive 的数据案例 统计网站的数据信息](https://ctyun-collect-0531.gdoss.xstore.ctyun.cn/files/2023/03/11/4e9c3e5fac22403d91d87c29c905b78a.png)
1.4 创建临时表session_info_tmp2
create table session_info_tmp2 as select
sessionId session_id,
trackTime trackTime,
trackeru trackerU,
url landing_url,
referer landing_url_ref
from track_log where date='20150828' ;
![hive 的数据案例 统计网站的数据信息 hive 的数据案例 统计网站的数据信息](https://ctyun-collect-0531.gdoss.xstore.ctyun.cn/files/2023/03/11/fc3fc3bef397467fa221f8d616249f12.png)
1.5 导入数据处理:
insert overwrite table session_info partition (date='20150828')
select
a.session_id,
a.guid,
b.trackerU,
b.landing_url,
b.landing_url_ref,
a.user_id,
a.pv,
a.stay_time,
a.min_trackTime,
a.ip,
a.provinceId
from session_info_tmp1 a join session_info_tmp2 b
on a.session_id=b.session_id
and a.min_trackTime=b.trackTime ;
![hive 的数据案例 统计网站的数据信息 hive 的数据案例 统计网站的数据信息](https://ctyun-collect-0531.gdoss.xstore.ctyun.cn/files/2023/03/11/ce9319183fbf40c2b65ef24b4df8d92e.png)
1.6 生成最后所需的表:
create table vistor_users_info as
select
date,
count(distinct guid) UV,
sum(pv) PV,
count(case when user_id != '' then user_id else null end) login_users,
count(case when user_id = '' then user_id else null end) vistor_users,
avg(stay_time) avg_stay_time,
count(case when pv>=2 then session_id else null end)/count(session_id) sec_ratio,
count(distinct ip) ip
from session_info where date='20150828'
group by date ;
![hive 的数据案例 统计网站的数据信息 hive 的数据案例 统计网站的数据信息](https://ctyun-collect-0531.gdoss.xstore.ctyun.cn/files/2023/03/11/8b3653cf144749fc8a28100205b7a255.png)
1.7 查询结果:
select * from vistor_users_info;
![hive 的数据案例 统计网站的数据信息 hive 的数据案例 统计网站的数据信息](https://ctyun-collect-0531.gdoss.xstore.ctyun.cn/files/2023/03/11/65b1f54f70264c3d89491e4b32c736c9.png)