|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,不能是year、month。
~~~ 如果是 year、month,定义 model、cube、build cube 都没问题,
~~~ 但是执行查询时涉及到year、month两列会报错。
四、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
分类:
bdv023-kylin
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」