前言
根据对用户ID、访问日期、访问次数的若干条数据,分析得出每个用户的累计访问次数,涉及字符串替换、时间函数、开窗、求和等技术。
1. 假如有用户访问数据(用户ID,访问日期,访问次数)
userId |
visitDate |
visitCount |
u01 |
2017/1/21 |
5 |
u02 |
2017/1/23 |
6 |
u03 |
2017/1/22 |
8 |
u04 |
2017/1/20 |
3 |
u01 |
2017/1/23 |
6 |
u01 |
2017/2/21 |
8 |
u02 |
2017/1/23 |
6 |
u01 |
2017/2/22 |
4 |
2. 希望得到每个月用户的访问累计次数
用户id |
月份 |
小计 |
累积 |
u01 |
2017-01 |
11 |
11 |
u01 |
2017-02 |
12 |
23 |
u02 |
2017-01 |
12 |
12 |
u03 |
2017-01 |
8 |
8 |
u04 |
2017-01 |
3 |
3 |
3. 启动hive,选库,建表
create table action
(userId string,
visitDate string,
visitCount int)
row format delimited fields terminated by "\t";
4. 将数据导入表
load data local inpath '/data/action.dat' into table action;
5. 转换日期字符串为标准格式
select
userId,
date_format(regexp_replace(visitDate,'/','-'),'yyyy-MM') mn,
visitCount
from
action;
userid |
mn |
visitcount |
u01 |
2017-01 |
5 |
u02 |
2017-01 |
6 |
u03 |
2017-01 |
8 |
u04 |
2017-01 |
3 |
u01 |
2017-01 |
6 |
u01 |
2017-02 |
8 |
u02 |
2017-01 |
6 |
u01 |
2017-02 |
4 |
6. 计算每人单月访问量
select
userId,
mn,
sum(visitCount) mn_count
from
(select
userId,
date_format(regexp_replace(visitDate,'/','-'),'yyyy-MM') mn,
visitCount
from
action) as t1
group by
userId,mn;
userid |
mn |
mn_count |
u01 |
2017-01 |
11 |
u01 |
2017-02 |
12 |
u02 |
2017-01 |
12 |
u03 |
2017-01 |
8 |
u04 |
2017-01 |
3 |
7. 按月累计访问量
select
userId,
mn,
mn_count,
sum(mn_count) over(partition by userId order by mn)
from (select
userId,
mn,
sum(visitCount) mn_count
from
(select
userId,
date_format(regexp_replace(visitDate,'/','-'),'yyyy-MM') mn,
visitCount
from
action) as t1
group by
userId,mn) as t2;
userid |
mn |
mn_count |
sum_window_0 |
u01 |
2017-01 |
11 |
11 |
u01 |
2017-02 |
12 |
23 |
u02 |
2017-01 |
12 |
12 |
u03 |
2017-01 |
8 |
8 |
u04 |
2017-01 |
3 |
3 |
The End