題目:
1.每個店鋪的uv(訪客數)
2.每個店鋪訪問top3的資訊(店鋪名稱,訪客id,訪問次數)
資料準備(第一列是使用者id,第二列是店鋪名):輸出結果:
a 4b 4
c 3輸出結果:
a u5 1
a u1 2
a u2 3
b u4 1
b u1 2
b u5 3
c u2 1
c u6 2
c u3 3
建表語句:u1 a
u2 b
u1 b
u1 a
u3 c
u4 b
u1 a
u2 c
u5 b
u4 b
u6 c
u2 c
u1 b
u2 a
u2 a
u3 a
u5 a
u5 a
u5 a
匯入資料:hive>
create
table visit(
> user_id string,
> shop string)
>
row format delimited fields
terminated
by'\t'
;
統計店鋪的訪客數hive>
load
data
local inpath '/usr/hdk/data/lx2.csv'
into
table visit;
統計訪客數就是去重後計數,那麼最難的就在於去重。
正常情況下去重操作想起來的第乙個是distinct關鍵字,但是如果表資料量大的話distinct會導致reduce數量過大(hive沒有對distinct優化)。
這樣的話,就只可以選擇分組語句。
select
shop,
user_id
from
visit
group
by shop,user_id
;
這就完成了第一步去重,然後count計數一下就行。輸出結果:
a u1
a u2
a u3
a u5
b u1
b u2
b u4
b u5
c u2
c u3
c u6
select
shop,
count(*
) uv
from
(select
shop,
user_id
from
visit
group
by shop,user_id
) t1
group
by shop
;
每個店鋪訪問數top3的資訊輸出結果:
a 4b 4
c 3
想要獲得每家店鋪訪問數前三前提是得得到訪問店鋪的每個人訪問了多少次。
select
shop,
user_id,
count(*
)from
visit
group
by shop,user_id
;
之後呢我們新增乙個rk列作為uv列的序號,然後讓rk<=3就可以取出top3。輸出結果
a u1 3
a u2 2
a u3 1
a u5 3
b u1 2
b u2 1
b u4 2
b u5 1
c u2 2
c u3 1
c u6 1
select
shop,
user_id,
ct,row_number(
)over
(partition
by shop order
by ct desc
) rk
from
(select
shop,
user_id,
count(*
) ct
from
visit
group
by shop,user_id
)t1;
然後加個where條件即可:輸出結果
a u5 3 1
a u1 3 2
a u2 2 3
a u3 1 4
b u4 2 1
b u1 2 2
b u5 1 3
b u2 1 4
c u2 2 1
c u6 1 2
c u3 1 3
select
shop,
user_id,
rkfrom
(select
shop,
user_id,
ct,row_number(
)over
(partition
by shop order
by ct desc
) rk
from
(select
shop,
user_id,
count(*
) ct
from
visit
group
by shop,user_id
)t1)t2
where rk <=
3;
輸出結果:
a u5 1
a u1 2
a u2 3
b u4 1
b u1 2
b u5 3
c u2 1
c u6 2
c u3 3
mysql 實現時統計 hql
date format date,format 根據format字串格式化date值。下列修飾符可以被用在format字串中 m 月名字 january december w 星期名字 sunday saturday d 有英語字首的月份的日期 1st,2nd,3rd,等等。y 年,數字,4 位 y...
hql簡單查詢語句
session中get 方法只能通過id來查詢結果 hibernate工具類的完善 hibernateutil public class hibernateutil static public static sessionfactory getsessionfactory public static...
編寫簡單的hql命令 hql 的執行方式
注意指令碼放的目錄和許可權 hive e sql語句 hive f file sql.txt crontab e 裡面的指令碼命令要寫絕對命令 crontab e 編輯 crontab l 檢視 crontab r 刪除 hql指令碼編寫的注意點 1 寫好sh指令碼 然後利用crontab e 進行...