数据仓库之【用户行为数仓】05:【ods层】原始数据层创建

数据仓库之【用户行为数仓】05:【ods层】原始数据层创建数据仓库分为4层:ods层、dwd层、dws层、app层 首先我们来构建第一层:ods层 一、创建ods层数据库 1、在hive中创建数据库ods_mall create database ods_m

大家好,欢迎来到IT知识分享网。

数据仓库分为4层:ods层、dwd层、dws层、app层
首先我们来构建第一层:ods层

一、创建ods层数据库

1、在hive中创建数据库ods_mall

create database ods_mall;

在这里插入图片描述
show databases;
在这里插入图片描述

2、针对用户行为的表有哪些?以及创表的注意事项

表名				解释
ods_user_active		用户主动活跃表(act=1)
ods_click_good		点击商品表(act=2)
ods_good_item		商品详情页表(act=3)
ods_good_list		商品列表页表(act=4)
ods_app_close		APP崩溃数据表(act=5)

注意:

1:由于在构建数据仓库的时候我们会创建多个数据库,所以在创建和使用表的时候最后都在表名前面带上对应的数据库名称,否则可能出现一些不必要的问题,可能会把ods层的表建到dwd层。

2:考虑到SQL重跑的情, 需要在SQL语句中添加if not exists

3:hive中可以用string、date、timestamp来表示日期时间,date用yyyy-MM-dd的形式表示,timestamp用yyyy-MM-dd hh:mm:ss的形式表示,String可以表示yyyy-MM-dd和yyyy-MM-dd hh:mm:ss。
这三种格式之间可以互相转换,不过在用的时候建议格式统一,String可以表示另外两种格式,并且也支持日期的大小比较,所以在这里针对时间统一使用String表示。

3、进行创建ods层的表

表名对应的建表语句
注意:在使用这里建表语句的时候注意里面的日期目录,需要和你HDFS中生成的日期目录保持一致。

hdfs dfs -ls /data/ods/user_action/20220309
hdfs dfs -ls -R /data/ods/user_action/20220309

在这里插入图片描述
act=1:打开APP
在这里插入图片描述
act=2:点击商品
在这里插入图片描述
act=3:商品详情页
在这里插入图片描述
act=4:商品列表
在这里插入图片描述
act=5:app奔溃数据
在这里插入图片描述

(1)ods_user_active

用户活跃就是用户打开app的操作,对应的是act=1,对应的数据在对应日期下的/1目录下:例如:20220309/1

create external table if not exists ods_mall.ods_user_active(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';

alter table ods_mall.ods_user_active add  if not exists partition(dt='20220309') location '20220309/1';

(2)ods_click_good

用户点击商品,对应的是act=2,对应的数据在对应日期下的/2目录下:例如:20220309/2

create external table if not exists ods_mall.ods_click_good(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';
 
alter table ods_mall.ods_click_good add  if not exists partition(dt='20220309') location '20220309/2';

(3)ods_good_item

商品详情,对应的是act=3,对应的数据在对应日期下的/3目录下:例如:20220309/3

create external table if not exists ods_mall.ods_good_item(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';

alter table ods_mall.ods_good_item add  if not exists partition(dt='20220309') location '20220309/3';

(4)ods_good_list

商品列表,对应的是act=4,对应的数据在对应日期下的/4目录下:例如:20220309/4

create external table if not exists ods_mall.ods_good_list(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';
 
alter table ods_mall.ods_good_list add  if not exists partition(dt='20220309') location '20220309/4';

(5)ods_app_close

app关闭,对应的是act=5,对应的数据在对应日期下的/5目录下:例如:20220309/5

create external table if not exists ods_mall.ods_app_close(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';

alter table ods_mall.ods_app_close add  if not exists partition(dt='20220309') location '20220309/5';

4、针对ods层抽取脚本

在工作中我们不会一个个去hive中去创建表以及关联数据的,我们都会将涉及的操作抽取成一个脚本实现自动化。
脚本通用放在/data/soft/warehouse_shell_user_action目录下。

mkdir -p /data/soft/warehouse_shell_user_action

(1)表初始化脚本(初始化执行一次)

ods_mall_init_table.sh
内容如下:

#!/bin/bash
# ods层数据库和表初始化脚本,只需要执行一次

hive -e "
create database if not exists ods_mall;

create external table if not exists ods_mall.ods_user_active(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';

 
create external table if not exists ods_mall.ods_click_good(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';
 
 
create external table if not exists ods_mall.ods_good_item(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';

 
create external table if not exists ods_mall.ods_good_list(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';


create external table if not exists ods_mall.ods_app_close(
    log    string
)partitioned by (dt string)
 row format delimited
 fields terminated by '\t'
 location 'hdfs://bigdata01:9000/data/ods/user_action/';

"

(2)通用添加分区脚本

add_partition.sh:这个脚本是通用的,所有添加分区的地方都可以使用。
内容如下:

#!/bin/bash
# 给外部分区表添加分区
# 接收三个参数
#1:表名
#2:分区字段dt的值:格式20220309
#3:分区路径(相对路径或者绝对路径都可以)

if [ $# != 3 ]
then
echo "参数异常:add_partition.sh <tabkle_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}';

(3)添加分区数据脚本(每天执行一次)

ods_mall_add_partition.sh

内容如下:

#!/bin/bash
# 给ods层的表添加分区,这个脚本后期每天执行一次
# 每天凌晨,添加昨天的分区,添加完分区之后,再执行后面的计算脚本

# 默认获取昨天的日期,也支持传参指定一个日期
if [ "z$1" = "z" ]
then
dt=`date +%Y%m%d --date="1 days ago"`
else
dt=$1
fi

#alter table ods_mall.ods_user_active add  if not exists partition(dt='20220309') location '20220309/1';
#alter table ods_mall.ods_click_good add  if not exists partition(dt='20220309') location '20220309/2';
#alter table ods_mall.ods_good_item add  if not exists partition(dt='20220309') location '20220309/3';
#alter table ods_mall.ods_good_list add  if not exists partition(dt='20220309') location '20220309/4';
#alter table ods_mall.ods_app_close add  if not exists partition(dt='20220309') location '20220309/5';
sh add_partition.sh ods_mall.ods_user_active ${dt} ${dt}/1
sh add_partition.sh ods_mall.ods_click_good ${dt} ${dt}/2
sh add_partition.sh ods_mall.ods_good_item ${dt} ${dt}/3
sh add_partition.sh ods_mall.ods_good_list ${dt} ${dt}/4
sh add_partition.sh ods_mall.ods_app_close ${dt} ${dt}/5

5、执行脚本

最后脚本如下:
在这里插入图片描述
执行脚本:

sh ods_mall_init_table.sh
sh ods_mall_add_partition.sh 20220309

在这里插入图片描述
在这里插入图片描述

6、验证一下

连接到hive

use ods_mall;
show tables;

在这里插入图片描述
查询一下表中是否有数据:

select * from ods_app_close limit 1;
select * from ods_click_good limit 1;
select * from ods_good_item limit 1;
select * from ods_good_list limit 1;
select * from ods_user_active limit 1;

在这里插入图片描述
我们发现hive中的表都是有数据的,说明创建的外部分区表都关联到hdfs上的数据了。
到此ods层就创建好了。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22167.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信