Hive多维分析函数——With cube、Grouping sets、With rollup

有些指标涉及【多维度】的聚合,大的汇总维度,小的明细维度,需要精细化的下钻。

  • grouping sets: 多维度组合,组合维度自定义;
  • with cube: 多维度组合,程序自由组合,组合为各个维度的笛卡尔积;
  • **with rollup:**是 with cube的子集,以左侧维度为主,即不允许左侧为NULL,右侧为非NULL的情况出现

一、grouping sets

0 数据准备

sql 复制代码
with test1 as
     (select '2021-08' as month_date,'2021-08-11' as day_date,10 as pv
      union all
      select '2021-08' as month_date,'2021-08-10' as day_date,15 as pv
      union all
      select '2021-08' as month_date,'2021-08-08' as day_date,35 as pv
      union all
      select '2021-07' as month_date,'2021-07-08' as day_date,35 as pv
      union all
      select '2021-07' as month_date,'2021-07-06' as day_date,25 as pv
      union all
      select '2021-07' as month_date,'2021-07-03' as day_date,15 as pv)
      select 
        month_date,
        day_date,
        sum(pv) as pv
     from test1
      group by month_date,day_date
      grouping sets
       (
          (),
          (month_date),
          (month_date,day_date),
          (day_date)
        )
      order by month_date,day_date;

1 结果分析

二、with cube

0 数据准备

sql 复制代码
with test1 as
(select '2021-08' as month_date,'2021-08-11' as day_date,10 as pv
 union all
 select '2021-08' as month_date,'2021-08-10' as day_date,15 as pv
 union all
 select '2021-08' as month_date,'2021-08-08' as day_date,35 as pv
 union all
 select '2021-07' as month_date,'2021-07-08' as day_date,35 as pv
 union all
 select '2021-07' as month_date,'2021-07-06' as day_date,25 as pv
 union all
 select '2021-07' as month_date,'2021-07-03' as day_date,15 as pv)
 
 select 
   month_date,
   day_date,
   sum(pv) as pv
 from test1
 group by month_date,day_date
 with cube
 order by month_date,day_date;

1 结果分析

2 总结

  • 从with cube和grouping sets的案例可以看出,两个结果是一样的;
  • with cube的维度组合(各个维度的笛卡尔积)就是groupingsets里面手动添加的维度,即为month_date,day_date两个维度的笛卡尔积。维度组合即为:()、 (month_date)、 (month_date,day_date)、 (day_date)

三、 with rollup

0 数据准备

sql 复制代码
with test1 as
(select '2021-08' as month_date,'2021-08-11' as day_date,10 as pv
 union all
 select '2021-08' as month_date,'2021-08-10' as day_date,15 as pv
 union all
 select '2021-08' as month_date,'2021-08-08' as day_date,35 as pv
 union all
 select '2021-07' as month_date,'2021-07-08' as day_date,35 as pv
 union all
 select '2021-07' as month_date,'2021-07-06' as day_date,25 as pv
 union all
 select '2021-07' as month_date,'2021-07-03' as day_date,15 as pv)

select 
  month_date,
  day_date,
  sum(pv) as pv
from test1
group by month_date,day_date
with rollup
order by month_date,day_date;

1 结果分析

2 总结

从结果上可以看出,with rollup 和with cube的区别是,少了day_date这个单独维度的聚合,因为with rollup是以左侧维度为主,当左侧month_date维度为NULL时,右侧day_date维度必须为NULL。

参考文章 :

https://zhuanlan.zhihu.com/p/631268351

https://blog.51cto.com/u_14555/6696007

相关推荐
更深兼春远5 小时前
flink+clinkhouse安装部署
大数据·clickhouse·flink
专注API从业者8 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
媒体人8889 小时前
GEO 优化专家孟庆涛:技术破壁者重构 AI 时代搜索逻辑
大数据·人工智能
最初的↘那颗心10 小时前
Flink Stream API 源码走读 - print()
java·大数据·hadoop·flink·实时计算
君不见,青丝成雪11 小时前
hadoop技术栈(九)Hbase替代方案
大数据·hadoop·hbase
晴天彩虹雨11 小时前
存算分离与云原生:数据平台的新基石
大数据·hadoop·云原生·spark
朗迪锋11 小时前
数字孪生 :提高制造生产力的智能方法
大数据·人工智能·制造
杨荧12 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
健康平安的活着12 小时前
es7.x es的高亮与solr高亮查询的对比&对比说明
大数据·elasticsearch·solr
缘华工业智维13 小时前
CNN 在故障诊断中的应用:原理、案例与优势
大数据·运维·cnn