需求一:用户信息宽表
原创大约 1 分钟
实现思路
这个需求比较简单,只需要对dwd_user
表和dwd_user_addr
表执行left join
操作,通过userid
进行关联即可,将结果保存到dws_user_all
表中就行了。
DWS层开发
通过脚本的方式初始化dws_user_all
表。
> cd /home/work/warehouse_goods_order
> vi dws_shopmall_requirement01_init.sh
#!/bin/bash
# 需求一:用户信息宽表
# dws层数据库和表初始化,只需要执行一次
hive -e "
create database if not exists dws_shopmall;
create external table if not exists dws_shopmall.dws_user_all (
id bigint,
username string,
gender tinyint,
birthday string,
email string,
mobile string,
createtime string,
disabled tinyint,
addrid bigint,
addrname string,
delivername string,
phone string,
isdefault tinyint
) partitioned by(dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/dws/user_all/';
"
因为可能每天都会有新增用户,所以需要按照天来分区,并在每天的凌晨时执行一次。
> cd /home/work/warehouse_goods_order
> vi dws_shopmall_requirement01_add_partition.sh
#!/bin/bash
# 需求一:用户信息宽表
# 每天凌晨执行一次
if [ "d$1" = "d" ]
then
dt=`date +%Y%m%d --date="1 days ago"`
else
dt=$1
fi
hive -e "
insert overwrite table dws_shopmall.dws_user_all partition(dt = '${dt}') select
du.id,
du.username,
du.gender,
du.birthday,
du.email,
du.mobile,
du.createtime,
du.disabled,
dua.id as addrid,
dua.addrname,
dua.isdefault,
dua.username as delivername,
dua.mobile as phone
from dwd_shopmall.dwd_user as du
left join dwd_shopmall.dwd_user_addr as dua
on du.id = dua.userid
where du.dt = '${dt}' and due.dt = '${dt}';
"
可以通过定时任务来执行它。
> crontab -e
# 每天凌晨0点1分执行
1 0 * * * /home/work/warehouse_goods_order/dws_shopmall_requirement01_add_partition.sh
感谢支持
更多内容,请移步《超级个体》。