分享

企业级数据仓库构建(八):搭建DWD 层-业务数据

本帖最后由 hanyunsong 于 2020-9-18 10:57 编辑

问题导读:

1. 拉链表是什么?
2. 为什么要做拉链表?
3. 拉链表的制作过程是怎么样的?



上一篇:企业级数据仓库构建(七):搭建DWD 层

1.1 DWD 层(业务数据)

20200504174024741.png

1.1.1 商品维度表(全量表)

20200505094740738.png

1)建表语句
  1. DROP TABLE IF EXISTS `dwd_dim_sku_info`;
  2. CREATE EXTERNAL TABLE `dwd_dim_sku_info` (
  3. `id` string COMMENT '商品 id',
  4. `spu_id` string COMMENT 'spuid',
  5. `price` double COMMENT '商品价格',
  6. `sku_name` string COMMENT '商品名称',
  7. `sku_desc` string COMMENT '商品描述',
  8. `weight` double COMMENT '重量',
  9. `tm_id` string COMMENT '品牌 id',
  10. `tm_name` string COMMENT '品牌名称',
  11. `category3_id` string COMMENT '三级分类 id',
  12. `category2_id` string COMMENT '二级分类 id',
  13. `category1_id` string COMMENT '一级分类 id',
  14. `category3_name` string COMMENT '三级分类名称',
  15. `category2_name` string COMMENT '二级分类名称',
  16. `category1_name` string COMMENT '一级分类名称',
  17. `spu_name` string COMMENT 'spu 名称',
  18. `create_time` string COMMENT '创建时间'
  19. )
  20. COMMENT '商品维度表'
  21. PARTITIONED BY (`dt` string)
  22. stored as parquet
  23. location '/warehouse/gmall/dwd/dwd_dim_sku_info/'
  24. tblproperties ("parquet.compression"="lzo");
复制代码
2)数据装载
  1. insert overwrite table dwd_dim_sku_info partition(dt='2020-03-10')
  2. select
  3. sku.id,
  4. sku.spu_id,
  5. sku.price,
  6. sku.sku_name,
  7. sku.sku_desc,
  8. sku.weight,
  9. sku.tm_id,
  10. ob.tm_name,
  11. sku.category3_id,
  12. c2.id category2_id,
  13. c1.id category1_id,
  14. c3.name category3_name,
  15. c2.name category2_name,
  16. c1.name category1_name,
  17. spu.spu_name,
  18. sku.create_time
  19. from
  20. (
  21. select * from ods_sku_info where dt='2020-03-10'
  22. )sku
  23. join
  24. (
  25. select * from ods_base_trademark where dt='2020-03-10'
  26. )ob on sku.tm_id=ob.tm_id
  27. join
  28. (
  29. select * from ods_spu_info where dt='2020-03-10'
  30. )spu on spu.id = sku.spu_id
  31. join
  32. (
  33. select * from ods_base_category3 where dt='2020-03-10'
  34. )c3 on sku.category3_id=c3.id
  35. join
  36. (
  37. select * from ods_base_category2 where dt='2020-03-10'
  38. )c2 on c3.category2_id=c2.id
  39. join
  40. (
  41. select * from ods_base_category1 where dt='2020-03-10'
  42. )c1 on c2.category1_id=c1.id;
复制代码
3)查询加载结果
  1. select * from dwd_dim_sku_info where dt='2020-03-10';
复制代码
1.1.2 优惠券信息表(全量)

把 ODS 层 ods_coupon_info 表数据导入到 DWD 层优惠卷信息表,在导入过程中可以做适当的清洗

1)建表语句
  1. drop table if exists dwd_dim_coupon_info;
  2. create external table dwd_dim_coupon_info(
  3. `id` string COMMENT '购物券编号',
  4. `coupon_name` string COMMENT '购物券名称',
  5. `coupon_type` string COMMENT '购物券类型 1 现金券 2 折扣券 3 满减券 4 满件打折券',
  6. `condition_amount` string COMMENT '满额数',
  7. `condition_num` string COMMENT '满件数',
  8. `activity_id` string COMMENT '活动编号',
  9. `benefit_amount` string COMMENT '减金额',
  10. `benefit_discount` string COMMENT '折扣',
  11. `create_time` string COMMENT '创建时间',
  12. `range_type` string COMMENT '范围类型 1、商品 2、品类 3、品牌',
  13. `spu_id` string COMMENT '商品 id',
  14. `tm_id` string COMMENT '品牌 id',
  15. `category3_id` string COMMENT '品类 id',
  16. `limit_num` string COMMENT '最多领用次数',
  17. `operate_time` string COMMENT '修改时间',
  18. `expire_time` string COMMENT '过期时间'
  19. ) COMMENT '优惠券信息表'
  20. PARTITIONED BY (`dt` string)
  21. row format delimited fields terminated by '\t'
  22. stored as parquet
  23. location '/warehouse/gmall/dwd/dwd_dim_coupon_info/'
  24. tblproperties ("parquet.compression"="lzo");
复制代码
2)数据装载
  1. insert overwrite table dwd_dim_coupon_info partition(dt='2020-03-10')
  2. select
  3. id,
  4. coupon_name,
  5. coupon_type,
  6. condition_amount,
  7. condition_num,
  8. activity_id,
  9. benefit_amount,
  10. benefit_discount,
  11. create_time,
  12. range_type,
  13. spu_id,
  14. tm_id,
  15. category3_id,
  16. limit_num,
  17. operate_time,
  18. expire_time
  19. from ods_coupon_info
  20. where dt='2020-03-10';
复制代码
3)查询加载结果
  1. select * from dwd_dim_coupon_info where dt='2020-03-10';
复制代码
1.1.3 活动维度表(全量)

20200505095109257.png

1)建表语句
  1. drop table if exists dwd_dim_activity_info;
  2. create external table dwd_dim_activity_info(
  3. `id` string COMMENT '编号',
  4. `activity_name` string COMMENT '活动名称',
  5. `activity_type` string COMMENT '活动类型',
  6. `condition_amount` string COMMENT '满减金额',
  7. `condition_num` string COMMENT '满减件数',
  8. `benefit_amount` string COMMENT '优惠金额',
  9. `benefit_discount` string COMMENT '优惠折扣',
  10. `benefit_level` string COMMENT '优惠级别',
  11. `start_time` string COMMENT '开始时间',
  12. `end_time` string COMMENT '结束时间',
  13. `create_time` string COMMENT '创建时间'
  14. ) COMMENT '活动信息表'
  15. PARTITIONED BY (`dt` string)
  16. row format delimited fields terminated by '\t'
  17. stored as parquet
  18. location '/warehouse/gmall/dwd/dwd_dim_activity_info/'
  19. tblproperties ("parquet.compression"="lzo");
复制代码
2)数据装载
  1. insert overwrite table dwd_dim_activity_info partition(dt='2020-03-10')
  2. select
  3. info.id,
  4. info.activity_name,
  5. info.activity_type,
  6. rule.condition_amount,
  7. rule.condition_num,
  8. rule.benefit_amount,
  9. rule.benefit_discount,
  10. rule.benefit_level,
  11. info.start_time,
  12. info.end_time,
  13. info.create_time
  14. from
  15. (
  16. select * from ods_activity_info where dt='2020-03-10'
  17. )info
  18. left join
  19. (
  20. select * from ods_activity_rule where dt='2020-03-10'
  21. )rule on info.id = rule.activity_id;
复制代码
3)查询加载结果
  1. select * from dwd_dim_activity_info where dt='2020-03-10';
复制代码
1.1.4 地区维度表(特殊)

20200505095217326.png

1)建表语句
  1. DROP TABLE IF EXISTS `dwd_dim_base_province`;
  2. CREATE EXTERNAL TABLE `dwd_dim_base_province` (
  3. `id` string COMMENT 'id',
  4. `province_name` string COMMENT '省市名称',
  5. `area_code` string COMMENT '地区编码',
  6. `iso_code` string COMMENT 'ISO 编码',
  7. `region_id` string COMMENT '地区 id',
  8. `region_name` string COMMENT '地区名称'
  9. )
  10. COMMENT '地区省市表'
  11. stored as parquet
  12. location '/warehouse/gmall/dwd/dwd_dim_base_province/'
  13. tblproperties ("parquet.compression"="lzo");
复制代码
2)数据装载
  1. insert overwrite table dwd_dim_base_province
  2. select
  3. bp.id,
  4. bp.name,
  5. bp.area_code,
  6. bp.iso_code,
  7. bp.region_id,
  8. br.region_name
  9. from ods_base_province bp
  10. join ods_base_region br
  11. on bp.region_id=br.id;
复制代码
1.1.5 时间维度表(特殊)(预留)

1)建表语句
  1. DROP TABLE IF EXISTS `dwd_dim_date_info`;
  2. CREATE EXTERNAL TABLE `dwd_dim_date_info`(
  3. `date_id` string COMMENT '日',
  4. `week_id` int COMMENT '周',
  5. `week_day` int COMMENT '周的第几天',
  6. `day` int COMMENT '每月的第几天',
  7. `month` int COMMENT '第几月',
  8. `quarter` int COMMENT '第几季度',
  9. `year` int COMMENT '年',
  10. `is_workday` int COMMENT '是否是周末',
  11. `holiday_id` int COMMENT '是否是节假日'
  12. )
  13. row format delimited fields terminated by '\t'
  14. stored as parquet
  15. location '/warehouse/gmall/dwd/dwd_dim_date_info/'
  16. tblproperties ("parquet.compression"="lzo");
复制代码
2)把 date_info.txt 文件上传到 node01 的 /opt/modules/db_log/路径

3)数据装载
  1. load data local inpath '/opt/modules/db_log/date_info.txt' into table dwd_dim_date_info;
复制代码
4)查询加载结果
  1. select * from dwd_dim_date_info;
复制代码
1.1.6 订单明细事实表(事务型快照事实表)

20200505143316662.png

20200505143333201.png

1)建表语句
  1. drop table if exists dwd_fact_order_detail;
  2. create external table dwd_fact_order_detail (
  3. `id` string COMMENT '订单编号',
  4. `order_id` string COMMENT '订单号',
  5. `user_id` string COMMENT '用户 id',
  6. `sku_id` string COMMENT 'sku 商品 id',
  7. `sku_name` string COMMENT '商品名称',
  8. `order_price` decimal(10,2) COMMENT '商品价格',
  9. `sku_num` bigint COMMENT '商品数量',
  10. `create_time` string COMMENT '创建时间',
  11. `province_id` string COMMENT '省份 ID',
  12. `total_amount` decimal(20,2) COMMENT '订单总金额'
  13. )
  14. PARTITIONED BY (`dt` string)
  15. stored as parquet
  16. location '/warehouse/gmall/dwd/dwd_fact_order_detail/'
  17. tblproperties ("parquet.compression"="lzo");
复制代码
2)数据装载
  1. insert overwrite table dwd_fact_order_detail partition(dt='2020-03-10')
  2. select
  3. od.id,
  4. od.order_id,
  5. od.user_id,
  6. od.sku_id,
  7. od.sku_name,
  8. od.order_price,
  9. od.sku_num,
  10. od.create_time,
  11. oi.province_id,
  12. od.order_price*od.sku_num
  13. from
  14. (
  15. select * from ods_order_detail where dt='2020-03-10'
  16. ) od
  17. join
  18. (
  19. select * from ods_order_info where dt='2020-03-10'
  20. ) oi
  21. on od.order_id=oi.id;
复制代码
3)查询加载结果
  1. select * from dwd_fact_order_detail where dt='2020-03-10';
复制代码
1.1.7 支付事实表(事务型快照事实表)

20200505143521719.png
20200505143535593.png

1)建表语句
  1. drop table if exists dwd_fact_payment_info;
  2. create external table dwd_fact_payment_info (
  3. `id` string COMMENT '',
  4. `out_trade_no` string COMMENT '对外业务编号',
  5. `order_id` string COMMENT '订单编号',
  6. `user_id` string COMMENT '用户编号',
  7. `alipay_trade_no` string COMMENT '支付宝交易流水编号',
  8. `payment_amount` decimal(16,2) COMMENT '支付金额',
  9. `subject` string COMMENT '交易内容',
  10. `payment_type` string COMMENT '支付类型',
  11. `payment_time` string COMMENT '支付时间',
  12. `province_id` string COMMENT '省份 ID'
  13. )
  14. PARTITIONED BY (`dt` string)
  15. stored as parquet
  16. location '/warehouse/gmall/dwd/dwd_fact_payment_info/'
  17. tblproperties ("parquet.compression"="lzo");
复制代码
2)数据装载
  1. insert overwrite table dwd_fact_payment_info partition(dt='2020-03-10')
  2. select
  3. pi.id,
  4. pi.out_trade_no,
  5. pi.order_id,
  6. pi.user_id,
  7. pi.alipay_trade_no,
  8. pi.total_amount,
  9. pi.subject,
  10. pi.payment_type,
  11. pi.payment_time,
  12. oi.province_id
  13. from
  14. (
  15. select * from ods_payment_info where dt='2020-03-10'
  16. )pi
  17. join
  18. (
  19. select id, province_id from ods_order_info where dt='2020-03-10'
  20. )oi
  21. on pi.order_id = oi.id;
复制代码
3)查询加载结果
  1. select * from dwd_fact_payment_info where dt='2020-03-10';
复制代码
1.1.8 退款事实表(事务型快照事实表)

把 ODS 层 ods_order_refund_info 表数据导入到 DWD 层退款事实表,在导入过程中可以做适当的清洗

2020050514371795.png

1)建表语句
  1. drop table if exists dwd_fact_order_refund_info;
  2. create external table dwd_fact_order_refund_info(
  3. `id` string COMMENT '编号',
  4. `user_id` string COMMENT '用户 ID',
  5. `order_id` string COMMENT '订单 ID',
  6. `sku_id` string COMMENT '商品 ID',
  7. `refund_type` string COMMENT '退款类型',
  8. `refund_num` bigint COMMENT '退款件数',
  9. `refund_amount` decimal(16,2) COMMENT '退款金额',
  10. `refund_reason_type` string COMMENT '退款原因类型',
  11. `create_time` string COMMENT '退款时间'
  12. ) COMMENT '退款事实表'
  13. PARTITIONED BY (`dt` string)
  14. row format delimited fields terminated by '\t'
  15. location '/warehouse/gmall/dwd/dwd_fact_order_refund_info/';
复制代码
2)数据装载
  1. insert overwrite table dwd_fact_order_refund_info partition(dt='2020-03-10')
  2. select
  3. id,
  4. user_id,
  5. order_id,
  6. sku_id,
  7. refund_type,
  8. refund_num,
  9. refund_amount,
  10. refund_reason_type,
  11. create_time
  12. from ods_order_refund_info
  13. where dt='2020-03-10';
复制代码
3)查询加载结果
  1. select * from dwd_fact_order_refund_info where dt='2020-03-10';
复制代码
1.1.9 评价事实表(事务型快照事实表)

把 ODS 层 ods_comment_info 表数据导入到 DWD 层评价事实表,在导入过程中可以做适当的清洗

20200505143849205.png

1)建表语句
  1. drop table if exists dwd_fact_comment_info;
  2. create external table dwd_fact_comment_info(
  3. `id` string COMMENT '编号',
  4. `user_id` string COMMENT '用户 ID',
  5. `sku_id` string COMMENT '商品 sku',
  6. `spu_id` string COMMENT '商品 spu',
  7. `order_id` string COMMENT '订单 ID',
  8. `appraise` string COMMENT '评价',
  9. `create_time` string COMMENT '评价时间'
  10. ) COMMENT '评价事实表'
  11. PARTITIONED BY (`dt` string)
  12. row format delimited fields terminated by '\t'
  13. location '/warehouse/gmall/dwd/dwd_fact_comment_info/';
复制代码
2)数据装载
  1. insert overwrite table dwd_fact_comment_info partition(dt='2020-03-10')
  2. select
  3. id,
  4. user_id,
  5. sku_id,
  6. spu_id,
  7. order_id,
  8. appraise,
  9. create_time
  10. from ods_comment_info
  11. where dt='2020-03-10';
复制代码
3)查询加载结果
  1. select * from dwd_fact_comment_info where dt='2020-03-10';
复制代码
1.1.10 加购事实表(周期型快照事实表,每日快照)

由于购物车的数量是会发生变化,所以导增量不合适
每天做一次快照,导入的数据是全量,区别于事务型事实表是每天导入新增
周期型快照事实表劣势:存储的数据量会比较大
解决方案:周期型快照事实表存储的数据比较讲究时效性,时间太久了的意义不大,可以删除以前的数据

20200505144038298.png

1)建表语句
  1. drop table if exists dwd_fact_cart_info;
  2. create external table dwd_fact_cart_info(
  3. `id` string COMMENT '编号',
  4. `user_id` string COMMENT '用户 id',
  5. `sku_id` string COMMENT 'skuid',
  6. `cart_price` string COMMENT '放入购物车时价格',
  7. `sku_num` string COMMENT '数量',
  8. `sku_name` string COMMENT 'sku 名称 (冗余)',
  9. `create_time` string COMMENT '创建时间',
  10. `operate_time` string COMMENT '修改时间',
  11. `is_ordered` string COMMENT '是否已经下单。1 为已下单;0 为未下单',
  12. `order_time` string COMMENT '下单时间'
  13. ) COMMENT '加购事实表'
  14. PARTITIONED BY (`dt` string)
  15. row format delimited fields terminated by '\t'
  16. location '/warehouse/gmall/dwd/dwd_fact_cart_info/';
复制代码
2)数据装载
  1. insert overwrite table dwd_fact_cart_info partition(dt='2020-03-10')
  2. select
  3. id,
  4. user_id,
  5. sku_id,
  6. cart_price,
  7. sku_num,
  8. sku_name,
  9. create_time,
  10. operate_time,
  11. is_ordered,
  12. order_time
  13. from ods_cart_info
  14. where dt='2020-03-10';
复制代码
3)查询加载结果
  1. select * from dwd_fact_cart_info where dt='2020-03-10';
复制代码
1.1.11 收藏事实表(周期型快照事实表,每日快照)

收藏的标记,是否取消,会发生变化,做增量不合适
每天做一次快照,导入的数据是全量,区别于事务型事实表是每天导入新增

20200505144508707.png

1)建表语句
  1. drop table if exists dwd_fact_favor_info;
  2. create external table dwd_fact_favor_info(
  3. `id` string COMMENT '编号',
  4. `user_id` string COMMENT '用户 id',
  5. `sku_id` string COMMENT 'skuid',
  6. `spu_id` string COMMENT 'spuid',
  7. `is_cancel` string COMMENT '是否取消',
  8. `create_time` string COMMENT '收藏时间',
  9. `cancel_time` string COMMENT '取消时间'
  10. ) COMMENT '收藏事实表'
  11. PARTITIONED BY (`dt` string)
  12. row format delimited fields terminated by '\t'
  13. location '/warehouse/gmall/dwd/dwd_fact_favor_info/';
复制代码
2)数据装载
  1. insert overwrite table dwd_fact_favor_info partition(dt='2020-03-10')
  2. select
  3. id,
  4. user_id,
  5. sku_id,
  6. spu_id,
  7. is_cancel,
  8. create_time,
  9. cancel_time
  10. from ods_favor_info
  11. where dt='2020-03-10';
复制代码
3)查询加载结果
  1. select * from dwd_fact_favor_info where dt='2020-03-10';
复制代码
1.1.12 优惠券领用事实表(累积型快照事实表)

20200505144712257.png

优惠卷的生命周期:领取优惠卷-》用优惠卷下单-》优惠卷参与支付

累积型快照事实表使用:统计优惠卷领取次数、优惠卷下单次数、优惠卷参与支付次数

1)建表语句
  1. drop table if exists dwd_fact_coupon_use;
  2. create external table dwd_fact_coupon_use(
  3. `id` string COMMENT '编号',
  4. `coupon_id` string COMMENT '优惠券 ID',
  5. `user_id` string COMMENT 'userid',
  6. `order_id` string COMMENT '订单 id',
  7. `coupon_status` string COMMENT '优惠券状态',
  8. `get_time` string COMMENT '领取时间',
  9. `using_time` string COMMENT '使用时间(下单)',
  10. `used_time` string COMMENT '使用时间(支付)'
  11. ) COMMENT '优惠券领用事实表'
  12. PARTITIONED BY (`dt` string)
  13. row format delimited fields terminated by '\t'
  14. location '/warehouse/gmall/dwd/dwd_fact_coupon_use/';
复制代码
注意:dt 是按照优惠卷领用时间 get_time 做为分区

2)数据装载

20200505144843195.png

  1. set hive.exec.dynamic.partition.mode=nonstrict;
  2. insert overwrite table dwd_fact_coupon_use partition(dt)
  3. select
  4. if(new.id is null,old.id,new.id),
  5. if(new.coupon_id is null,old.coupon_id,new.coupon_id),
  6. if(new.user_id is null,old.user_id,new.user_id),
  7. if(new.order_id is null,old.order_id,new.order_id),
  8. if(new.coupon_status is null,old.coupon_status,new.coupon_status),
  9. if(new.get_time is null,old.get_time,new.get_time),
  10. if(new.using_time is null,old.using_time,new.using_time),
  11. if(new.used_time is null,old.used_time,new.used_time),
  12. date_format(if(new.get_time is null,old.get_time,new.get_time),'yyyy-MM-dd')
  13. from
  14. (
  15. select
  16. id,
  17. coupon_id,
  18. user_id,
  19. order_id,
  20. coupon_status,
  21. get_time,
  22. using_time,
  23. used_time
  24. from dwd_fact_coupon_use
  25. where dt in
  26. (
  27. select
  28. date_format(get_time,'yyyy-MM-dd')
  29. from ods_coupon_use
  30. where dt='2020-03-10'
  31. )
  32. )old
  33. full outer join
  34. (
  35. select
  36. id,
  37. coupon_id,
  38. user_id,
  39. order_id,
  40. coupon_status,
  41. get_time,
  42. using_time,
  43. used_time
  44. from ods_coupon_use
  45. where dt='2020-03-10'
  46. )new
  47. on old.id=new.id;
复制代码
3)查询加载结果
  1. select * from dwd_fact_coupon_use where dt='2020-03-10';
复制代码
1.1.13 订单事实表(累积型快照事实表)

1)concat 函数

concat 函数在连接字符串的时候,只要其中一个是 NULL,那么将返回 NULL
  1. hive> select concat('a','b');
  2. ab
  3. hive> select concat('a','b',null);
  4. NULL
复制代码
2)concat_ws 函数

concat_ws 函数在连接字符串的时候,只要有一个字符串不是 NULL,就不会返回 NULL。concat_ws 函数需要指定分隔符
  1. hive> select concat_ws('-','a','b');
  2. a-b
  3. hive> select concat_ws('-','a','b',null);
  4. a-b
  5. hive> select concat_ws('','a','b',null);
  6. ab
复制代码
3)STR_TO_MAP 函数

  • (1)语法描述

STR_TO_MAP(VARCHAR text, VARCHAR listDelimiter, VARCHAR keyValueDelimiter)

  • (2)功能描述

使用 listDelimiter 将 text 分隔成 K-V 对,然后使用 keyValueDelimiter 分隔每个 K-V 对,
组装成 MAP 返回。默认 listDelimiter 为( ,),keyValueDelimiter 为(=)。

  • (3)案例

str_to_map(‘1001=2020-03-10,1002=2020-03-10’, ‘,’ , ‘=’)
输出{“1001”:“2020-03-10”,“1002”:“2020-03-10”}

4)建表语句

20200505145132635.png

订单生命周期:创建时间=》支付时间=》取消时间=》完成时间=》退款时间=》退款完成时间

由于 ODS 层订单表只有创建时间和操作时间两个状态,不能表达所有时间含义,所以需要关联订单状态表。订单事实表里面增加了活动 id,所以需要关联活动订单表
  1. drop table if exists dwd_fact_order_info;
  2. create external table dwd_fact_order_info (
  3. `id` string COMMENT '订单编号',
  4. `order_status` string COMMENT '订单状态',
  5. `user_id` string COMMENT '用户 id',
  6. `out_trade_no` string COMMENT '支付流水号',
  7. `create_time` string COMMENT '创建时间(未支付状态)',
  8. `payment_time` string COMMENT '支付时间(已支付状态)',
  9. `cancel_time` string COMMENT '取消时间(已取消状态)',
  10. `finish_time` string COMMENT '完成时间(已完成状态)',
  11. `refund_time` string COMMENT '退款时间(退款中状态)',
  12. `refund_finish_time` string COMMENT '退款完成时间(退款完成状态)',
  13. `province_id` string COMMENT '省份 ID',
  14. `activity_id` string COMMENT '活动 ID',
  15. `original_total_amount` string COMMENT '原价金额',
  16. `benefit_reduce_amount` string COMMENT '优惠金额',
  17. `feight_fee` string COMMENT '运费',
  18. `final_total_amount` decimal(10,2) COMMENT '订单金额'
  19. )
  20. PARTITIONED BY (`dt` string)
  21. stored as parquet
  22. location '/warehouse/gmall/dwd/dwd_fact_order_info/'
  23. tblproperties ("parquet.compression"="lzo");
复制代码
5)数据装载

20200505145235320.png

5)常用函数

更多函数请点击博客【HIve】Hive入门解析(五)

6)数据装载
  1. set hive.exec.dynamic.partition.mode=nonstrict;
  2. insert overwrite table dwd_fact_order_info partition(dt)
  3. select
  4. if(new.id is null,old.id,new.id),
  5. if(new.order_status is null,old.order_status,new.order_status),
  6. if(new.user_id is null,old.user_id,new.user_id),
  7. if(new.out_trade_no is null,old.out_trade_no,new.out_trade_no),
  8. if(new.tms['1001'] is null,old.create_time,new.tms['1001']),--1001 对应未支付状态
  9. if(new.tms['1002'] is null,old.payment_time,new.tms['1002']),
  10. if(new.tms['1003'] is null,old.cancel_time,new.tms['1003']),
  11. if(new.tms['1004'] is null,old.finish_time,new.tms['1004']),
  12. if(new.tms['1005'] is null,old.refund_time,new.tms['1005']),
  13. if(new.tms['1006'] is null,old.refund_finish_time,new.tms['1006']),
  14. if(new.province_id is null,old.province_id,new.province_id),
  15. if(new.activity_id is null,old.activity_id,new.activity_id),
  16. if(new.original_total_amount is
  17. null,old.original_total_amount,new.original_total_amount),
  18. if(new.benefit_reduce_amount is
  19. null,old.benefit_reduce_amount,new.benefit_reduce_amount),
  20. if(new.feight_fee is null,old.feight_fee,new.feight_fee),
  21. if(new.final_total_amount is null,old.final_total_amount,new.final_total_amount),
  22. date_format(if(new.tms['1001'] is
  23. null,old.create_time,new.tms['1001']),'yyyy-MM-dd')
  24. from
  25. (
  26. select
  27. id,
  28. order_status,
  29. user_id,
  30. out_trade_no,
  31. create_time,
  32. payment_time,
  33. cancel_time,
  34. finish_time,
  35. refund_time,
  36. refund_finish_time,
  37. province_id,
  38. activity_id,
  39. original_total_amount,
  40. benefit_reduce_amount,
  41. feight_fee,
  42. final_total_amount
  43. from dwd_fact_order_info
  44. where dt
  45. in
  46. (
  47. select
  48. date_format(create_time,'yyyy-MM-dd')
  49. from ods_order_info
  50. where dt='2020-03-10'
  51. )
  52. )old
  53. full outer join
  54. (
  55. select
  56. info.id,
  57. info.order_status,
  58. info.user_id,
  59. info.out_trade_no,
  60. info.province_id,
  61. act.activity_id,
  62. log.tms,
  63. info.original_total_amount,
  64. info.benefit_reduce_amount,
  65. info.feight_fee,
  66. info.final_total_amount
  67. from
  68. (
  69. select
  70. order_id,
  71. str_to_map(concat_ws(',',collect_set(concat(order_status,'=',operate_time))),',','=')
  72. tms
  73. from ods_order_status_log
  74. where dt='2020-03-10'
  75. group by order_id
  76. )log
  77. join
  78. (
  79. select * from ods_order_info where dt='2020-03-10'
  80. )info
  81. on log.order_id=info.id
  82. left join
  83. (
  84. select * from ods_activity_order where dt='2020-03-10'
  85. )act
  86. on log.order_id=act.order_id
  87. )new
  88. on old.id=new.id;
复制代码
6)查询加载结果
  1. select * from dwd_fact_order_info where dt='2020-03-10';
复制代码
1.1.14 用户维度表(拉链表)

用户表中的数据每日既有可能新增,也有可能修改,但修改频率并不高,属于缓慢变化
维度,此处采用拉链表存储用户维度数据

1)什么是拉链表

2020050521051431.png

2)为什么要做拉链表

20200505210530597.png

20200505210624412.png

3)拉链表形成过程

20200505210705447.png

4)拉链表制作过程图

20200505210739381.png

5)拉链表制作过程

步骤 0:初始化拉链表(首次独立执行)

(1)建立拉链表
  1. drop table if exists dwd_dim_user_info_his;
  2. create external table dwd_dim_user_info_his(
  3. `id` string COMMENT '用户 id',
  4. `name` string COMMENT '姓名',
  5. `birthday` string COMMENT '生日',
  6. `gender` string COMMENT '性别',
  7. `email` string COMMENT '邮箱',
  8. `user_level` string COMMENT '用户等级',
  9. `create_time` string COMMENT '创建时间',
  10. `operate_time` string COMMENT '操作时间',
  11. `start_date` string COMMENT '有效开始日期',
  12. `end_date` string COMMENT '有效结束日期'
  13. ) COMMENT '订单拉链表'
  14. stored as parquet
  15. location '/warehouse/gmall/dwd/dwd_dim_user_info_his/'
  16. tblproperties ("parquet.compression"="lzo");
复制代码
(2)初始化拉链表
  1. insert overwrite table dwd_dim_user_info_his
  2. select
  3. id,
  4. name,
  5. birthday,
  6. gender,
  7. email,
  8. user_level,
  9. create_time,
  10. operate_time,
  11. '2020-03-10',
  12. '9999-99-99'
  13. from ods_user_info oi
  14. where oi.dt='2020-03-10';
复制代码
步骤 1:制作当日变动数据(包括新增,修改)每日执行

(1)如何获得每日变动表

  • a.最好表内有创建时间和变动时间(Lucky!)
  • b.如果没有,可以利用第三方工具监控比如 canal,监控 MySQL 的实时变化进行记录(麻烦)
  • c.逐行对比前后两天的数据,检查 md5(concat(全部有可能变化的字段))是否相同(low)
  • d.要求业务数据库提供变动流水(人品,颜值)

(2)因为 ods_order_info 本身导入过来就是新增变动明细的表,所以不用处理

  • a)数据库中新增 2020-03-11 一天的数据
  • b)通过 Sqoop 把 2020-03-11 日所有数据导入mysqlTohdfs.sh all 2020-03-11
  • c)ods 层数据导入hdfs_to_ods_db.sh all 2020-03-11

步骤 2:先合并变动信息,再追加新增信息,插入到临时表中

1)建立临时表
  1. drop table if exists dwd_dim_user_info_his_tmp;
  2. create external table dwd_dim_user_info_his_tmp(
  3. `id` string COMMENT '用户 id',
  4. `name` string COMMENT '姓名',
  5. `birthday` string COMMENT '生日',
  6. `gender` string COMMENT '性别',
  7. `email` string COMMENT '邮箱',
  8. `user_level` string COMMENT '用户等级',
  9. `create_time` string COMMENT '创建时间',
  10. `operate_time` string COMMENT '操作时间',
  11. `start_date` string COMMENT '有效开始日期',
  12. `end_date` string COMMENT '有效结束日期'
  13. ) COMMENT '订单拉链临时表'
  14. stored as parquet
  15. location '/warehouse/gmall/dwd/dwd_dim_user_info_his_tmp/'
  16. tblproperties ("parquet.compression"="lzo");
复制代码
2)导入脚本
  1. insert overwrite table dwd_dim_user_info_his_tmp
  2. select * from
  3. (
  4. select
  5. id,
  6. name,
  7. birthday,
  8. gender,
  9. email,
  10. user_level,
  11. create_time,
  12. operate_time,
  13. '2020-03-11' start_date,
  14. '9999-99-99' end_date
  15. from ods_user_info where dt='2020-03-11'
  16. union all
  17. select
  18. uh.id,
  19. uh.name,
  20. uh.birthday,
  21. uh.gender,
  22. uh.email,
  23. uh.user_level,
  24. uh.create_time,
  25. uh.operate_time,
  26. uh.start_date,
  27. if(ui.id is not null and uh.end_date='9999-99-99', date_add(ui.dt,-1),
  28. uh.end_date) end_date
  29. from dwd_dim_user_info_his uh left join
  30. (
  31. select
  32. *
  33. from ods_user_info
  34. where dt='2020-03-11'
  35. ) ui on uh.id=ui.id
  36. )his
  37. order by his.id, start_date;
复制代码
步骤 3:把临时表覆盖给拉链表

1)导入数据
  1. insert overwrite table dwd_dim_user_info_his
  2. select * from dwd_dim_user_info_his_tmp;
复制代码
2)查询导入数据
  1. select id, start_date, end_date from dwd_dim_user_info_his;
复制代码
1.1.15 DWD 层数据导入脚本

1)vim ods_to_dwd_db.sh
  1. #!/bin/bash
  2. APP=gmall
  3. hive=/opt/modules/hive/bin/hive
  4. # 如果是输入的日期按照取输入日期;如果没输入日期取当前时间的前一天
  5. if [ -n "$2" ] ;then
  6. do_date=$2
  7. else
  8. do_date=`date -d "-1 day" +%F`
  9. fi
  10. sql1="
  11. set hive.exec.dynamic.partition.mode=nonstrict;
  12. insert overwrite table ${APP}.dwd_dim_sku_info partition(dt='$do_date')
  13. select
  14. sku.id,
  15. sku.spu_id,
  16. sku.price,
  17. sku.sku_name,
  18. sku.sku_desc,
  19. sku.weight,
  20. sku.tm_id,
  21. ob.tm_name,
  22. sku.category3_id,
  23. c2.id category2_id,
  24. c1.id category1_id,
  25. c3.name category3_name,
  26. c2.name category2_name,
  27. c1.name category1_name,
  28. spu.spu_name,
  29. sku.create_time
  30. from
  31. (
  32. select * from ${APP}.ods_sku_info where dt='$do_date'
  33. )sku
  34. join
  35. (
  36. select * from ${APP}.ods_base_trademark where dt='$do_date'
  37. )ob on sku.tm_id=ob.tm_id
  38. join
  39. (
  40. select * from ${APP}.ods_spu_info where dt='$do_date'
  41. )spu on spu.id = sku.spu_id
  42. join
  43. (
  44. select * from ${APP}.ods_base_category3 where dt='$do_date'
  45. )c3 on sku.category3_id=c3.id
  46. join
  47. (
  48. select * from ${APP}.ods_base_category2 where dt='$do_date'
  49. )c2 on c3.category2_id=c2.id
  50. join
  51. (
  52. select * from ${APP}.ods_base_category1 where dt='$do_date'
  53. )c1 on c2.category1_id=c1.id;
  54. insert overwrite table ${APP}.dwd_dim_coupon_info partition(dt='$do_date')
  55. select
  56. id,
  57. coupon_name,
  58. coupon_type,
  59. condition_amount,
  60. condition_num,
  61. activity_id,
  62. benefit_amount,
  63. benefit_discount,
  64. create_time,
  65. range_type,
  66. spu_id,
  67. tm_id,
  68. category3_id,
  69. limit_num,
  70. operate_time,
  71. expire_time
  72. from ${APP}.ods_coupon_info
  73. where dt='$do_date';
  74. insert overwrite table ${APP}.dwd_dim_activity_info partition(dt='$do_date')
  75. select
  76. info.id,
  77. info.activity_name,
  78. info.activity_type,
  79. rule.condition_amount,
  80. rule.condition_num,
  81. rule.benefit_amount,
  82. rule.benefit_discount,
  83. rule.benefit_level,
  84. info.start_time,
  85. info.end_time,
  86. info.create_time
  87. from
  88. (
  89. select * from ${APP}.ods_activity_info where dt='$do_date'
  90. )info
  91. left join
  92. (
  93. select * from ${APP}.ods_activity_rule where dt='$do_date'
  94. )rule on info.id = rule.activity_id;
  95. insert overwrite table ${APP}.dwd_fact_order_detail partition(dt='$do_date')
  96. select
  97. od.id,
  98. od.order_id,
  99. od.user_id,
  100. od.sku_id,
  101. od.sku_name,
  102. od.order_price,
  103. od.sku_num,
  104. od.create_time,
  105. oi.province_id,
  106. od.order_price*od.sku_num
  107. from
  108. (
  109. select * from ${APP}.ods_order_detail where dt='$do_date'
  110. ) od
  111. join
  112. (
  113. select * from ${APP}.ods_order_info where dt='$do_date'
  114. ) oi
  115. on od.order_id=oi.id;
  116. insert overwrite table ${APP}.dwd_fact_payment_info partition(dt='$do_date')
  117. select
  118. pi.id,
  119. pi.out_trade_no,
  120. pi.order_id,
  121. pi.user_id,
  122. pi.alipay_trade_no,
  123. pi.total_amount,
  124. pi.subject,
  125. pi.payment_type,
  126. pi.payment_time,
  127. oi.province_id
  128. from
  129. (
  130. select * from ${APP}.ods_payment_info where dt='$do_date'
  131. )pi
  132. join
  133. (
  134. select id, province_id from ${APP}.ods_order_info where dt='$do_date'
  135. )oi
  136. on pi.order_id = oi.id;
  137. insert overwrite table ${APP}.dwd_fact_order_refund_info partition(dt='$do_date')
  138. select
  139. id,
  140. user_id,
  141. order_id,
  142. sku_id,
  143. refund_type,
  144. refund_num,
  145. refund_amount,
  146. refund_reason_type,
  147. create_time
  148. from ${APP}.ods_order_refund_info
  149. where dt='$do_date';
  150. insert overwrite table ${APP}.dwd_fact_comment_info partition(dt='$do_date')
  151. select
  152. id,
  153. user_id,
  154. sku_id,
  155. spu_id,
  156. order_id,
  157. appraise,
  158. create_time
  159. from ${APP}.ods_comment_info
  160. where dt='$do_date';
  161. insert overwrite table ${APP}.dwd_fact_cart_info partition(dt='$do_date')
  162. select
  163. id,
  164. user_id,
  165. sku_id,
  166. cart_price,
  167. sku_num,
  168. sku_name,
  169. create_time,
  170. operate_time,
  171. is_ordered,
  172. order_time
  173. from ${APP}.ods_cart_info
  174. where dt='$do_date';
  175. insert overwrite table ${APP}.dwd_fact_favor_info partition(dt='$do_date')
  176. select
  177. id,
  178. user_id,
  179. sku_id,
  180. spu_id,
  181. is_cancel,
  182. create_time,
  183. cancel_time
  184. from ${APP}.ods_favor_info
  185. where dt='$do_date';
  186. insert overwrite table ${APP}.dwd_fact_coupon_use partition(dt)
  187. select
  188. if(new.id is null,old.id,new.id),
  189. if(new.coupon_id is null,old.coupon_id,new.coupon_id),
  190. if(new.user_id is null,old.user_id,new.user_id),
  191. if(new.order_id is null,old.order_id,new.order_id),
  192. if(new.coupon_status is null,old.coupon_status,new.coupon_status),
  193. if(new.get_time is null,old.get_time,new.get_time),
  194. if(new.using_time is null,old.using_time,new.using_time),
  195. if(new.used_time is null,old.used_time,new.used_time),
  196. date_format(if(new.get_time is null,old.get_time,new.get_time),'yyyy-MM-dd')
  197. from
  198. (
  199. select
  200. id,
  201. coupon_id,
  202. user_id,
  203. order_id,
  204. coupon_status,
  205. get_time,
  206. using_time,
  207. used_time
  208. from ${APP}.dwd_fact_coupon_use
  209. where dt in
  210. (
  211. select
  212. date_format(get_time,'yyyy-MM-dd')
  213. from ${APP}.ods_coupon_use
  214. where dt='$do_date'
  215. )
  216. )old
  217. full outer join
  218. (
  219. select
  220. id,
  221. coupon_id,
  222. user_id,
  223. order_id,
  224. coupon_status,
  225. get_time,
  226. using_time,
  227. used_time
  228. from ${APP}.ods_coupon_use
  229. where dt='$do_date'
  230. )new
  231. on old.id=new.id;
  232. insert overwrite table ${APP}.dwd_fact_order_info partition(dt)
  233. select
  234. if(new.id is null,old.id,new.id),
  235. if(new.order_status is null,old.order_status,new.order_status),
  236. if(new.user_id is null,old.user_id,new.user_id),
  237. if(new.out_trade_no is null,old.out_trade_no,new.out_trade_no),
  238. if(new.tms['1001'] is null,old.create_time,new.tms['1001']),--1001 对应未支付状态
  239. if(new.tms['1002'] is null,old.payment_time,new.tms['1002']),
  240. if(new.tms['1003'] is null,old.cancel_time,new.tms['1003']),
  241. if(new.tms['1004'] is null,old.finish_time,new.tms['1004']),
  242. if(new.tms['1005'] is null,old.refund_time,new.tms['1005']),
  243. if(new.tms['1006'] is null,old.refund_finish_time,new.tms['1006']),
  244. if(new.province_id is null,old.province_id,new.province_id),
  245. if(new.activity_id is null,old.activity_id,new.activity_id),
  246. if(new.original_total_amount is
  247. null,old.original_total_amount,new.original_total_amount),
  248. if(new.benefit_reduce_amount is
  249. null,old.benefit_reduce_amount,new.benefit_reduce_amount),
  250. if(new.feight_fee is null,old.feight_fee,new.feight_fee),
  251. if(new.final_total_amount is
  252. null,old.final_total_amount,new.final_total_amount),
  253. date_format(if(new.tms['1001'] is
  254. null,old.create_time,new.tms['1001']),'yyyy-MM-dd')
  255. from
  256. (
  257. select
  258. id,
  259. order_status,
  260. user_id,
  261. out_trade_no,
  262. create_time,
  263. payment_time,
  264. cancel_time,
  265. finish_time,
  266. refund_time,
  267. refund_finish_time,
  268. province_id,
  269. activity_id,
  270. original_total_amount,
  271. benefit_reduce_amount,
  272. feight_fee,
  273. final_total_amount
  274. from ${APP}.dwd_fact_order_info
  275. where dt
  276. in
  277. (
  278. select
  279. date_format(create_time,'yyyy-MM-dd')
  280. from ${APP}.ods_order_info
  281. where dt='$do_date'
  282. )
  283. )old
  284. full outer join
  285. (
  286. select
  287. info.id,
  288. info.order_status,
  289. info.user_id,
  290. info.out_trade_no,
  291. info.province_id,
  292. act.activity_id,
  293. log.tms,
  294. info.original_total_amount,
  295. info.benefit_reduce_amount,
  296. info.feight_fee,
  297. info.final_total_amount
  298. from
  299. (
  300. select
  301. order_id,
  302. str_to_map(concat_ws(',',collect_set(concat(order_status,'=',operate_time))),',','
  303. =') tms
  304. from ${APP}.ods_order_status_log
  305. where dt='$do_date'
  306. group by order_id
  307. )log
  308. join
  309. (
  310. select * from ${APP}.ods_order_info where dt='$do_date'
  311. )info
  312. on log.order_id=info.id
  313. left join
  314. (
  315. select * from ${APP}.ods_activity_order where dt='$do_date'
  316. )act
  317. on log.order_id=act.order_id
  318. )new
  319. on old.id=new.id;
  320. insert overwrite table ${APP}.dwd_dim_user_info_his_tmp
  321. select * from
  322. (
  323. select
  324. id,
  325. name,
  326. birthday,
  327. gender,
  328. email,
  329. user_level,
  330. create_time,
  331. operate_time,
  332. '$do_date' start_date,
  333. '9999-99-99' end_date
  334. from ${APP}.ods_user_info where dt='$do_date'
  335. union all
  336. select
  337. uh.id,
  338. uh.name,
  339. uh.birthday,
  340. uh.gender,
  341. uh.email,
  342. uh.user_level,
  343. uh.create_time,
  344. uh.operate_time,
  345. uh.start_date,
  346. if(ui.id is not null and uh.end_date='9999-99-99', date_add(ui.dt,-1),
  347. uh.end_date) end_date
  348. from ${APP}.dwd_dim_user_info_his uh left join
  349. (
  350. select
  351. *
  352. from ${APP}.ods_user_info
  353. where dt='$do_date'
  354. ) ui on uh.id=ui.id
  355. )his
  356. order by his.id, start_date;
  357. insert overwrite table ${APP}.dwd_dim_user_info_his select * from
  358. ${APP}.dwd_dim_user_info_his_tmp;
  359. "
  360. sql2="
  361. insert overwrite table ${APP}.dwd_dim_base_province
  362. select
  363. bp.id,
  364. bp.name,
  365. bp.area_code,
  366. bp.iso_code,
  367. bp.region_id,
  368. br.region_name
  369. from ${APP}.ods_base_province bp
  370. join ${APP}.ods_base_region br
  371. on bp.region_id=br.id;
  372. "
  373. case $1 in
  374. "first"){
  375. $hive -e "$sql1"
  376. $hive -e "$sql2"
  377. };;
  378. "all"){
  379. $hive -e "$sql1"
  380. };;
  381. esac
复制代码
2)增加脚本执行权限
  1. chmod 770 ods_to_dwd_db.sh
复制代码
3)执行脚本导入数据
  1. ods_to_dwd_db.sh all 2020-03-11
复制代码
4)查看导入数据
  1. select * from dwd_fact_order_info where dt='2020-03-11';
  2. select * from dwd_fact_order_detail where dt='2020-03-11';
  3. select * from dwd_fact_comment_info where dt='2020-03-11';
  4. select * from dwd_fact_order_refund_info where dt='2020-03-11';
复制代码

结束语

本章着重介绍了DWD层的业务数据的搭建流程,自此,DWD层搭建完成,下章开启DWS层的搭建!!!




加微信w3aboutyun,获取更多资源



领取100本书+1T资源
http://www.aboutyun.com/forum.php?mod=viewthread&tid=26480

大数据5个项目视频
http://www.aboutyun.com/forum.php?mod=viewthread&tid=25235

名企资源、名企面试题、最新BAT面试题、专题面试题等资源汇总
https://www.aboutyun.com/forum.php?mod=viewthread&tid=27732



文章来源:https://together.blog.csdn.net/article/details/105924483

已有(1)人评论

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条