(37)DWD 层(业务数据)

2024-01-22 23:08
文章标签 数据 业务 37 dwd

本文主要是介绍(37)DWD 层(业务数据),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

业务数据方面 DWD 层的搭建主要注意点在于维度建模,减少后续大量 Join 操作。

1. 商品维度表(全量)

商品维度表主要是将商品表 SKU 表、商品一级分类、商品二级分类、商品三级分类、
商品品牌表和商品 SPU 表联接为商品表。
1 )建表语句
hive (gmall)>
DROP TABLE IF EXISTS `dwd_dim_sku_info`;
CREATE EXTERNAL TABLE `dwd_dim_sku_info` (
`id` string COMMENT ' 商品 id',
`spu_id` string COMMENT 'spuid',
`price` decimal(16,2) COMMENT ' 商品价格 ',
`sku_name` string COMMENT ' 商品名称 ',
`sku_desc` string COMMENT ' 商品描述 ',
`weight` decimal(16,2) COMMENT ' 重量 ',
`tm_id` string COMMENT ' 品牌 id',
`tm_name` string COMMENT ' 品牌名称 ',
`category3_id` string COMMENT ' 三级分类 id',
`category2_id` string COMMENT ' 二级分类 id',
`category1_id` string COMMENT ' 一级分类 id',
`category3_name` string COMMENT ' 三级分类名称 ',
`category2_name` string COMMENT ' 二级分类名称 ',
`category1_name` string COMMENT ' 一级分类名称 ',
`spu_name` string COMMENT 'spu 名称 ',
`create_time` string COMMENT ' 创建时间 '
) COMMENT ' 商品维度表 '
PARTITIONED BY (`dt` string)
stored as parquet
location '/warehouse/gmall/dwd/dwd_dim_sku_info/'
tblproperties ("parquet.compression"="lzo");

 2)数据装载

hive (gmall)>
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
insert overwrite table dwd_dim_sku_info partition(dt='2020-06-14')
select
sku.id,
sku.spu_id,
sku.price,
sku.sku_name,
sku.sku_desc,
sku.weight,
sku.tm_id,
ob.tm_name,
sku.category3_id,
c2.id category2_id,
c1.id category1_id,
c3.name category3_name,
c2.name category2_name,
c1.name category1_name,
spu.spu_name,
sku.create_time
from
(
select * from ods_sku_info where dt='2020-06-14'
)sku
join
(
select * from ods_base_trademark where dt='2020-06-14'
)ob on sku.tm_id=ob.tm_id
join
(
select * from ods_spu_info where dt='2020-06-14'
)spu on spu.id = sku.spu_id
join
(
select * from ods_base_category3 where dt='2020-06-14'
)c3 on sku.category3_id=c3.id
join
(
select * from ods_base_category2 where dt='2020-06-14'
)c2 on c3.category2_id=c2.id
join
(
select * from ods_base_category1 where dt='2020-06-14'
)c1 on c2.category1_id=c1.id;

 

3 )查询加载结果
hive (gmall)> select * from dwd_dim_sku_info where dt='2020-06-14' limit 2;
2.优惠券维度表(全量)
ODS ods_coupon_info 表数据导入到 DWD 层优惠卷维度表,在导入过程中可以做
适当的清洗。
1 )建表语句
hive (gmall)>
drop table if exists dwd_dim_coupon_info;
create external table dwd_dim_coupon_info(
`id` string COMMENT ' 购物券编号 ',
`coupon_name` string COMMENT ' 购物券名称 ',
`coupon_type` string COMMENT ' 购物券类型 1 现金券 2 折扣券 3 满减券 4 满件打折券 ',
`condition_amount` decimal(16,2) COMMENT ' 满额数 ',
`condition_num` bigint COMMENT ' 满件数 ',
`activity_id` string COMMENT ' 活动编号 ',
`benefit_amount` decimal(16,2) COMMENT ' 减金额 ',
`benefit_discount` decimal(16,2) COMMENT ' 折扣 ',
`create_time` string COMMENT ' 创建时间 ',
`range_type` string COMMENT ' 范围类型 1 、商品 2 、品类 3 、品牌 ',
`spu_id` string COMMENT ' 商品 id',
`tm_id` string COMMENT ' 品牌 id',
`category3_id` string COMMENT ' 品类 id',
`limit_num` bigint COMMENT ' 最多领用次数 ',
`operate_time` string COMMENT ' 修改时间 ',
`expire_time` string COMMENT ' 过期时间 '
) COMMENT ' 优惠券维度表 '
PARTITIONED BY (`dt` string)
stored as parquet
location '/warehouse/gmall/dwd/dwd_dim_coupon_info/'
tblproperties ("parquet.compression"="lzo");
2 )数据装载
hive (gmall)>
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
insert overwrite table dwd_dim_coupon_info partition(dt='2020-06-14')
select
id,
coupon_name,
coupon_type,
condition_amount,
condition_num,
activity_id,
benefit_amount,
benefit_discount,
create_time,
range_type,
spu_id,
tm_id,
category3_id,
limit_num,
operate_time,
expire_time
from ods_coupon_info
where dt='2020-06-14';
3 )查询加载结果
hive (gmall)> select * from dwd_dim_coupon_info where dt='2020-06-14' limit 2;
3 活动维度表(全量)
1 )建表语句
hive (gmall)>
drop table if exists dwd_dim_activity_info;
create external table dwd_dim_activity_info(
`id` string COMMENT ' 编号 ',
`activity_name` string COMMENT ' 活动名称 ',
`activity_type` string COMMENT ' 活动类型 ',
`start_time` string COMMENT ' 开始时间 ',
`end_time` string COMMENT ' 结束时间 ',
`create_time` string COMMENT ' 创建时间 '
) COMMENT ' 活动信息表 '
PARTITIONED BY (`dt` string)
stored as parquet
location '/warehouse/gmall/dwd/dwd_dim_activity_info/'
tblproperties ("parquet.compression"="lzo");
2 )数据装载
hive (gmall)>
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
insert overwrite table dwd_dim_activity_info partition(dt='2020-06-14')
select
id,
activity_name,
activity_type,
start_time,
end_time,
create_time
from ods_activity_info
where dt='2020-06-14';
3 )查询加载结果
hive (gmall)> select * from dwd_dim_activity_info where dt='2020-06-14' limit 2;
4 地区维度表(特殊)
1 )建表语句
hive (gmall)>
DROP TABLE IF EXISTS `dwd_dim_base_province`;
CREATE EXTERNAL TABLE `dwd_dim_base_province` (
`id` string COMMENT 'id',
`province_name` string COMMENT ' 省市名称 ',
`area_code` string COMMENT ' 地区编码 ',
`iso_code` string COMMENT 'ISO 编码 ',
`region_id` string COMMENT ' 地区 id',
`region_name` string COMMENT ' 地区名称 '
) COMMENT ' 地区维度表 '
stored as parquet
location '/warehouse/gmall/dwd/dwd_dim_base_province/'
tblproperties ("parquet.compression"="lzo");
2 )数据装载
hive (gmall)>
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
insert overwrite table dwd_dim_base_province
select
bp.id,
bp.name,
bp.area_code,
bp.iso_code,
bp.region_id,
br.region_name
from
(
select * from ods_base_province
) bp
join
(
select * from ods_base_region
) br
on bp.region_id = br.id;
3 )查询加载结果
hive (gmall)> select * from dwd_dim_base_province limit 2;
5 时间维度表(特殊)
1 )建表语句
hive (gmall)>
DROP TABLE IF EXISTS `dwd_dim_date_info`;
CREATE EXTERNAL TABLE `dwd_dim_date_info`(
`date_id` string COMMENT ' ',
`week_id` string COMMENT ' ',
`week_day` string COMMENT ' 周的第几天 ',
`day` string COMMENT ' 每月的第几天 ',
`month` string COMMENT ' 第几月 ',
`quarter` string COMMENT ' 第几季度 ',
`year` string COMMENT ' ',
`is_workday` string COMMENT ' 是否是周末 ',
`holiday_id` string COMMENT ' 是否是节假日 '
) COMMENT ' 时间维度表 '
stored as parquet
location '/warehouse/gmall/dwd/dwd_dim_date_info/'
tblproperties ("parquet.compression"="lzo");
2 )把 date_info.txt 文件上传到 hadoop102 /opt/module/db_log/ 路径
3 )数据装载
注意:由于 dwd_dim_date_info 是列式存储 +LZO 压缩。直接将 date_info.txt 文件导入到
目标表,并不会直接转换为列式存储 +LZO 压缩。我们需要创建一张普通的临时表
dwd_dim_date_info_tmp ,将 date_info.txt 加载到该临时表中。最后通过查询临时表数据,把
查询到的数据插入到最终的目标表中。
1 )创建临时表,非列式存储
hive (gmall)>
DROP TABLE IF EXISTS `dwd_dim_date_info_tmp`;
CREATE EXTERNAL TABLE `dwd_dim_date_info_tmp`(
`date_id` string COMMENT ' ',
`week_id` string COMMENT ' ',
`week_day` string COMMENT ' 周的第几天 ',
`day` string COMMENT ' 每月的第几天 ',
`month` string COMMENT ' 第几月 ',
`quarter` string COMMENT ' 第几季度 ',
`year` string COMMENT ' ',
`is_workday` string COMMENT ' 是否是周末 ',
`holiday_id` string COMMENT ' 是否是节假日 '
) COMMENT ' 时间临时表 '
row format delimited fields terminated by '\t'
location '/warehouse/gmall/dwd/dwd_dim_date_info_tmp/';
2 )将数据导入临时表
hive (gmall)>
load data
local
inpath '/opt/module/db_log/date_info.txt' into table
dwd_dim_date_info_tmp;
3 )将数据导入正式表
hive (gmall)>
insert overwrite table dwd_dim_date_info select * from dwd_dim_date_info_tmp;
4 )查询加载结果
hive (gmall)> select * from dwd_dim_date_info;

这篇关于(37)DWD 层(业务数据)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/634528

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元