hive3.1.x教程:统计PV、UV、流失用户、新增用户 作者:马育民 • 2025-12-06 22:10 • 阅读:10001 # 准备数据 ### 数据说明 第一列:IP 第二列:访问时间 第三列:访问的URL ### 数据 **注意:** `\N` 导入到 hive 中,表示 `NULL` ``` 87.157.197.56,2017-01-01 6:37:44,http://www.pinxixi.com/news?result=A 124.115.145.213,2017-01-01 11:7:32,http://www.pinxixi.com/newsmod=class 147.127.161.197,2017-01-01 20:51:19,http://www.pinxixi.com/register?uname=&passwd= 120.132.61.103,2017-01-01 5:13:50,http://www.pinxixi.com/news?cid=? 73.102.147.225,2017-01-01 15:3:11,http://www.pinxixi.com/index 93.127.144.196,2017-01-01 7:33:21,http://www.pinxixi.com/search?uname=&passwd= ``` ### 上传 上传到 `hadoop1` 服务器的 `/program` 目录,比如文件名:`1.txt` # hive操作 登录 `hadoop1` 服务器 ### 启动hive客户端 执行命令 `hive`,启动 hive 客户端, ### 创建表 ``` create table recorder( ip string, systime string, url string) partitioned by (sysdate string) row format delimited fields terminated by ',' stored as textfile; ``` ### 导入数据 ``` load data local inpath '/program/1.txt' overwrite into table recorder partition(sysdate='2017-01-01'); ``` # 统计PV 统计 2017-01-01 的 [PV](https://www.malaoshi.top/show_1GW2MK5RJj9b.html "PV"),并自动建表,将结果保存到表中,表命名规则:`pv_yyyyMMdd`,如:`pv_20170101` **提示:**页面浏览量即为 PV(Page View),是指所有用户浏览页面的总和(用户每访问一次页面就算 1 次) ### 第一步 ``` create table pv_20170101 as select count(1) as pv from recorder where sysdate='2017-01-01'; ``` 查询数据: ``` select * from pv_20170101; ``` 执行结果: ``` 6 ``` # 统计UV 统计 `2017-01-01` [UV](https://www.malaoshi.top/show_1GW2MK5RJj9b.html "UV"),并自动建表,将结果保存到表中,表命名规则:`uv_yyyyMMdd`,如:`uv_20170101` >**UV:**即:一天之内,访问网站的不同 IP 数之和。同一 IP 无论访问了几个页面,在 UV 中记为 1 ### 分析 IP去重,然后统计个数 ``` create table uv_20170101 as select count(distinct ip) as ip from recorder where sysdate='2017-01-01'; ``` 查询数据: ``` select * from uv_20170101; ``` 执行结果: ``` 6 ``` # 统计流失用户 统计 `2017-01-01` 流失用户数量,并自动建表,将结果保存到表中,表命名规则:` retention_yyyyMMdd`,如:`retention_20170101` >**流失用户:**浏览网站后,就不在浏览的用户 ### 分析 可以通过用户的 IP 进行分组,如果分组后的记录数只有一条,即为流失用户 **提示:**实际网站分析的时候,对流失用户定义更复杂。 ``` create table retention_20170101 as select count(1) as retention from (select count(ip) as times from recorder where sysdate='2017-01-01' group by ip having times=1) e; ``` 查询: ``` select * from retention_20170101; ``` 执行结果: ``` 6 ``` # 统计新增用户 统计 `2017-01-01` 新增用户数量,并自动建表,将结果保存到表中,表命名规则:` increase_yyyyMMdd`,如:`increase_20170101` ### 分析 该网站的用户注册页面的 path 为 `Register`,所以当用户点击注册时请求的是 Register 即可。 ``` create table increase_20170101 as select count(1) as increase from recorder where sysdate='2017-01-01' and url like '%register%'; ``` 查询: ``` select * from increase_20170101; ``` 执行结果: ``` 1 ``` 原文出处:http://malaoshi.top/show_1GW2MKMFxFGJ.html