电商数仓项目----笔记七(数仓DIM层)

2023-12-24 18:36
文章标签 项目 笔记 数仓 dim 商数

本文主要是介绍电商数仓项目----笔记七(数仓DIM层),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

所谓的维度层其实就是分析数据的角度,维度层保存的表其实是分析数据的角度,比如:

        --性别,年龄,品牌,品类

这层的表主要用于统计分析,因此DIM层的数据存储格式为orc列式存储+snappy压缩(时间短)

orc列式存储的好处:

  1. 查询的时候不需要扫描全部的数据,而只需要读取每次查询涉及的列,这样可以将I/O消耗降低N倍,另外可以保存每一列的统计信息(min、max、sum等),实现部分的谓词下推。
  2. 由于每一列的成员都是同构的,可以针对不同的数据类型使用更高效的数据压缩算法,进一步减小I/O。
  3. 由于每一列的成员的同构性,可以使用更加适合CPU pipeline的编码方式,减小CPU的缓存失效。

维度表的设计

        一个维度就是一张表,从实践的角度来讲,不同的维度就是这张表的字段,可以达到解耦的目的。如果维度特别简单,可以不用创建表,可以在事实表直接使用。

        字段:只要能用来分析的维度,都是字段;

        数据(字段)来源:参考业务数据的表字段:

                -- 主维表:业务数据库主要用于分析维度字段的表;

                -- 相关维表:业务数据库相关用于分析维度字段的表;

        维度字段的确定:

                尽可能生成丰富的维度属性:字段越多越好;

                编码和文字共存(0男/1女);

                计算通用的维度属性;

下面举几个例子:

优惠券维度表 

从主维表和相关维表分析:

        主维表:coupon_info,相关维表:coupon_range,coupon_use,但是coupon_use算是一种行为概念,并不属于状态,状态才是用来做分析的。但是在coupon_info里面也有range相关字段,因此发生了冗余,只需关注coupon_info即可。

coupon_info长这样:

我们这样设计:

DROP TABLE IF EXISTS dim_coupon_full;
CREATE EXTERNAL TABLE dim_coupon_full
(`id`               STRING COMMENT '购物券编号',`coupon_name`      STRING COMMENT '购物券名称',`coupon_type_code` STRING COMMENT '购物券类型编码',`coupon_type_name` STRING COMMENT '购物券类型名称',`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 '折扣',`benefit_rule`     STRING COMMENT '优惠规则:满元*减*元,满*件打*折',`create_time`      STRING COMMENT '创建时间',`range_type_code`  STRING COMMENT '优惠范围类型编码',`range_type_name`  STRING COMMENT '优惠范围类型名称',`limit_num`        BIGINT COMMENT '最多领取次数',`taken_count`      BIGINT COMMENT '已领取次数',`start_time`       STRING COMMENT '可以领取的开始日期',`end_time`         STRING COMMENT '可以领取的结束日期',`operate_time`     STRING COMMENT '修改时间',`expire_time`      STRING COMMENT '过期时间'
) COMMENT '优惠券维度表'PARTITIONED BY (`dt` STRING)STORED AS ORCLOCATION '/warehouse/gmall/dim/dim_coupon_full/'TBLPROPERTIES ('orc.compress' = 'snappy');

        其中不一样的地方有我们将ODS层原表的coupon_type分解为了coupon_type_code和coupon_type_name。将range_type分解为了range_type_code,range_type_name,并且增加了benefit_rule字段(优惠规则)。这样做符合我们上面说的编码和文字共存规则。

数据装载

        我们的表主要从coupon_info和base_dic(字典表)中取得:

        记住这里的主维表是coupon_info,因此我们先select coupon_info这张表,select里面的字段依照我们建表语句里面的字段先写好,当然其中肯定会有几个字段会报红,没关系我们后面还要join 操作,其中coupon_type_code,coupon_type_name,range_type_code,range_type_name字段是找不到的,因此需要join操作。我们join base_dic字典表:

join base_dic两次分别得到coupon_type_code,coupon_type_name字段和range_type_code,range_type_name字段;

        接下来是benefit_rule字段,这里需要我们自行拼接。拼接逻辑如下:

case coupon_typewhen '3201' then concat('满',condition_amount,'元减',benefit_amount,'元')when '3202' then concat('满',condition_num,'件打',10*(1-benefit_discount),'折')when '3203' then concat('减',benefit_amount,'元')end benefit_rule,

 完整是这样:

selectid,coupon_name,coupon_type,coupon_dic.dic_name,condition_amount,condition_num,activity_id,benefit_amount,benefit_discount,case coupon_typewhen '3201' then concat('满',condition_amount,'元减',benefit_amount,'元')when '3202' then concat('满',condition_num,'件打',10*(1-benefit_discount),'折')when '3203' then concat('减',benefit_amount,'元')end benefit_rule,create_time,range_type,range_dic.dic_name,limit_num,taken_count,start_time,end_time,operate_time,expire_time
from
(selectid,coupon_name,coupon_type,condition_amount,condition_num,activity_id,benefit_amount,benefit_discount,create_time,range_type,limit_num,taken_count,start_time,end_time,operate_time,expire_timefrom ods_coupon_info_fullwhere dt='2020-06-14'
)ci
left join
(selectdic_code,dic_namefrom ods_base_dic_fullwhere dt='2020-06-14'and parent_code='32'
)coupon_dic
on ci.coupon_type=coupon_dic.dic_code
left join
(selectdic_code,dic_namefrom ods_base_dic_fullwhere dt='2020-06-14'and parent_code='33'
)range_dic
on ci.range_type=range_dic.dic_code;

数据装载我们只需要前面加上下面这一句即可:

insert overwrite table dim_coupon_full partition(dt='2020-06-14')

我们的Dim层优惠券维度表就设计完啦。

活动维度表

        同样的,找到主维表和相关维表。

        activity_info ,activity_rule,activity_sku:我们分析的更多的是活动规则,而不是活动本身,所以主维表是activity_rule,相关维表是activity_info。

我们这样设计:

DROP TABLE IF EXISTS dim_activity_full;
CREATE EXTERNAL TABLE dim_activity_full
(`activity_rule_id`   STRING COMMENT '活动规则ID',`activity_id`        STRING COMMENT '活动ID',`activity_name`      STRING COMMENT '活动名称',`activity_type_code` STRING COMMENT '活动类型编码',`activity_type_name` STRING COMMENT '活动类型名称',`activity_desc`      STRING COMMENT '活动描述',`start_time`         STRING COMMENT '开始时间',`end_time`           STRING COMMENT '结束时间',`create_time`        STRING COMMENT '创建时间',`condition_amount`   DECIMAL(16, 2) COMMENT '满减金额',`condition_num`      BIGINT COMMENT '满减件数',`benefit_amount`     DECIMAL(16, 2) COMMENT '优惠金额',`benefit_discount`   DECIMAL(16, 2) COMMENT '优惠折扣',`benefit_rule`       STRING COMMENT '优惠规则',`benefit_level`      STRING COMMENT '优惠级别'
) COMMENT '活动信息表'PARTITIONED BY (`dt` STRING)STORED AS ORCLOCATION '/warehouse/gmall/dim/dim_activity_full/'TBLPROPERTIES ('orc.compress' = 'snappy');

数据装载:

insert overwrite table dim_activity_full partition(dt='2020-06-14')
select`activity_rule_id`   ,--STRING COMMENT '活动规则ID',`activity_id`        ,--STRING COMMENT '活动ID',`activity_name`      ,--STRING COMMENT '活动名称',`activity_type_code` ,--STRING COMMENT '活动类型编码',`activity_type_name` ,--STRING COMMENT '活动类型名称',`activity_desc`      ,--STRING COMMENT '活动描述',`start_time`         ,--STRING COMMENT '开始时间',`end_time`           ,--STRING COMMENT '结束时间',`create_time`        ,--STRING COMMENT '创建时间',`condition_amount`   ,--DECIMAL(16, 2) COMMENT '满减金额',`condition_num`      ,--BIGINT COMMENT '满减件数',`benefit_amount`     ,--DECIMAL(16, 2) COMMENT '优惠金额',`benefit_discount`   ,--DECIMAL(16, 2) COMMENT '优惠折扣',`benefit_rule`       ,--STRING COMMENT '优惠规则',`benefit_level`      --STRING COMMENT '优惠级别'
from(selectid `activity_rule_id`   ,--STRING COMMENT '活动规则ID',`activity_id`        ,--STRING COMMENT '活动ID',--`activity_name`      ,--STRING COMMENT '活动名称',activity_type `activity_type_code` ,--STRING COMMENT '活动类型编码',--`activity_type_name` ,--STRING COMMENT '活动类型名称',--`activity_desc`      ,--STRING COMMENT '活动描述',--`start_time`         ,--STRING COMMENT '开始时间',--`end_time`           ,--STRING COMMENT '结束时间',dt create_time                   ,--STRING COMMENT '创建时间',`condition_amount`   ,--DECIMAL(16, 2) COMMENT '满减金额',`condition_num`      ,--BIGINT COMMENT '满减件数',`benefit_amount`     ,--DECIMAL(16, 2) COMMENT '优惠金额',`benefit_discount`   ,--DECIMAL(16, 2) COMMENT '优惠折扣',case activity_typewhen '3101' then concat('满',condition_amount,'元减',benefit_amount,'元')when '3102' then concat('满',condition_num,'件打',benefit_discount,'折')when '3103' then concat('打',benefit_discount,'折')end `benefit_rule`       ,--STRING COMMENT '优惠规则',`benefit_level`      --STRING COMMENT '优惠级别'from ods_activity_rule_fullwhere dt='2020-06-14')rule
left join(selectid,activity_name,activity_desc,start_time,end_timefrom ods_activity_info_fullwhere dt='2020-06-14') info
on rule.activity_id=info.id
left join (selectdic_code,dic_name activity_type_namefrom ods_base_dic_fullwhere dt='2020-06-14' and parent_code='31')dic on rule.activity_type_code=dic.dic_code

        整体思路就是先将create表中的字段复制到select 主维表的语句中,爆红的字段我们一一给他们join出来,或在join的那张表中给他们查询出来,这里就不详细分析了。

日期维度表

建表语句

DROP TABLE IF EXISTS dim_date;
CREATE EXTERNAL TABLE dim_date
(`date_id`    STRING COMMENT '日期ID',`week_id`    STRING COMMENT '周ID,一年中的第几周',`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 ORCLOCATION '/warehouse/gmall/dim/dim_date/'TBLPROPERTIES ('orc.compress' = 'snappy');

数据装载

        通常情况下,时间维度表的数据并不是来自于业务系统,而是手动写入,并且由于时间维度表数据的可预见性,无须每日导入,一般可一次性导入一年的数据。

(1)创建临时表

DROP TABLE IF EXISTS tmp_dim_date_info;
CREATE EXTERNAL TABLE tmp_dim_date_info (`date_id` STRING COMMENT '日',`week_id` STRING COMMENT '周ID',`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/tmp/tmp_dim_date_info/';

将数据文件上传到HFDS上临时表路径/warehouse/gmall/tmp/tmp_dim_date_info 

(3)执行以下语句将其导入时间维度表

insert overwrite table dim_date select * from tmp_dim_date_info;

 

用户维度表

        用户维度表这里我们使用拉链表,来记录用户姓名的变更或者用户的增加减少。

(1)数据装载过程

(2)数据流向 

首日装载 

我们的用户数据在进行首日装载和后续的变更都是insert overwrite到9999-12-31的分区,首日装载如下:

insert overwrite table dim_user_zip partition (dt='9999-12-31')
selectdata.id,data.login_name,data.nick_name,md5(data.name),md5(data.phone_num),md5(data.email),data.user_level,data.birthday,data.gender,data.create_time,data.operate_time,'2020-06-14' start_date,'9999-12-31' end_date
from ods_user_info_inc
where dt='2020-06-14'
and type='bootstrap-insert';

每日装载

        装载思路:

         装载语句:

with
tmp as
(selectold.id old_id,old.login_name old_login_name,old.nick_name old_nick_name,old.name old_name,old.phone_num old_phone_num,old.email old_email,old.user_level old_user_level,old.birthday old_birthday,old.gender old_gender,old.create_time old_create_time,old.operate_time old_operate_time,old.start_date old_start_date,old.end_date old_end_date,new.id new_id,new.login_name new_login_name,new.nick_name new_nick_name,new.name new_name,new.phone_num new_phone_num,new.email new_email,new.user_level new_user_level,new.birthday new_birthday,new.gender new_gender,new.create_time new_create_time,new.operate_time new_operate_time,new.start_date new_start_date,new.end_date new_end_datefrom(selectid,login_name,nick_name,name,phone_num,email,user_level,birthday,gender,create_time,operate_time,start_date,end_datefrom dim_user_zipwhere dt='9999-12-31')oldfull outer join(selectid,login_name,nick_name,md5(name) name,md5(phone_num) phone_num,md5(email) email,user_level,birthday,gender,create_time,operate_time,'2020-06-15' start_date,'9999-12-31' end_datefrom(selectdata.id,data.login_name,data.nick_name,data.name,data.phone_num,data.email,data.user_level,data.birthday,data.gender,data.create_time,data.operate_time,row_number() over (partition by data.id order by ts desc) rnfrom ods_user_info_incwhere dt='2020-06-15')t1where rn=1)newon old.id=new.id
)
insert overwrite table dim_user_zip partition(dt)
selectif(new_id is not null,new_id,old_id),if(new_id is not null,new_login_name,old_login_name),if(new_id is not null,new_nick_name,old_nick_name),if(new_id is not null,new_name,old_name),if(new_id is not null,new_phone_num,old_phone_num),if(new_id is not null,new_email,old_email),if(new_id is not null,new_user_level,old_user_level),if(new_id is not null,new_birthday,old_birthday),if(new_id is not null,new_gender,old_gender),if(new_id is not null,new_create_time,old_create_time),if(new_id is not null,new_operate_time,old_operate_time),if(new_id is not null,new_start_date,old_start_date),if(new_id is not null,new_end_date,old_end_date),if(new_id is not null,new_end_date,old_end_date) dt
from tmp
union all
selectold_id,old_login_name,old_nick_name,old_name,old_phone_num,old_email,old_user_level,old_birthday,old_gender,old_create_time,old_operate_time,old_start_date,cast(date_add('2020-06-15',-1) as string) old_end_date,cast(date_add('2020-06-15',-1) as string) dt
from tmp
where old_id is not null
and new_id is not null;

 

数据装载脚本

首日装载脚本

#!/bin/bashAPP=gmallif [ -n "$2" ] ;thendo_date=$2
else echo "请传入日期参数"exit
fi dim_user_zip="
insert overwrite table ${APP}.dim_user_zip partition (dt='9999-12-31')
selectdata.id,data.login_name,data.nick_name,md5(data.name),md5(data.phone_num),md5(data.email),data.user_level,data.birthday,data.gender,data.create_time,data.operate_time,'$do_date' start_date,'9999-12-31' end_date
from ${APP}.ods_user_info_inc
where dt='$do_date'
and type='bootstrap-insert';
"dim_sku_full="
with
sku as
(selectid,price,sku_name,sku_desc,weight,is_sale,spu_id,category3_id,tm_id,create_timefrom ${APP}.ods_sku_info_fullwhere dt='$do_date'
),
spu as
(selectid,spu_namefrom ${APP}.ods_spu_info_fullwhere dt='$do_date'
),
c3 as
(selectid,name,category2_idfrom ${APP}.ods_base_category3_fullwhere dt='$do_date'
),
c2 as
(selectid,name,category1_idfrom ${APP}.ods_base_category2_fullwhere dt='$do_date'
),
c1 as
(selectid,namefrom ${APP}.ods_base_category1_fullwhere dt='$do_date'
),
tm as
(selectid,tm_namefrom ${APP}.ods_base_trademark_fullwhere dt='$do_date'
),
attr as
(selectsku_id,collect_set(named_struct('attr_id',attr_id,'value_id',value_id,'attr_name',attr_name,'value_name',value_name)) attrsfrom ${APP}.ods_sku_attr_value_fullwhere dt='$do_date'group by sku_id
),
sale_attr as
(selectsku_id,collect_set(named_struct('sale_attr_id',sale_attr_id,'sale_attr_value_id',sale_attr_value_id,'sale_attr_name',sale_attr_name,'sale_attr_value_name',sale_attr_value_name)) sale_attrsfrom ${APP}.ods_sku_sale_attr_value_fullwhere dt='$do_date'group by sku_id
)
insert overwrite table ${APP}.dim_sku_full partition(dt='$do_date')
selectsku.id,sku.price,sku.sku_name,sku.sku_desc,sku.weight,sku.is_sale,sku.spu_id,spu.spu_name,sku.category3_id,c3.name,c3.category2_id,c2.name,c2.category1_id,c1.name,sku.tm_id,tm.tm_name,attr.attrs,sale_attr.sale_attrs,sku.create_time
from sku
left join spu on sku.spu_id=spu.id
left join c3 on sku.category3_id=c3.id
left join c2 on c3.category2_id=c2.id
left join c1 on c2.category1_id=c1.id
left join tm on sku.tm_id=tm.id
left join attr on sku.id=attr.sku_id
left join sale_attr on sku.id=sale_attr.sku_id;
"dim_province_full="
insert overwrite table ${APP}.dim_province_full partition(dt='$do_date')
selectprovince.id,province.name,province.area_code,province.iso_code,province.iso_3166_2,region_id,region_name
from
(selectid,name,region_id,area_code,iso_code,iso_3166_2from ${APP}.ods_base_province_fullwhere dt='$do_date'
)province
left join
(selectid,region_namefrom ${APP}.ods_base_region_fullwhere dt='$do_date'
)region
on province.region_id=region.id;
"dim_coupon_full="
insert overwrite table ${APP}.dim_coupon_full partition(dt='$do_date')
selectid,coupon_name,coupon_type,coupon_dic.dic_name,condition_amount,condition_num,activity_id,benefit_amount,benefit_discount,case coupon_typewhen '3201' then concat('满',condition_amount,'元减',benefit_amount,'元')when '3202' then concat('满',condition_num,'件打',10*(1-benefit_discount),'折')when '3203' then concat('减',benefit_amount,'元')end benefit_rule,create_time,range_type,range_dic.dic_name,limit_num,taken_count,start_time,end_time,operate_time,expire_time
from
(selectid,coupon_name,coupon_type,condition_amount,condition_num,activity_id,benefit_amount,benefit_discount,create_time,range_type,limit_num,taken_count,start_time,end_time,operate_time,expire_timefrom ${APP}.ods_coupon_info_fullwhere dt='$do_date'
)ci
left join
(selectdic_code,dic_namefrom ${APP}.ods_base_dic_fullwhere dt='$do_date'and parent_code='32'
)coupon_dic
on ci.coupon_type=coupon_dic.dic_code
left join
(selectdic_code,dic_namefrom ${APP}.ods_base_dic_fullwhere dt='$do_date'and parent_code='33'
)range_dic
on ci.range_type=range_dic.dic_code;
"dim_activity_full="
insert overwrite table ${APP}.dim_activity_full partition(dt='$do_date')
selectrule.id,info.id,activity_name,rule.activity_type,dic.dic_name,activity_desc,start_time,end_time,create_time,condition_amount,condition_num,benefit_amount,benefit_discount,case rule.activity_typewhen '3101' then concat('满',condition_amount,'元减',benefit_amount,'元')when '3102' then concat('满',condition_num,'件打',10*(1-benefit_discount),'折')when '3103' then concat('打',10*(1-benefit_discount),'折')end benefit_rule,benefit_level
from
(selectid,activity_id,activity_type,condition_amount,condition_num,benefit_amount,benefit_discount,benefit_levelfrom ${APP}.ods_activity_rule_fullwhere dt='$do_date'
)rule
left join
(selectid,activity_name,activity_type,activity_desc,start_time,end_time,create_timefrom ${APP}.ods_activity_info_fullwhere dt='$do_date'
)info
on rule.activity_id=info.id
left join
(selectdic_code,dic_namefrom ${APP}.ods_base_dic_fullwhere dt='$do_date'and parent_code='31'
)dic
on rule.activity_type=dic.dic_code;
"case $1 in
"dim_user_zip")hive -e "$dim_user_zip"
;;
"dim_sku_full")hive -e "$dim_sku_full"
;;
"dim_province_full")hive -e "$dim_province_full"
;;
"dim_coupon_full")hive -e "$dim_coupon_full"
;;
"dim_activity_full")hive -e "$dim_activity_full"
;;
"all")hive -e "$dim_user_zip$dim_sku_full$dim_province_full$dim_coupon_full$dim_activity_full"
;;
esac

每日装载脚本

#!/bin/bashAPP=gmall# 如果是输入的日期按照取输入日期;如果没输入日期取当前时间的前一天
if [ -n "$2" ] ;thendo_date=$2
else do_date=`date -d "-1 day" +%F`
fidim_user_zip="
set hive.exec.dynamic.partition.mode=nonstrict;
with
tmp as
(selectold.id old_id,old.login_name old_login_name,old.nick_name old_nick_name,old.name old_name,old.phone_num old_phone_num,old.email old_email,old.user_level old_user_level,old.birthday old_birthday,old.gender old_gender,old.create_time old_create_time,old.operate_time old_operate_time,old.start_date old_start_date,old.end_date old_end_date,new.id new_id,new.login_name new_login_name,new.nick_name new_nick_name,new.name new_name,new.phone_num new_phone_num,new.email new_email,new.user_level new_user_level,new.birthday new_birthday,new.gender new_gender,new.create_time new_create_time,new.operate_time new_operate_time,new.start_date new_start_date,new.end_date new_end_datefrom(selectid,login_name,nick_name,name,phone_num,email,user_level,birthday,gender,create_time,operate_time,start_date,end_datefrom ${APP}.dim_user_zipwhere dt='9999-12-31')oldfull outer join(selectid,login_name,nick_name,md5(name) name,md5(phone_num) phone_num,md5(email) email,user_level,birthday,gender,create_time,operate_time,'$do_date' start_date,'9999-12-31' end_datefrom(selectdata.id,data.login_name,data.nick_name,data.name,data.phone_num,data.email,data.user_level,data.birthday,data.gender,data.create_time,data.operate_time,row_number() over (partition by data.id order by ts desc) rnfrom ${APP}.ods_user_info_incwhere dt='$do_date')t1where rn=1)newon old.id=new.id
)
insert overwrite table ${APP}.dim_user_zip partition(dt)
selectif(new_id is not null,new_id,old_id),if(new_id is not null,new_login_name,old_login_name),if(new_id is not null,new_nick_name,old_nick_name),if(new_id is not null,new_name,old_name),if(new_id is not null,new_phone_num,old_phone_num),if(new_id is not null,new_email,old_email),if(new_id is not null,new_user_level,old_user_level),if(new_id is not null,new_birthday,old_birthday),if(new_id is not null,new_gender,old_gender),if(new_id is not null,new_create_time,old_create_time),if(new_id is not null,new_operate_time,old_operate_time),if(new_id is not null,new_start_date,old_start_date),if(new_id is not null,new_end_date,old_end_date),if(new_id is not null,new_end_date,old_end_date) dt
from tmp
union all
selectold_id,old_login_name,old_nick_name,old_name,old_phone_num,old_email,old_user_level,old_birthday,old_gender,old_create_time,old_operate_time,old_start_date,cast(date_add('$do_date',-1) as string) old_end_date,cast(date_add('$do_date',-1) as string) dt
from tmp
where old_id is not null
and new_id is not null;
"dim_sku_full="
with
sku as
(selectid,price,sku_name,sku_desc,weight,is_sale,spu_id,category3_id,tm_id,create_timefrom ${APP}.ods_sku_info_fullwhere dt='$do_date'
),
spu as
(selectid,spu_namefrom ${APP}.ods_spu_info_fullwhere dt='$do_date'
),
c3 as
(selectid,name,category2_idfrom ${APP}.ods_base_category3_fullwhere dt='$do_date'
),
c2 as
(selectid,name,category1_idfrom ${APP}.ods_base_category2_fullwhere dt='$do_date'
),
c1 as
(selectid,namefrom ${APP}.ods_base_category1_fullwhere dt='$do_date'
),
tm as
(selectid,tm_namefrom ${APP}.ods_base_trademark_fullwhere dt='$do_date'
),
attr as
(selectsku_id,collect_set(named_struct('attr_id',attr_id,'value_id',value_id,'attr_name',attr_name,'value_name',value_name)) attrsfrom ${APP}.ods_sku_attr_value_fullwhere dt='$do_date'group by sku_id
),
sale_attr as
(selectsku_id,collect_set(named_struct('sale_attr_id',sale_attr_id,'sale_attr_value_id',sale_attr_value_id,'sale_attr_name',sale_attr_name,'sale_attr_value_name',sale_attr_value_name)) sale_attrsfrom ${APP}.ods_sku_sale_attr_value_fullwhere dt='$do_date'group by sku_id
)
insert overwrite table ${APP}.dim_sku_full partition(dt='$do_date')
selectsku.id,sku.price,sku.sku_name,sku.sku_desc,sku.weight,sku.is_sale,sku.spu_id,spu.spu_name,sku.category3_id,c3.name,c3.category2_id,c2.name,c2.category1_id,c1.name,sku.tm_id,tm.tm_name,attr.attrs,sale_attr.sale_attrs,sku.create_time
from sku
left join spu on sku.spu_id=spu.id
left join c3 on sku.category3_id=c3.id
left join c2 on c3.category2_id=c2.id
left join c1 on c2.category1_id=c1.id
left join tm on sku.tm_id=tm.id
left join attr on sku.id=attr.sku_id
left join sale_attr on sku.id=sale_attr.sku_id;
"dim_province_full="
insert overwrite table ${APP}.dim_province_full partition(dt='$do_date')
selectprovince.id,province.name,province.area_code,province.iso_code,province.iso_3166_2,region_id,region_name
from
(selectid,name,region_id,area_code,iso_code,iso_3166_2from ${APP}.ods_base_province_fullwhere dt='$do_date'
)province
left join
(selectid,region_namefrom ${APP}.ods_base_region_fullwhere dt='$do_date'
)region
on province.region_id=region.id;
"dim_coupon_full="
insert overwrite table ${APP}.dim_coupon_full partition(dt='$do_date')
selectid,coupon_name,coupon_type,coupon_dic.dic_name,condition_amount,condition_num,activity_id,benefit_amount,benefit_discount,case coupon_typewhen '3201' then concat('满',condition_amount,'元减',benefit_amount,'元')when '3202' then concat('满',condition_num,'件打',10*(1-benefit_discount),'折')when '3203' then concat('减',benefit_amount,'元')end benefit_rule,create_time,range_type,range_dic.dic_name,limit_num,taken_count,start_time,end_time,operate_time,expire_time
from
(selectid,coupon_name,coupon_type,condition_amount,condition_num,activity_id,benefit_amount,benefit_discount,create_time,range_type,limit_num,taken_count,start_time,end_time,operate_time,expire_timefrom ${APP}.ods_coupon_info_fullwhere dt='$do_date'
)ci
left join
(selectdic_code,dic_namefrom ${APP}.ods_base_dic_fullwhere dt='$do_date'and parent_code='32'
)coupon_dic
on ci.coupon_type=coupon_dic.dic_code
left join
(selectdic_code,dic_namefrom ${APP}.ods_base_dic_fullwhere dt='$do_date'and parent_code='33'
)range_dic
on ci.range_type=range_dic.dic_code;
"dim_activity_full="
insert overwrite table ${APP}.dim_activity_full partition(dt='$do_date')
selectrule.id,info.id,activity_name,rule.activity_type,dic.dic_name,activity_desc,start_time,end_time,create_time,condition_amount,condition_num,benefit_amount,benefit_discount,case rule.activity_typewhen '3101' then concat('满',condition_amount,'元减',benefit_amount,'元')when '3102' then concat('满',condition_num,'件打',10*(1-benefit_discount),'折')when '3103' then concat('打',10*(1-benefit_discount),'折')end benefit_rule,benefit_level
from
(selectid,activity_id,activity_type,condition_amount,condition_num,benefit_amount,benefit_discount,benefit_levelfrom ${APP}.ods_activity_rule_fullwhere dt='$do_date'
)rule
left join
(selectid,activity_name,activity_type,activity_desc,start_time,end_time,create_timefrom ${APP}.ods_activity_info_fullwhere dt='$do_date'
)info
on rule.activity_id=info.id
left join
(selectdic_code,dic_namefrom ${APP}.ods_base_dic_fullwhere dt='$do_date'and parent_code='31'
)dic
on rule.activity_type=dic.dic_code;
"case $1 in
"dim_user_zip")hive -e "$dim_user_zip"
;;
"dim_sku_full")hive -e "$dim_sku_full"
;;
"dim_province_full")hive -e "$dim_province_full"
;;
"dim_coupon_full")hive -e "$dim_coupon_full"
;;
"dim_activity_full")hive -e "$dim_activity_full"
;;
"all")hive -e "$dim_user_zip$dim_sku_full$dim_province_full$dim_coupon_full$dim_activity_full"
;;
esac

这篇关于电商数仓项目----笔记七(数仓DIM层)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步