|NO.Z.00018|——————————|BigDataEnd|——|Hadoop&OLAP_Kylin.V18|——|Kylin.v18|Cube优化|使用衍生维度.V1|

一、使用衍生维度
### --- 使用衍生维度

~~~     一个维度可以是普通维度或者是衍生维度(Derived)。
~~~     将维度表的维度设置为衍生维度,
~~~     这个维度不会参与预计算,而是使用维度表的主键(或事实表的外键)来替代它。
~~~     Kylin会在底层记录维表主键与维度表其他维度之间的映射关系,
~~~     以便在查询时能够动态地将维度表的主键翻译成这些非主键维度,并进行实时聚合。
~~~     创建Cube的时候,这些维度如果指定为衍生维度,
~~~     Kylin将会排除这些维度,而是使用维度表的主键来代替它们创建Cuboid。
~~~     后续查询的时候,再基于主键的聚合结果,再进行一次聚合。
~~~     使用衍生维度会有效减少Cube中 Cuboid 的数量;但在查询时会增加聚合的时间。
### --- 不适用的场景:

~~~     如果从维度表主键到某个维度表维度所需要的聚合工作量非常大,
~~~     此时作为一个普通的维度聚合更合适,否则会影响Kylin的查询性能
二、案例一:定义衍生维度及对比:
### --- 案例一:定义衍生维度及对比:

~~~     # 有以下时间日期维表:
~~~     # 构建流程
~~~     创建项目 => 指定数据源 => 定义 model => 定义Cube => 查询
类型 示例值 列名 注释
string 2021-10-01 dateid 年-月-日
string 2021 dayofyear 年份,备注不能使用:year
string 1 dayofmonth 月份,备注不能使用:month
string 1 day_in_year 当年的第几天
string 1 day_in_month 当月的第几天
string 星期一 weekday 星期
string 1 week_in_month 当月的第几个星期
string 1 week_in_year 当年的第几个星期
string workday、weekend、holiday date_type 工作日:workday;休息日:weekend
国家法定节假日:holiday
string Q1 quarter 季度
三、准备数据源
### --- 准备数据文件

~~~     # 准备数据文件
[root@hadoop02 ~]# vim /data/kylin/dim_date.txt
2021-11-01,2021,2,32,1,5,1,5,workday,Q1
2021-11-02,2021,2,33,2,6,1,5,workday,Q1
### --- 在hive下建表并加载数据

~~~     # 在hive下建表并加载数据
hive (default)> drop table yanqi_kylin.dim_date;
hive (default)> create table yanqi_kylin.dim_date(
                dateid string,
                dayofyear string,
                dayofmonth string,
                day_in_year string,
                day_in_month string,
                weekday string,
                week_in_month string,
                week_in_year string,
                date_type string,
                quarter string
                )ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
~~~     # 加载数据
hive (default)> LOAD DATA LOCAL INPATH '/data/kylin/dim_date.txt' OVERWRITE INTO TABLE yanqi_kylin.dim_date;

~~~     # 查询加载的数据
hive (default)> select * from yanqi_kylin.dim_date;
~~~输出参数
dim_date.dateid dim_date.dayofyear  dim_date.dayofmonth dim_date.day_in_year    dim_date.day_in_month   dim_date.weekday    dim_date.week_in_month  dim_date.week_in_year   dim_date.date_type  dim_date.quarter
2021-11-01  2021    2   32  1   5   1   5   workday Q1
2021-11-02  2021    2   33  2   6   1   5   workday Q1
### --- 数据源说明

~~~     日期维表dim_date中有两个字段:dayofyear、dayofmonth,不能是yearmonth~~~     如果是 yearmonth,定义 model、cube、build cube 都没问题,
~~~     但是执行查询时涉及到yearmonth两列会报错。
四、Cube设计
### --- 查询

hive (default)> select dim_date.dayofyear, sum(price) 
                from yanqi_kylin.dw_sales 
                join yanqi_kylin.dim_date 
                on dw_sales.date1 = dim_date.dateid
                group by dim_date.dayofyear;
~~~输出参数
dim_date.dayofyear  _c1
2021    119000.0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(40)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示