用户行为数仓ODS层
原创大约 3 分钟
构建ODS层
进入Hive,创建ODS层
数据库。
> cd /home/work/hive-3.1.3
> ./bin/hive
# 先创建数据库
hive> create database if not exists ods_shopmall;
# 再创建数据表
hive> create external table if not exists ods_shopmall.ods_app_open (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
hive> create external table if not exists ods_shopmall.ods_goods_click (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
hive> create external table if not exists ods_shopmall.ods_goods_item (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
hive> create external table if not exists ods_shopmall.ods_goods_list (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
hive> create external table if not exists ods_shopmall.ods_app_collapse (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
# 给表增加分区
hive> alter table ods_shopmall.ods_app_open add if not exists partition(dt = '20240518') location '20240518/1';
# 查询数据
hive> select * from ods_shopmall.ods_app_open where dt = '20240518';
也可以将这些语句放到脚本中去批量执行。
> mkdir -p /home/work/warehouse_user_action
> cd /home/work/warehouse_user_action
> vi ods_shopmall_init.sh
#!/bin/bash
# ods层数据库和表初始化脚本
hive -e "
create database if not exists ods_shopmall;
create external table if not exists ods_shopmall.ods_app_open (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
create external table if not exists ods_shopmall.ods_goods_click (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
create external table if not exists ods_shopmall.ods_goods_item (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
create external table if not exists ods_shopmall.ods_goods_list (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
create external table if not exists ods_shopmall.ods_app_collapse (
log string
) partitioned by (dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://server01:9000/data/ods/ua/';
"
# 执行脚本
> sh ods_shopmall_init.sh
# 给表增加分区
hive> alter table ods_shopmall.ods_app_open add if not exists partition(dt = '20240518') location '20240518/1';
# 查询数据
hive> select * from ods_shopmall.ods_app_open where dt = '20240518';
通用分区脚本
因为表的分区是按照日期来创建的,所以每天都需要通过alter
语句增加一个当天日期的分区。
用手工来做这件事是几乎不可能的,还是得用脚本来自动化执行。
先创建一个通用的添加分区的脚本,所有需要添加分区的地方都可以用(包括其他层)。
> cd /home/work
> vi common_add_partition.sh
#!/bin/bash
# 给外部分区表添加分区,参数如下
#$1:表名table,格式为 数据库名.表名,例如 'ods_shopmall.ods_app_open'
#$2:分区字段dt,格式为 yyyyMMdd,例如 '20240518'
#$3:分区路径path,例如 '20240518/1'
if [ $# != 3 ]
then
echo "参数异常:common_add_partition.sh <table_name> <dt> <path>"
exit 100
fi
table_name=$1
dt=$2
path=$3
hive -e "
alter table ${table_name} add if not exists partition(dt = '${dt}') location '${path}';
"
ODS层抽取
有了通用脚本,就可以给每个行为类别添加分区。
> cd /home/work/warehouse_user_action
> vi ods_shopmall_add_partition.sh
#!/bin/bash
# 给ods层的表都添加分区,每天凌晨执行一次
if [ "d$1" = "d" ]
then
dt=`date +%Y%m%d --date="1 days ago"`
else
dt=$1
fi
sh ../common_add_partition.sh ods_shopmall.ods_app_open ${dt} ${dt}/1
sh ../common_add_partition.sh ods_shopmall.ods_goods_click ${dt} ${dt}/2
sh ../common_add_partition.sh ods_shopmall.ods_goods_item ${dt} ${dt}/3
sh ../common_add_partition.sh ods_shopmall.ods_goods_list ${dt} ${dt}/4
sh ../common_add_partition.sh ods_shopmall.ods_app_collapse ${dt} ${dt}/5
可以通过定时任务来执行它。
> crontab -e
# 每天凌晨0点1分执行
1 0 * * * /home/work/warehouse_user_action/ods_shopmall_add_partition.sh
感谢支持
更多内容,请移步《超级个体》。