长表开发模式样例

三段式开发:
一、开头
====step:0====
drop table if exists platform_temp.sr_dw_commodity_item_info_base_${dt};
create table platform_temp.sr_dw_commodity_item_info_base_${dt}
(
  `commodity_id` bigint,
  `attr_key` string,
  `attr_value` string
)
PARTITIONED BY
(
  `step` string
)
;

说明:
建立类似长表的临时分区表,作为中间过渡层,以便独立开发各个指标,最后汇总。
临时分区表为了支持并发刷数据,拼接了日期参数在表名中。

二、中间
====step:1====
drop table if exists platform_temp.sr_dw_commodity_item_info_base_category_01_${dt};
create table platform_temp.sr_dw_commodity_item_info_base_category_01_${dt}
as
select ……; --宽表

====step:1====
drop table if exists platform_temp.sr_dw_commodity_item_info_base_category_02_${dt};
create table platform_temp.sr_dw_commodity_item_info_base_category_02_${dt}
as
select ……; --宽表

====step:2====
insert overwrite table platform_temp.sr_dw_commodity_item_info_base_${dt} partition(step='category_info')
select
    commodity_id,
    'category_info' as attr_key,
    category_info attr_value
from
(
    select ……
    from
        platform_temp.sr_dw_commodity_item_info_base_category_01_${dt} t1
    left join
        platform_temp.sr_dw_commodity_item_info_base_category_02_${dt} t2
    on ……
)tt
;

====step:1====
insert overwrite table platform_temp.sr_dw_commodity_item_info_base_${dt} partition(step='item_sku')
select
    commodity_id,
    split(index,'=:=')[0] as attr_key,
    split(index,'=:=')[1] as attr_value
from ……;

说明:
1.中间层可以建立很多常规的临时宽表作为过渡表方便数据处理;
2.中间层每个step分区作为一个指标集开发空间,互相独立可以设置为并发执行;
3.一张宽表的多个字段提取需要用到列转行,拼接处理时用到符号“=:=”,因为可能处理的是一个字符类型的字段,其中很可能包含如“=”、“#”等简单字符,在split处理时会分隔异常。

三、结尾
====step:3====
insert overwrite table platform_dw.platform_dw_sr_dw_user_item_info partition(dt='${day}',label='base')
select
    user_id,attr_key,attr_value
from
    platform_temp.sr_dw_user_item_info_base_${dt} t1
left semi join
(
    select user_id from platform_dw.platform_dw_sr_dw_user_main
    where dt='${day}'
) t2
on t1.user_id = t2.user_id
where nvl(trim(attr_value),'')<>''
;

insert overwrite table platform_dw.platform_dw_sr_dw_feature_log partition(dt='${day}',label='base')
select
   attr_key,
   count(*) as coverage,
   from_unixtime(unix_timestamp()) as update_time
from
(
    select attr_key,user_id from platform_dw.platform_dw_sr_dw_user_item_info
    where dt='${day}'and label='base'
) t
group by attr_key
;

说明:
1.因为中间表和目标表都是长表,所以不限制分区就可以直接取到所有中间结果,不用关联,性能高效。
2.将临时分区表中的数据主键关联各大维度的主表,限制记录数后直接插入到开发层长表的指定label分区中。
并过滤掉无意义的数据:
  1> 在宽表中为null值的字段,经过列转行后生成的指标名为null的记录。
  2> 字段值本身为空字符串'',或字段值只包含多个空格的记录
3.基于过滤后的数据,统计任务生成的指标覆盖了多少主键,并记录下更新时间,插入日志表中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值