大数据之hive:hive新功能之GROUPING SETS,Cube, Rollup
Posted 浊酒南街
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据之hive:hive新功能之GROUPING SETS,Cube, Rollup相关的知识,希望对你有一定的参考价值。
一、GROUPING SETS
1、概述
GROUPING SETS作为GROUP BY的子句,可以简单理解为多条group by语句通过union all把查询结果聚合起来;
2、实战
查看表test_03字段
hive (default)> desc test_03;
OK
col_name data_type comment
user_id bigint
device_id int
os_id int
app_id int
--sql1
select device_id,os_id,app_id,count(user_id) from test_03 group by device_id,os_id,app_id grouping sets((device_id))
--等价于sql1
SELECT device_id,null,null,count(user_id) FROM test_03 group by device_id
--sql2
select device_id,os_id,app_id,count(user_id) from test_03 group by device_id,os_id,app_id grouping sets((device_id,os_id))
--等价于sql2
SELECT device_id,os_id,null,count(user_id) FROM test_03 group by device_id,os_id
--sql3
select device_id,os_id,app_id,count(user_id) from test_03 group by device_id,os_id,app_id grouping sets((device_id,os_id),(device_id))
--等价于sql3
SELECT device_id,os_id,null,count(user_id) FROM test_03 group by device_id,os_id
UNION ALL
SELECT device_id,null,null,count(user_id) FROM test_03 group by device_id
--sql4
select device_id,os_id,app_id,count(user_id) from test_03 group by device_id,os_id,app_id grouping sets((device_id),(os_id),(device_id,os_id),())
--等价于sql4
SELECT device_id,null,null,count(user_id) FROM test_03 group by device_id
UNION ALL
SELECT null,os_id,null,count(user_id) FROM test_03 group by os_id
UNION ALL
SELECT device_id,os_id,null,count(user_id) FROM test_03 group by device_id,os_id
UNION ALL
SELECT null,null,null,count(user_id) FROM test_03
二、Cube
1、概述
cube简称数据魔方,可以实现hive多个任意维度的查询,cube(a,b,c)则首先会对(a,b,c)进行group by,然后依次是(a,b),(a,c),(a),(b,c),(b),©,最后在对全表进行group by,它会统计所选列中值的所有组合的聚合;
2、实战
select device_id,os_id,app_id count(user_id)
from test_xinyan_reg
group by device_id,os_id,app_id with cube;
--共有3^0+3^1+3^2+3^3=8种可能,如果维度增大,代码量显著增加
SELECT device_id,null,null ,count(user_id) FROM test_03 group by device_id
UNION ALL
SELECT null,os_id,null,count(user_id) FROM test_xinyan_reg group by os_id
UNION ALL
SELECT device_id,os_id,null,count(user_id) FROM test_xinyan_reg group by device_id,os_id
UNION ALL
SELECT null,app_id,null,count(user_id) FROM test_xinyan_reg group by app_id
UNION ALL
SELECT device_id,null,app_id ,count(user_id) FROM test_xinyan_reg group by device_id,app_id
UNION ALL
SELECT null,os_id,app_id,count(user_id) FROM test_xinyan_reg group by os_id,app_id
UNION ALL
SELECT device_id,os_id,app_id,count(user_id) FROM test_xinyan_reg group by device_id,os_id,app_id
UNION ALL
SELECT null,null,null count(user_id) FROM test_xinyan_reg group by client_version
三、Rollup
1、概述
rollup可以实现从右到做递减多级的统计,显示统计某一层次结构的聚合。
2、实战
--sql1
select device_id,os_id,app_id,count(user_id)
from test_xinyan_reg
group by device_id,os_id,app_id with rollup;
--等价于sql1
select device_id,os_id,app_id,count(user_id)
from test_xinyan_reg
group by device_id,os_id,app_id,client_version,from_id
grouping sets ((device_id,os_id,app_id),(device_id,os_id),(device_id),());
四、Grouping_ID函数
当我们没有统计某一列时,它的值显示为null,这可能与列本身就有null值冲突,这就需要一种方法区分是没有统计还是值本来就是null,grouping_id其实就是所统计各列二进制和;
比如表test_04有两个字段id,type;
id | type |
---|---|
1 | 1 |
1 | null |
2 | 1 |
3 | 2 |
3 | null |
SELECT id, type, GROUPING__ID, count(*) from test_04 GROUP BY id, type grouping sets((id, type),(id)) order by GROUPING__ID;
--结果如下
id type grouping__id _c3
3 2 0 1
2 1 0 1
1 NULL 0 1
1 1 0 1
3 NULL 0 1
3 NULL 1 2
1 NULL 1 2
2 NULL 1 1
grouping_id计算方法
grouping sets 中的每一种粒度,都对应唯一的 grouping__id 值,其计算公式与 group by 的顺序、当前粒度的字段有关。
序号 grouping set 给倒序排列的字段(sex class)赋值 对应的十进制(grouping__id 的值)
1 id 01 1
2 id,type 00 0
这就是上面 grouping sets 的结果中 grouping__id 值的由来。
以上是关于大数据之hive:hive新功能之GROUPING SETS,Cube, Rollup的主要内容,如果未能解决你的问题,请参考以下文章