DWD learning from Shucang Learning

Insert image description here
Insert image description here

Login is not an atomic behavior, but login success is,
and payment is not atomic, and there are successes and failures.
Insert image description here
What is an atomic type? One line.
Pay attention to the password. The password of mysql is MD5 encryption, and the length of MD5 is fixed 32
Insert image description here

1. How to build a transaction table?

1. Determine the table name
2. Determine the meaning of a row of data
3. Determine the column definition
4. Determine the measurement value, which is a value that can be counted.

2. Purchase additional DWD table settings

1.What is add-on purchase?

Add to cart or add quantity

2. Thinking about tables

1. Corresponding items

Shopping cart user product
Shopping cart ID User ID Product ID Product quantity

2.Measurement

Number of Products

Shopping cart ID, user ID, product ID, product quantity modification time (add time)

3. Why do we need date ID and purchase time?

The date ID is for dimensional analysis
and the purchase time is the behavior time (ts)

DROP TABLE IF EXISTS dwd_trade_cart_add_inc;
CREATE EXTERNAL TABLE dwd_trade_cart_add_inc
(
    `id`                  STRING COMMENT '编号',
    `user_id`            STRING COMMENT '用户ID',
    `sku_id`             STRING COMMENT 'SKU_ID',
    `date_id`            STRING COMMENT '日期ID',
    `create_time`        STRING COMMENT '加购时间',
    `sku_num`            BIGINT COMMENT '加购物车件数'
) COMMENT '交易域加购事务事实表'
    PARTITIONED BY (`dt` STRING)
    STORED AS ORC
    LOCATION '/warehouse/gmall/dwd/dwd_trade_cart_add_inc/'
    TBLPROPERTIES ('orc.compress' = 'snappy');

4.Data loading

1. First day data loading

By default, all the first days are increments.

-- 首日数据装载
insert overwrite table dwd_trade_cart_add_inc partition (dt)
select
    data.`id`                 ,-- STRING COMMENT '编号',
    data.`user_id`            ,--STRING COMMENT '用户ID',
    data.`sku_id`             ,--STRING COMMENT 'SKU_ID',
    date_format(data.create_time, 'yyyy-MM-dd') `date_id`            ,--STRING COMMENT '日期ID',
    data.`create_time`        ,--STRING COMMENT '加购时间',
    data.`sku_num`            ,--BIGINT COMMENT '加购物车件数'
    date_format(data.create_time, 'yyyy-MM-dd')
from ods_cart_info_inc
where dt = '2022-06-08'
and type = 'bootstrap-insert';

2. Daily data loading

-- 每日数据装载
    -- 9号只能获取9号的数据,所以分区采用静态分区即可
insert overwrite table dwd_trade_cart_add_inc partition (dt='2022-06-09')
select
    data.`id`                 ,-- STRING COMMENT '编号',
    data.`user_id`            ,--STRING COMMENT '用户ID',
    data.`sku_id`             ,--STRING COMMENT 'SKU_ID',
    date_format(if ( type = 'insert', data.`create_time`, data.operate_time  ), 'yyyy-MM-dd') `date_id`            ,--STRING COMMENT '日期ID',
    if ( type = 'insert', data.`create_time`, data.operate_time  )         ,--STRING COMMENT '加购时间',
    if ( type = 'insert', data.`sku_num`, data.sku_num - cast(old['sku_num'] as bigint) )             --BIGINT COMMENT '加购物车件数'
from ods_cart_info_inc 
where dt = '2022-06-09'
and type = 'insert'
or  (
    type = 'update'
  and
    array_contains(map_keys(old), 'sku_num')
  and
    data.sku_num > cast(old['sku_num'] as bigint)
);

Insert image description here
Long type - string
hive automatically converts to long type
, but comparison cannot help comparison type conversion.
It cannot judge whether you want to compare on the left or on the right, and cannot identify the intention.
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/qq_42265608/article/details/132759818