这是我参与11月更文挑战的第29天,活动详情查看:2021最后一次更文挑战
一、准备数据
- 维度表的优化:
-
要具有数据一致性, 主键值必须是唯一的(否则
Kylin
构建过程会报错) -
维度表越小越好, 因为
Kylin
会将维度表加载到内存中供查询, 过大的表不适合作为维度表, 默认的阈值是300MB
-
改变频率低,
Kylin
会在每次构建中试图重用维度表的快照(Snapshot
), 如果维度表经常改变的话, 重用就会失效, 这就会导致要经常对维度表创建快照 -
维度表最好不要是
Hive
视图(View
), 因为每次都需要将视图进行物化, 从而导致额外的时间开销
- 事实表的优化:
-
移除不参与
Cube
构建的字段, 可以提升构建速度, 降低Cube
构建结果的大小 -
尽可能将事实表进行维度拆分, 提取公用的维度
-
保证维度表与事实表的映射关系, 过滤无法映射的记录
准备脚本
将 4个 数据文件:
dw_sales_data.txt
dim_channel_data.txt
dim_product_data.txt
dim_region_data.txt
1个 SQL
脚本: kylin_examples.sql
放置在 /root/kylin
目录下。
kylin_examples.sql
脚本内容:
-- 创建订单数据库、表结构
create database if not exists `lagou_kylin`;
-- 1. 销售表:dw_sales
-- id 唯一标识
-- date1 日期
-- channelId 渠道ID
-- productId 产品ID
-- regionId 区域ID
-- amount 数量
-- price 金额
create table lagou_kylin.dw_sales(
id string,
date1 string,
channelId string,
productId string,
regionId string,
amount int,
price double
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
-- 2. 渠道表: dim_channel
-- channelId 渠道ID
-- channelName 渠道名称
create table lagou_kylin.dim_channel(
channelId string,
channelName string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
-- 3. 产品表: dim_product
create table lagou_kylin.dim_product(
productId string,
productName string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
-- 4. 区域表: dim_region
create table lagou_kylin.dim_region(
regionId string,
regionName string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
-- 导入数据
LOAD DATA LOCAL INPATH '/root/kylin/dw_sales_data.txt' OVERWRITE INTO TABLE lagou_kylin.dw_sales;
LOAD DATA LOCAL INPATH '/root/kylin/dim_channel_data.txt' OVERWRITE INTO TABLE lagou_kylin.dim_channel;
LOAD DATA LOCAL INPATH '/root/kylin/dim_product_data.txt' OVERWRITE INTO TABLE lagou_kylin.dim_product;
LOAD DATA LOCAL INPATH '/root/kylin/dim_region_data.txt' OVERWRITE INTO TABLE lagou_kylin.dim_region;
复制代码
- 执行命令
hive -f kylin_examples.sql
[root@linux122 kylin]# ll
total 20
-rw-r--r-- 1 root root 31 May 9 17:20 dim_channel_data.txt
-rw-r--r-- 1 root root 44 May 9 17:20 dim_product_data.txt
-rw-r--r-- 1 root root 22 May 9 17:20 dim_region_data.txt
-rw-r--r-- 1 root root 999 May 9 17:20 dw_sales_data.txt
-rw-r--r-- 1 root root 1397 May 9 17:20 kylin_examples.sql
[root@linux122 kylin]# hive -f kylin_examples.sql
复制代码
- 检查建表、数据导入操作是否成功
[root@linux122 kylin]# hive
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/lagou/servers/hive-2.3.7/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/lagou/servers/hadoop-2.9.2/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Logging initialized using configuration in jar:file:/opt/lagou/servers/hive-2.3.7/lib/hive-common-2.3.7.jar!/hive-log4j2.properties Async: true
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
hive (default)> show databases;
OK
database_name
ads
default
dim
dwd
dws
lagou_kylin
mydb
ods
sale
test1
tmp
Time taken: 0.661 seconds, Fetched: 11 row(s)
hive (default)> use lagou_kylin;
OK
Time taken: 0.064 seconds
hive (lagou_kylin)> show tables;
OK
tab_name
dim_channel
dim_product
dim_region
dw_sales
Time taken: 0.083 seconds, Fetched: 4 row(s)
复制代码
- 按照日期统计订单量、订单总金额
use lagou_kylin;
select date1, sum(price) as total_money, sum(amount) as total_amount
from dw_sales
group by date1;
-- 实操如下:
date1 total_money total_amount
2019-02-01 57800.0 17
2019-02-02 61200.0 18
Time taken: 3.739 seconds, Fetched: 2 row(s)
复制代码
二、创建 Cube
(按日期)
(1)核心步骤
核心步骤: DataSource
=> Model
=> Cube
Model
: 描述了一个星型模式的数据结构, 定义事实表(Fact Table
) 和 维表(Lookup Table
), 以及它们之间的关系。
基于一个 model
可创建多个 Cube
, 可减少重复定义工作。
(2)Cube
设计
维度: 日期
度量: 订单商品销售量、销售总金额
SQL
,如下:
SELECT date1, sum(price), sum(amount)
FROM dw_sales
GROUP BY date1;
date1 _c1 _c2
2019-02-01 57800.0 17
2019-02-02 61200.0 18
Time taken: 1.518 seconds, Fetched: 2 row(s)
复制代码
(3)操作步骤
执行步骤:
-
创建项目(
Project
)【非必须】 -
创建数据源(
DataSource
)。指定有哪些数据需要进行数据分析 -
创建模型(
Model
)。指定具体要对哪个事实表、那些维度进行数据分析 -
创建立方体(
Cube
)。指定对哪个数据模型执行数据预处理,生成不同维度的数据 -
执行构建、等待构建完成
-
再执行
SQL
查询, 获取结果。从Cube
中查询数据
1. 创建项目(Project
)
如下图所示:
2. 创建数据源(DataSource
)
指定有哪些数据需要进行数据分析。
如下图所示:
3. 创建模型(Model
)
指定具体要对哪个事实表、那些维度进行数据分析
如下图所示:
4. 创建立方体(Cube
)
指定对哪个数据模型执行数据预处理,生成不同维度的数据
5. 执行构建、等待构建完成
如下图所示:
6. 再执行 SQL
查询, 获取结果。
从
Cube
中查询数据
如图:
三、创建 Cube
(按渠道)
(1)Cube
设计
维度: 渠道
指标: 销售总金额、订单总笔数、最大订单金额、订单的平均金额
SQL
:
select t2.channelid, t2.channelname,
sum(t1.price), max(t1.price), count(t1.price), avg(t1.price)
from dw_sales t1 join dim_channel t2 on t1.channelid = t2.channelid
group by t2.channelid, t2.channelname;
复制代码
(2)核心步骤
指定数据源【可省略】 => 定义 model
=> 定义 Cube
=> Cube Build
=> SQL
核心步骤如下:
- 创建
Model
- 创建
Cube
- 执行构建、等待构建完成
- 执行
SQL
查询, 获取结果
1. 创建 Model
其他的直接 NEXT
。
2. 创建 Cube
其他均 NEXT
。