TL;DR
- 场景:在 h122 节点已启动 Kylin,使用 Hive 装载自造样例数据,演示 Cube 预计算与查询加速。
- 结论:按星型模型建模并裁剪 Aggregation Group,可显著缩短多维聚合查询时延;实时场景可用 Kylin 4.x + Kafka。
- 产出:数据脚本+Hive DDL/DML+验证 SQL 与截图;可直接复用到小型 PoC 或团队内训。


Cube 介绍
Apache Kylin 是一个开源的分布式分析引擎,专注于提供大数据的实时OLAP(在线分析处理)能力。Cube(立方体)是 Apache Kylin 的核心概念之一,通过预计算大规模数据的多维数据集合,加速复杂的 SQL 查询。下面详细介绍 Cube 的关键点:
Cube 的基本概念
Kylin 中的 Cube 是通过对一组事实表(通常是业务数据表)进行多维建模后,生成的预计算数据结构。Cube 涉及对多维数据的度量和维度的组合,从而可以在查询时通过检索预先计算的结果来显著减少计算开销。
- 维度(Dimension):数据中用于分组、筛选和切片的数据字段,例如时间、地区、产品等。
- 度量(Measure):通常是需要进行聚合计算的数据字段,例如销售额、订单数等。
- Cuboid:每个 Cube 由多个 Cuboid 构成,Cuboid 是一个特定维度组合的子集。Cube 中每种维度组合都会生成一个 Cuboid,每个 Cuboid 存储了该组合下的预聚合结果。
Cube 的创建过程
- 数据建模:首先在 Kylin 中创建一个数据模型(Data Model),这个模型定义了事实表和维度表之间的关系,类似于星型或雪花型模式。模型中也定义了需要聚合的度量字段。
- Cube 设计:基于数据模型设计 Cube,指定 Cube 的维度和度量。Kylin 会根据定义自动计算所有可能的维度组合(Cuboid)。
- 构建 Cube:构建过程会读取底层数据源(如 Hive、HBase、Kafka),然后根据指定的维度和度量生成每个 Cuboid 的预计算数据。这些预计算结果存储在 HBase 或其他存储引擎中。
Cube 的查询与优化
- 查询加速:当有 SQL 查询请求到达时,Kylin 会根据查询所涉及的维度组合,选择合适的 Cuboid 返回结果,避免了实时计算,极大地提高了查询性能。
- Cube 优化:为了控制 Cube 大小和加速构建,Kylin 支持裁剪 Cube,通过配置仅生成部分 Cuboid,这称为"Aggregation Group",可以减少冗余计算。
实时 OLAP
Kylin 4.0 引入了对实时 OLAP 的支持,使用 Kafka 作为实时数据流输入,构建实时 Cube。通过使用 Lambda 架构,Kylin 可以支持实时和批处理数据的整合分析。
Cube 的典型应用场景
- 大规模数据分析:Cube 适用于分析超大规模的数据集,通过预计算方式加速查询。
- 实时分析:实时 Cube 允许用户在近乎实时的基础上分析流数据。
- 商业智能(BI)工具的集成:Kylin 提供与 Tableau、Power BI 等常见 BI 工具的集成,用户可以使用熟悉的 SQL 查询语言进行复杂的多维分析。
前置要求
需要你配置并且启动好了 Kylin! 由于我是在 h122.wzk.icu 节点上启动的,所以下面的操作都在 h122 节点上,后续没有详细说明就是在该机器上了。
准备数据

准备数据
将4个数据文件:
- dw_sales_data.txt
- dim_channel_data.txt
- dim_product_data.txt
- dim_region_data.txt
我写了几个脚本来辅助生成数据 
dw_sales_data
python
import random
import datetime
# 设置参数
num_records = 1000
output_file = 'dw_sales_data.txt'
# 定义可能的值
channel_ids = ['C001', 'C002', 'C003', 'C004']
product_ids = ['P001', 'P002', 'P003', 'P004']
region_ids = ['R001', 'R002', 'R003', 'R004']
base_date = datetime.date(2024, 1, 1)
# 生成数据
with open(output_file, 'w') as f:
for i in range(num_records):
record_id = f"{i+1:04d}"
date1 = (base_date + datetime.timedelta(days=random.randint(0, 365))).strftime('%Y-%m-%d')
channel_id = random.choice(channel_ids)
product_id = random.choice(product_ids)
region_id = random.choice(region_ids)
amount = random.randint(1, 100)
price = round(random.uniform(10.0, 500.0), 2)
line = f"{record_id},{date1},{channel_id},{product_id},{region_id},{amount},{price}\n"
f.write(line)
print(f"{num_records} records have been written to {output_file}")
生成数据如下图所示: 
dim_channel_data
python
# 设置参数
output_file = 'dim_channel_data.txt'
# 定义渠道ID和渠道名称
channels = [
('C001', 'Online Sales'),
('C002', 'Retail Store'),
('C003', 'Wholesale'),
('C004', 'Direct Sales')
]
# 生成数据
with open(output_file, 'w') as f:
for channel_id, channel_name in channels:
line = f"{channel_id},{channel_name}\n"
f.write(line)
print(f"Channel data has been written to {output_file}")
生成数据如下图所示: 
dim_product_data
python
# 设置参数
output_file = 'dim_product_data.txt'
# 定义产品ID和产品名称
products = [
('P001', 'Smartphone'),
('P002', 'Laptop'),
('P003', 'Tablet'),
('P004', 'Smartwatch'),
('P005', 'Camera'),
('P006', 'Headphones'),
('P007', 'Monitor'),
('P008', 'Keyboard'),
('P009', 'Mouse'),
('P010', 'Printer')
]
# 生成数据
with open(output_file, 'w') as f:
for product_id, product_name in products:
line = f"{product_id},{product_name}\n"
f.write(line)
print(f"Product data has been written to {output_file}")
生成数据如下图所示: 
dim_region_data
python
# 设置参数
output_file = 'dim_region_data.txt'
# 定义区域ID和区域名称
regions = [
('R001', 'North America'),
('R002', 'Europe'),
('R003', 'Asia'),
('R004', 'South America'),
('R005', 'Africa'),
('R006', 'Australia'),
('R007', 'Antarctica')
]
# 生成数据
with open(output_file, 'w') as f:
for region_id, region_name in regions:
line = f"{region_id},{region_name}\n"
f.write(line)
print(f"Region data has been written to {output_file}")
生成的数据如下图所示: 
kylin_examples.sql
sql
-- 创建订单数据库、表结构
create database if not exists `wzk_kylin`;
-- 1、销售表:dw_sales
-- id 唯一标识
-- date1 日期
-- channelId 渠道ID
-- productId 产品ID
-- regionId 区域ID
-- amount 数量
-- price 金额
create table wzk_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 wzk_kylin.dim_channel(
channelId string,
channelName string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
-- 3、产品表:dim_product
create table wzk_kylin.dim_product(
productId string,
productName string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
--4、区域表:dim_region
create table wzk_kylin.dim_region(
regionId string,
regionName string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
-- 导入数据
LOAD DATA LOCAL INPATH '/opt/wzk/kylin_test/dw_sales_data.txt'
OVERWRITE INTO TABLE wzk_kylin.dw_sales;
LOAD DATA LOCAL INPATH '/opt/wzk/kylin_test/dim_channel_data.txt'
OVERWRITE INTO TABLE wzk_kylin.dim_channel;
LOAD DATA LOCAL INPATH '/opt/wzk/kylin_test/dim_product_data.txt'
OVERWRITE INTO TABLE wzk_kylin.dim_product;
LOAD DATA LOCAL INPATH '/opt/wzk/kylin_test/dim_region_data.txt'
OVERWRITE INTO TABLE wzk_kylin.dim_region;
运行数据
我们需要把刚才的数据上传到指定目录上,/opt/wzk/目录下。
shell
cd /opt/wzk/kylin_test
我已经上传到服务器上了: 
SQL文件也记得上传上去
执行Hive:
shell
hive -f kylin_examples.sql
执行结果如下图所示: 
测试数据
我们需要启动Hive
shell
hive
执行结果如下图所示:
执行如下的指令:
shell
use wzk_kylin;
select date1, sum(price) as total_money, sum(amount) as
total_amount
from dw_sales
group by date1;
执行结果如下图所示: 
错误速查
| 症状 | 根因定位 | 修复 |
|---|---|---|
| Table not found wzk_kylin.dw_sales | 数据库/表未创建或大小写不一致 | 重新执行建表脚本;确认大小写与库前缀一致 |
| LOAD DATA LOCAL 失败或数据为空 | 本地路径错误/权限不足/分隔符不符 | 修正路径与权限;确认 CSV 分隔与列顺序一致 |
| Kylin 构建任务卡住或失败 | HDFS/YARN 队列/权限或 MapReduce 参数不足 | 提升队列资源;在 Kylin 调优构建并行度与内存 |
| 查询命中慢(未走预计算) | 维度/度量或 Aggregation Group 设计不当 | 精简维度组合,合理裁剪 Cuboid;为高频查询单独建 AG |
| No realize for query | Cube 未覆盖该维度组合或时间分区 | 扩充维度组合或补构该分区 Segment |
| 实时接入延迟大 | Kafka Topic/Schema 与模型不匹配 | 对齐 Schema;调大批次/缓存;核对时间戳与水位 |
| RegionTooBusy/Timeout(HBase) | 区域热点或内存参数过小 | 预分区/冷热分离;调大 MemStore/BlockCache |
| 权限相关报错(Kerberos/ACL) | 集群开启安全认证 | 检查 keytab 与 principal;获取票据并配置 Kylin/Hive 客户端 |
其他系列
🚀 AI篇持续更新中(长期更新)
AI炼丹日志-29 - 字节跳动 DeerFlow 深度研究框斜体样式架 私有部署 测试上手 架构研究 ,持续打造实用AI工具指南! AI研究-127 Qwen2.5-Omni 深解:Thinker-Talker 双核、TMRoPE 与流式语音
💻 Java篇持续更新中(长期更新)
Java-174 FastFDS 从单机到分布式文件存储:实战与架构取舍 MyBatis 已完结,Spring 已完结,Nginx已完结,Tomcat已完结,分布式服务已完结,Dubbo已完结,MySQL已完结,MongoDB已完结,Neo4j已完结,FastDFS 正在更新,深入浅出助你打牢基础!
📊 大数据板块已完成多项干货更新(300篇):
包括 Hadoop、Hive、Kafka、Flink、ClickHouse、Elasticsearch 等二十余项核心组件,覆盖离线+实时数仓全栈! 大数据-278 Spark MLib - 基础介绍 机器学习算法 梯度提升树 GBDT案例 详解