大家好,欢迎来到本篇博客,博主是一名刚入大数据行业的小白,利用空闲的时间来分享自己所学的知识,帮助和博主一样刚处于起步阶段的同学,水平不高,若有什么错误和纰漏之处恳请大佬不吝赐教,目前个人博客只有CSDN:
https://zhenyu.blog.csdn.net/
,感谢大家的支持,谢谢
送给大家一句话:今日事,今日毕本篇博客主要讲解:入门使用案例
目标:
- 学习Kylin的基本使用
Kylin
测试数据表结构介绍
dw_sales:事实表
列名 | 列类型 | 说明 |
---|---|---|
id | string | 订单id |
date1 | string | 订单日期 |
channelid | string | 订单渠道(商场、京东、天猫) |
productid | string | 产品id |
regionid | string | 区域名称 |
amount | int | 商品下单数量 |
price | double | 商品金额 |
dim_channel:维度表_渠道方式
列名 | 列类型 | 说明 |
---|---|---|
channelid | string | 渠道id |
channelname | string | 渠道名称 |
dim_product:维度表_产品名称
列名 | 列类型 | 说明 |
---|---|---|
productid | string | 产品id |
productname | string | 产品名称 |
dim_region:维度表_区域
列名 | 类类型 | 说明 |
---|---|---|
regionid | string | 区域id |
regionname | string | 区域名称 |
导入测试数据
------为了方便后续学习Kylin的使用,需要准备一些测试表,测试数据
- Hive中创建表
- 将数据从本地文件导入到Hive
操作步骤
- 使用 beeline 连接Hive:
!connect jdbc:hive2://node1:10000
- 创建并切换到 itcast_dw 数据库:
create database kylin_dw;
- 执行语句
-- 查看数据库是否创建成功
show database;
use kylin_dw;
--创建表
-- 1、销售表:dw_sales
-- id 唯一标识
-- date1 日期
-- channelId 渠道 ID
-- productId 产品 ID
-- regionId 区域 ID
-- amount 数量
-- price 金额
create table dw_sales(id string,date1 string,channelId string, productId string, regionId string,amount int,price double)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile;
-- 2、渠道表:dim_channel
-- channelId 渠道ID
-- channelName 渠道名称
create table dim_channel(channelId string, channelName string )ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile;
-- 3、产品表:dim_product
create table dim_product(productId string, productName string )ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile;
--4、区域表:dim_region
create table dim_region(regionId string,regionName string)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile;
- 文件
dw_sales_data.txt
0001,2019-02-01,01,01,010,1,3400.00
0002,2019-02-01,02,02,021,2,6800.00
0003,2019-02-01,01,01,010,1,3400.00
0004,2019-02-01,01,02,021,1,3400.00
0005,2019-02-01,02,01,010,1,3400.00
0006,2019-02-01,01,01,021,2,6800.00
0007,2019-02-01,03,02,010,1,3400.00
0008,2019-02-01,01,01,021,1,3400.00
0009,2019-02-01,01,03,010,1,3400.00
0010,2019-02-01,02,01,021,3,10200.00
0011,2019-02-01,01,04,010,1,3400.00
0012,2019-02-01,03,01,021,1,3400.00
0013,2019-02-01,01,04,010,1,3400.00
0014,2019-02-02,01,01,010,1,3400.00
0015,2019-02-02,02,02,021,2,6800.00
0016,2019-02-02,01,01,010,1,3400.00
0017,2019-02-02,01,02,021,1,3400.00
0018,2019-02-02,02,01,010,1,3400.00
0019,2019-02-02,01,01,021,2,6800.00
0020,2019-02-02,03,02,010,1,3400.00
0021,2019-02-02,01,01,021,1,3400.00
0022,2019-02-02,01,03,010,1,3400.00
0023,2019-02-02,02,01,021,3,10200.00
0024,2019-02-02,01,04,010,1,3400.00
0025,2019-02-02,03,01,021,1,3400.00
0026,2019-02-02,01,04,010,1,3400.00
0027,2019-02-02,01,04,010,1,3400.00
-----------------------------------------------------------------------------------------
dim_channel_data.txt
01,商场
02,京东
03,天猫
-----------------------------------------------------------------------------------------
dim_product_data.txt
01,meta20
02,p30
03,ihpone Xs
04,小米 9
-----------------------------------------------------------------------------------------
dim_region_data.txt
010,北京
021,上海
- 数据加载
-- 导入数据
LOAD DATA LOCAL INPATH '/hivefile/testkylin/dw_sales_data.txt' OVERWRITE INTO TABLE dw_sales;
LOAD DATA LOCAL INPATH '/hivefile/testkylin/dim_channel_data.txt' OVERWRITE INTO TABLE dim_channel;
LOAD DATA LOCAL INPATH '/hivefile/testkylin/dim_product_data.txt' OVERWRITE INTO TABLE dim_product;
LOAD DATA LOCAL INPATH '/hivefile/testkylin/dim_region_data.txt' OVERWRITE INTO TABLE dim_region;
- 执行一条SQL语句,确认数据是否已经成功导入
select * from dw_sales;
Kylin一张表入门练习
按照日期统计订单总额/总数量
操作步骤:
- 使用beeline连接Hive
- 切换到kylin_dw数据库
- 编写SQL语句
具体步骤:
-
使用beeline连接Hive
-
切换到kylin_dw数据库
use itcast_kylin_dw
- 在代码目录中创建sql文件,编写SQL语句
select date1, sum(price) as total_money, sum(amount) as total_amount from dw_sales group by date1;
按照日期统计订单总额/总数量(Kylin方式)
要使用Kylin进行OLAP分析,需要按照以下方式来进行
-
创建项目(Project)
-
创建数据源(DataSource)
- 指定有哪些数据需要进行数据分析
-
创建模型(Model)
- 指定具体要对哪个事实表、那些维度进行数据分析
-
创建立方体(Cube)
- 指定对哪个数据模型执行数据预处理,生成不同维度的数据
-
执行构建、等待构建完成
-
再执行SQL查询,获取结果
- 从Cube中查询数据
操作步骤
创建项目
创建项目(Project) |
---|
![]() |
![]() ![]() |
创建数据源(DataSource)
将Hive中的表都导入到Kylin中 在添加表名中,指定数据库名 kylin_dw.dim_channel kylin_dw.dim_product kylin_dw.dim_region kylin_dw.dw_sales |
---|
Load Table From Tree |
![]() |
![]() ![]() ![]() |
创建模型(Model)
1. 指定模型名称 |
---|
![]() |
![]() |
2. 指定事实表 |
---|
![]() |
3. 指定维度表 |
---|
![]() |
4.指定待分析的指标 |
---|
![]() |
5. 指定分区和过滤条件 |
---|
![]() |
6.创捷好的Model |
---|
![]() |
创建立方体(Cube)
1.选择数据模型 |
---|
![]() |
Cube一定是在创建Model之后在创建,因为他有一个所属哪个MOdel的问题 |
![]() |
2.指定维度 |
---|
![]() |
![]() |
![]() |
3.指定度量 |
---|
![]() |
![]() |
![]() |
4. 指定刷新设置 |
----- |
![]() |
5.指定执行引擎 |
---|
![]() |
执行构建
- 点击Bulid之后
执行SQL语句分析
在Insight选项卡中,执行以下SQL语句
select
date1,
sum(price) as total_money,
sum(amount) as total_amount
from
dw_sales
group by date1;
Kylin多张表练习
按照订单渠道名称统计订单总额/总数量
1.创建Model |
---|
![]() |
![]() |
点击Add Lookup Table:指定关联表 |
![]() |
![]() |
指定两张表的关联条件 |
![]() |
注意:Skip snapshot for this lookup table |
![]() |
![]() |
指定维度 |
![]() |
指定度量 |
![]() |
指定分区 |
![]() |
保存结果 |
![]() |
2.创建Cube |
![]() |
![]() |
添加维度 |
![]() |
![]() |
![]() |
指定度量 |
![]() |
![]() |
![]() |
指定引擎 |
![]() |
这里可以看到一些设置![]() |
Bulid |
![]() |
![]() |
select
t2.channelid,
t2.channelname,
sum(t1.price) as total_money,
sum(t1.amount) as total_amount
from
dw_sales t1
inner join dim_channel t2
on t1.channelid = t2.channelid
group by t2.channelid, t2.channelname
结果 |
---|
![]() |
按照日期、区域、产品维度统计订单总额/总数量
1.创建Model |
---|
![]() |
![]() |
![]() |
选择关联表 |
![]() |
![]() |
![]() |
选择维度 |
![]() |
选择度量 |
![]() |
2.创建Cube |
![]() |
![]() |
![]() |
![]() |
引擎 |
![]() |
Build |
![]() |
查看进度 |
![]() |
查询结果 |
![]() |
select
t1.date1,
t2.regionid,
t2.regionname,
t3.productid,
t3.productname,
sum(t1.price) as total_money,
sum(t1.amount) as total_amount
from
dw_sales t1
inner join dim_region t2
on t1.regionid = t2.regionid
inner join dim_product t3
on t1.productid = t3.productid
group by
t1.date1,
t2.regionid,
t2.regionname,
t3.productid,
t3.productname
order by
t1.date1,
t2.regionname,
t3.productname
小结
--------Kylin的入门案例实现到这里就结束啦
感谢大家的支持,若有什么不正确的地方还请大家能及时的反馈,记得点赞收藏支持一下!