【項目】數倉項目(六)

在這裏插入圖片描述在這裏插入圖片描述

(圖片來源於網絡,侵刪)


一、數倉分層

1.1 爲什麼要分層

在這裏插入圖片描述

1.2 數據集市與數據倉庫概念

在這裏插入圖片描述

1.3 數倉命名規範

1.3.1 表命名
  • ODS層命名爲ods_表名
  • DWD層命名爲dwd_dim/fact_表名
  • DWS層命名爲dws_表名
  • DWT層命名爲dwt_購物車
  • ADS層命名爲ads_表名
  • 臨時表命名爲xxx_tmp
  • 用戶行爲表,以log爲後綴
1.3.2 腳本命名
  • 數據源_to_目標_db/log.sh
  • 用戶行爲腳本以log爲後綴;業務數據腳本以db爲後綴

二、數倉理論

2.1 範式理論

2.1.1 範式概念

1)定義
範式可以理解爲設計一張數據表的表結構,符合的標準級別,即表的規範和要求

2)優點
關係型數據庫設計時,遵照一定的規範要求,目的在於降低數據的冗餘性

爲什麼要降低數據冗餘性?

  • (1)十幾年前,磁盤很貴,爲了減少磁盤存儲。
  • (2)以前沒有分佈式系統,都是單機,只能增加磁盤,磁盤個數也是有限的
  • (3)一次修改,需要修改多個表,很難保證數據一致性

3)缺點
範式的缺點是獲取數據時,需要通過 Join 拼接出最後的數據

4)分類
目前業界範式有:第一範式(1NF)、第二範式(2NF)、第三範式(3NF)、巴斯-科德範式(BCNF)、第四範式(4NF)、第五範式(5NF)

2.1.2 函數依賴

在這裏插入圖片描述

2.1.3 三範式區分

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

2.2 關係建模與維度建模

當今的數據處理大致可以分成兩大類:聯機事務處理 OLTP(on-line transactionprocessing)、聯機分析處理 OLAP(On-Line Analytical Processing)

  • OLTP 是傳統的關係型數據庫的主要應用,主要是基本的、日常的事務處理,例如銀行交易

  • OLAP 是數據倉庫系統的主要應用,支持複雜的分析操作,側重決策支持,並且提供直觀易懂的查詢結果

二者的主要區別對比如下表所示👇
在這裏插入圖片描述

2.2.1 關係建模

關係模型如下圖所示,嚴格遵循第三範式(3NF),從圖中可以看出,較爲鬆散、零碎,
物理表數量多,而數據冗餘程度低
由於數據分佈於衆多的表中,這些數據可以更爲靈活地被應用,功能性較強。關係模型主要應用與 OLTP 系統中,爲了保證數據的一致性以及避免冗餘,所以大部分業務系統的表都是遵循第三範式的
在這裏插入圖片描述

維度模型如下圖所示,主要應用於 OLAP 系統中,通常以某一個事實表爲中心進行表的組織,主要面向業務,特徵是可能存在數據的冗餘,但是能方便的得到數據

關係模型雖然冗餘少,但是在大規模數據,跨表分析統計查詢過程中,會造成多表關
聯,這會大大降低執行效率。所以通常我們採用維度模型建模,把相關各種表整理成兩種:
事實表維度表兩種
在這裏插入圖片描述

2.2.2 維度建模

在維度建模的基礎上又分爲三種模型:星型模型、雪花模型、星座模型
在這裏插入圖片描述
在這裏插入圖片描述

2.3 維度表和事實表

2.3.1 維度表

維度表:一般是對事實的描述信息。每一張維表對應現實世界中的一個對象或者概念。
例如:用戶、商品、日期、地區等。
維表的特徵:

  • 維表的範圍很寬(具有多個屬性、列比較多)
  • 跟事實表相比,行數相對較小:通常< 10 萬條
  • 內容相對固定:編碼表

時間維度表:
在這裏插入圖片描述

2.3.2 事實表

事實表中的每行數據代表一個業務事件(下單、支付、退款、評價等)。
“事實”這個術語表示的是業務事件的度量值(可統計次數、個數、件數、金額等)
例如,訂單事件中的下單金額

每一個事實表的行包括:具有可加性的數值型的度量值、與維表相連接的外鍵、通常具
有兩個和兩個以上的外鍵、外鍵之間表示維表之間多對多的關係

事實表的特徵:

  • 非常的大
  • 內容相對的窄:列數較少
  • 經常發生變化,每天會新增加很多

1)事務型事實表
以每個事務或事件爲單位,例如一個銷售訂單記錄,一筆支付記錄等,作爲事實表裏的一行數據。一旦事務被提交,事實表數據被插入,數據就不再進行更改,其更新方式爲增量更新

2)週期型快照事實表
週期型快照事實表中不會保留所有數據,只保留固定時間間隔的數據,例如每天或者每月的銷售額,或每月的賬戶餘額等

3)累積型快照事實表
累計快照事實表用於跟蹤業務事實的變化。例如,數據倉庫中可能需要累積或者存儲訂單從下訂單開始,到訂單商品被打包、運輸、和簽收的各個業務階段的時間點數據來跟蹤訂單聲明週期的進展情況。當這個業務過程進行時,事實表的記錄也要不斷更新
在這裏插入圖片描述

2.4 數據倉庫建模(絕對重點)

2.4.1 ODS 層
  • (1)保持數據原貌不做任何修改,起到備份數據的作用。
  • (2)數據採用壓縮,減少磁盤存儲空間(例如:原始數據 100G,可以壓縮到 10G 左
    右)
  • (3)創建分區表,防止後續的全表掃描
2.4.2 DWD 層

DWD 層需構建維度模型,一般採用星型模型,呈現的狀態一般爲星座模型

維度建模一般按照以下四個步驟:
選擇業務過程 → 聲明粒度 → 確認維度 → 確認事實

  • (1)選擇業務過程
    在業務系統中,挑選我們感興趣的業務線,比如下單業務,支付業務,退款業務,物流業務,一條業務線對應一張事實表

  • (2)聲明粒度
    數據粒度指數據倉庫的數據中保存數據的細化程度或綜合程度的級別。
    聲明粒度意味着精確定義事實表中的一行數據表示什麼,應該儘可能選擇最小粒度,以此來應各種各樣的需求
    典型的粒度聲明如下:
    訂單中,每個商品項作爲下單事實表中的一行,粒度爲每次下單
    每週的訂單次數作爲一行,粒度就是每週下單
    每月的訂單次數作爲一行,粒度就是每月下單

  • (3)確定維度
    維度的主要作用是描述業務是事實,主要表示的是“誰,何處,何時”等信息

  • (4)確定事實
    此處的“事實”一詞,指的是業務中的度量值,例如訂單金額、下單次數等
    在 DWD 層,以業務過程爲建模驅動,基於每個具體業務過程的特點,構建最細粒度的明細層事實表。事實表可做適當的寬表化處理

在這裏插入圖片描述
在這裏插入圖片描述
至此,數倉的維度建模已經完畢,DWS、DWT 和 ADS 和 維度建模已經沒有關係了
DWS 和 DWT 都是建寬表,寬表都是按照主題去建。主題相當於觀察問題的角度,對應着維度表

2.4.3 DWS 層

統計各個主題對象的當天行爲,服務於 DWT 層的主題寬表,以及一些業務明細數據,應對特殊需求(例如,購買行爲,統計商品復購率)
在這裏插入圖片描述

2.4.4 DWT 層

以分析的主題對象爲建模驅動,基於上層的應用和產品的指標需求,構建主題對象的全量寬表
在這裏插入圖片描述

2.4.5 ADS 層

對電商系統各大主題指標分別進行分析

三、數倉搭建 - ODS 層

  • 1)保持數據原貌不做任何修改,起到備份數據的作用
  • 2)數據採用 LZO 壓縮,減少磁盤存儲空間。100G 數據可以壓縮到 10G 以內
  • 3)創建分區表,防止後續的全表掃描,在企業開發中大量使用分區表。
  • 4)創建外部表。在企業開發中,除了自己用的臨時表,創建內部表外,絕大多數場景都是創建外部表

3.1 創建數據庫

1)顯示數據庫

show databases;

2)創建數據庫

create database gmall;

3)使用數據庫

use gmall;

3.2 ODS 層(用戶行爲數據)

在這裏插入圖片描述

3.2.1 創建啓動日誌表 ods_start_log

在這裏插入圖片描述
1)創建輸入數據是 lzo, 輸出是 text,支持 json 解析的分區表

drop table if exists ods_start_log;
create external table ods_start_log (`line` string)
PARTITIONED BY (`dt` string)
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION '/warehouse/gmall/ods/ods_start_log';

說明 Hive 的 LZO 壓縮:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LZO

2)加載數據

load data inpath '/origin_data/gmall/log/topic_start/2020-03-10' into table gmall.ods_start_log partition(dt='2020-03-10');

注意:時間格式都配置成 YYYY-MM-DD 格式,這是 Hive 默認支持的時間格式

3)查看是否加載成功

select * from ods_start_log where dt='2020-03-10' limit 2;

4)爲 lzo 壓縮文件創建索引

hadoop jar /opt/modules/hadoop/share/hadoop/common/hadoop-lzo-0.4.20.jar
com.hadoop.compression.lzo.DistributedLzoIndexer
/warehouse/gmall/ods/ods_start_log/dt=2020-03-10
3.2.2 創建事件日誌表 ods_event_log

在這裏插入圖片描述
1)創建輸入數據是 lzo ,輸出是 text,支持 json 解析的分區表

drop table if exists ods_event_log;
create external table ods_event_log(`line` string)
PARTITIONED BY (`dt` string)
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION '/warehouse/gmall/ods/ods_event_log';

2)加載數據

load data inpath '/origin_data/gmall/log/topic_event/2020-03-10'
into table gmall.ods_event_log partition(dt='2020-03-10');

注意:時間格式都配置成 YYYY-MM-DD 格式,這是 Hive 默認支持的時間格式

3)查看是否加載成功

select * from ods_event_log where dt="2020-03-10" limit 2;

4)爲 lzo 壓縮文件創建索引

hadoop jar /opt/module/hadoop-2.7.2/share/hadoop/common/hadoop-lzo-0.4.20.jar 
com.hadoop.compression.lzo.DistributedLzoIndexer
/warehouse/gmall/ods/ods_event_log/dt=2020-03-10
3.2.3 Shell 中單引號和雙引號區別

(1)單引號不取變量值
(2)雙引號取變量值
(3)反引號 ` ,執行引號中命令
(4)雙引號內部嵌套單引號,取出變量值
(5)單引號內部嵌套雙引號,不取出變量值

3.2.4 ODS 層加載數據腳本

1) vim hdfs_to_ods_log.sh
在腳本中編寫如下內容

#!/bin/bash
# 定義變量方便修改
APP=gmall
hive=/opt/modules/hive/bin/hive
# 如果是輸入的日期按照取輸入日期;如果沒輸入日期取當前時間的前一天
if [ -n "$1" ] ;then
do_date=$1
else
do_date=`date -d "-1 day" +%F`
fi
echo "===日誌日期爲 $do_date==="
sql="
load data inpath '/origin_data/gmall/log/topic_start/$do_date' overwrite
into table ${APP}.ods_start_log partition(dt='$do_date');
load data inpath '/origin_data/gmall/log/topic_event/$do_date' overwrite
into table ${APP}.ods_event_log partition(dt='$do_date');
"
$hive -e "$sql"
hadoop jar /opt/modules/hadoop/share/hadoop/common/hadoop-lzo-0.4.20.jar
com.hadoop.compression.lzo.DistributedLzoIndexer
/warehouse/gmall/ods/ods_start_log/dt=$do_date
hadoop jar /opt/modules/hadoop/share/hadoop/common/hadoop-lzo-0.4.20.jar
com.hadoop.compression.lzo.DistributedLzoIndexer
/warehouse/gmall/ods/ods_event_log/dt=$do_date

2)增加腳本執行權限

chmod 777 hdfs_to_ods_log.sh

3)腳本使用

hdfs_to_ods_log.sh 2020-03-11

4)查看導入數據

select * from ods_start_log where dt='2020-03-11' limit 2;
select * from ods_event_log where dt='2020-03-11' limit 2;

5)腳本執行時間
企業開發中一般在每日凌晨 30 分~1 點

3.3 ODS 層(業務數據)

在這裏插入圖片描述

3.3.1 訂單表(增量及更新)
drop table if exists ods_order_info;
create external table ods_order_info (
`id` string COMMENT '訂單號',
`final_total_amount` decimal(10,2) COMMENT '訂單金額',
`order_status` string COMMENT '訂單狀態',
`user_id` string COMMENT '用戶 id',
`out_trade_no` string COMMENT '支付流水號',
`create_time` string COMMENT '創建時間',
`operate_time` string COMMENT '操作時間',
`province_id` string COMMENT '省份 ID',
`benefit_reduce_amount` decimal(10,2) COMMENT '優惠金額',
`original_total_amount` decimal(10,2) COMMENT '原價金額',
`feight_fee` decimal(10,2) COMMENT '運費'
) COMMENT '訂單表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_order_info/';
3.3.2 訂單詳情表(增量)
drop table if exists ods_order_detail;
create external table ods_order_detail(
`id` string COMMENT '訂單編號',
`order_id` string COMMENT '訂單號',
`user_id` string COMMENT '用戶 id',
`sku_id` string COMMENT '商品 id',
`sku_name` string COMMENT '商品名稱',
`order_price` decimal(10,2) COMMENT '商品價格',
`sku_num` bigint COMMENT '商品數量',
`create_time` string COMMENT '創建時間'
) COMMENT '訂單詳情表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_order_detail/';
3.3.3 SKU 商品表(全量)
drop table if exists ods_sku_info;
create external table ods_sku_info(
`id` string COMMENT 'skuId',
`spu_id` string COMMENT 'spuid',
`price` decimal(10,2) COMMENT '價格',
`sku_name` string COMMENT '商品名稱',
`sku_desc` string COMMENT '商品描述',
`weight` string COMMENT '重量',
`tm_id` string COMMENT '品牌 id',
`category3_id` string COMMENT '品類 id',
`create_time` string COMMENT '創建時間'
) COMMENT 'SKU 商品表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
3.3.4 用戶表(增量及更新)
drop table if exists ods_user_info;
create external table ods_user_info(
`id` string COMMENT '用戶 id',
`name` string COMMENT '姓名',
`birthday` string COMMENT '生日',
`gender` string COMMENT '性別',
`email` string COMMENT '郵箱',
`user_level` string COMMENT '用戶等級',
`create_time` string COMMENT '創建時間',
`operate_time` string COMMENT '操作時間'
) COMMENT '用戶表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_user_info/';
3.3.5 商品一級分類表(全量)
drop table if exists ods_base_category1;
create external table ods_base_category1(
`id` string COMMENT 'id',
`name` string COMMENT '名稱'
) COMMENT '商品一級分類表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_base_category1/';
3.3.6 商品二級分類表(全量)
drop table if exists ods_base_category2;
create external table ods_base_category2(
`id` string COMMENT ' id',
`name` string COMMENT '名稱',
category1_id string COMMENT '一級品類 id'
) COMMENT '商品二級分類表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_base_category2/';
3.3.7 商品三級分類表(全量)
drop table if exists ods_base_category3;
create external table ods_base_category3(
`id` string COMMENT ' id',
`name` string COMMENT '名稱',
category2_id string COMMENT '二級品類 id') COMMENT '商品三級分類表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_base_category3/';
3.3.8 支付流水錶(增量)
drop table if exists ods_payment_info;
create external table ods_payment_info(
`id` bigint COMMENT '編號',
`out_trade_no` string COMMENT '對外業務編號',
`order_id` string COMMENT '訂單編號',
`user_id` string COMMENT '用戶編號',
`alipay_trade_no` string COMMENT '支付寶交易流水編號',
`total_amount` decimal(16,2) COMMENT '支付金額',
`subject` string COMMENT '交易內容',
`payment_type` string COMMENT '支付類型',
`payment_time` string COMMENT '支付時間'
) COMMENT '支付流水錶'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_payment_info/';
3.3.9 省份表(特殊)
drop table if exists ods_base_province;
create external table ods_base_province (
`id` bigint COMMENT '編號',
`name` string COMMENT '省份名稱',
`region_id` string COMMENT '地區 ID',
`area_code` string COMMENT '地區編碼',
`iso_code` string COMMENT 'iso 編碼'
) COMMENT '省份表'
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_base_province/';
3.3.10 地區表(特殊)
drop table if exists ods_base_region;
create external table ods_base_region (
`id` bigint COMMENT '編號',
`region_name` string COMMENT '地區名稱'
) COMMENT '地區表'
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_base_region/';
3.3.11 品牌表(全量)
drop table if exists ods_base_trademark;
create external table ods_base_trademark (
`tm_id` bigint COMMENT '編號',
`tm_name` string COMMENT '品牌名稱'
) COMMENT '品牌表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_base_trademark/';
3.3.12 訂單狀態表(增量)
drop table if exists ods_order_status_log;
create external table ods_order_status_log (
`id` bigint COMMENT '編號',
`order_id` string COMMENT '訂單 ID',
`order_status` string COMMENT '訂單狀態',
`operate_time` string COMMENT '修改時間'
) COMMENT '訂單狀態表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_order_status_log/';
3.3.13 SPU 商品表(全量)
drop table if exists ods_spu_info;
create external table ods_spu_info(
`id` string COMMENT 'spuid',
`spu_name` string COMMENT 'spu 名稱',
`category3_id` string COMMENT '品類 id',
`tm_id` string COMMENT '品牌 id'
) COMMENT 'SPU 商品表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_spu_info/';
3.3.14 商品評論表(增量)
create external table ods_comment_info(
`id` string COMMENT '編號',
`user_id` string COMMENT '用戶 ID',
`sku_id` string COMMENT '商品 sku',
`spu_id` string COMMENT '商品 spu',
`order_id` string COMMENT '訂單 ID',
`appraise` string COMMENT '評價',
`create_time` string COMMENT '評價時間'
) COMMENT '商品評論表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_comment_info/';
3.3.15 退單表(增量)
drop table if exists ods_order_refund_info;
create external table ods_order_refund_info(
`id` string COMMENT '編號',
`user_id` string COMMENT '用戶 ID',
`order_id` string COMMENT '訂單 ID',
`sku_id` string COMMENT '商品 ID',
`refund_type` string COMMENT '退款類型',
`refund_num` bigint COMMENT '退款件數',
`refund_amount` decimal(16,2) COMMENT '退款金額',
`refund_reason_type` string COMMENT '退款原因類型',
`create_time` string COMMENT '退款時間'
) COMMENT '退單表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_order_refund_info/';
3.3.16 加購表(全量)
drop table if exists ods_cart_info;
create external table ods_cart_info(
`id` string COMMENT '編號',
`user_id` string COMMENT '用戶 id',
`sku_id` string COMMENT 'skuid',
`cart_price` string COMMENT '放入購物車時價格',
`sku_num` string COMMENT '數量',
`sku_name` string COMMENT 'sku 名稱 (冗餘)',
`create_time` string COMMENT '創建時間',
`operate_time` string COMMENT '修改時間',
`is_ordered` string COMMENT '是否已經下單',
`order_time` string COMMENT '下單時間'
) COMMENT '加購表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_cart_info/';
3.3.17 商品收藏表(全量)
drop table if exists ods_favor_info;
create external table ods_favor_info(
`id` string COMMENT '編號',
`user_id` string COMMENT '用戶 id',
`sku_id` string COMMENT 'skuid',
`spu_id` string COMMENT 'spuid',
`is_cancel` string COMMENT '是否取消',
`create_time` string COMMENT '收藏時間',
`cancel_time` string COMMENT '取消時間'
) COMMENT '商品收藏表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_favor_info/';
3.3.18 優惠券領用表(新增及變化)
drop table if exists ods_coupon_use;
create external table ods_coupon_use(
`id` string COMMENT '編號',
`coupon_id` string COMMENT '優惠券 ID',
`user_id` string COMMENT 'skuid',
`order_id` string COMMENT 'spuid',
`coupon_status` string COMMENT '優惠券狀態',
`get_time` string COMMENT '領取時間',
`using_time` string COMMENT '使用時間(下單)',
`used_time` string COMMENT '使用時間(支付)'
) COMMENT '優惠券領用表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_coupon_use/';
3.3.19 優惠券表(全量)
drop table if exists ods_coupon_info;
create external table ods_coupon_info(
`id` string COMMENT '購物券編號',
`coupon_name` string COMMENT '購物券名稱',
`coupon_type` string COMMENT '購物券類型 1 現金券 2 折扣券 3 滿減券 4 滿件打折券',
`condition_amount` string COMMENT '滿額數',
`condition_num` string COMMENT '滿件數',
`activity_id` string COMMENT '活動編號',
`benefit_amount` string COMMENT '減金額',
`benefit_discount` string COMMENT '折扣',
`create_time` string COMMENT '創建時間',
`range_type` string COMMENT '範圍類型 1、商品 2、品類 3、品牌',
`spu_id` string COMMENT '商品 id',
`tm_id` string COMMENT '品牌 id',
`category3_id` string COMMENT '品類 id',
`limit_num` string COMMENT '最多領用次數',
`operate_time` string COMMENT '修改時間',
`expire_time` string COMMENT '過期時間'
) COMMENT '優惠券表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_coupon_info/';
3.3.20 活動表(全量)
drop table if exists ods_activity_info;
create external table ods_activity_info(
`id` string COMMENT '編號',
`activity_name` string COMMENT '活動名稱',
`activity_type` string COMMENT '活動類型',
`start_time` string COMMENT '開始時間',
`end_time` string COMMENT '結束時間',
`create_time` string COMMENT '創建時間'
) COMMENT '活動表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_activity_info/';
3.3.21 活動訂單關聯表(增量)
drop table if exists ods_activity_order;
create external table ods_activity_order(
`id` string COMMENT '編號',
`activity_id` string COMMENT '優惠券 ID',
`order_id` string COMMENT 'skuid',
`create_time` string COMMENT '領取時間'
) COMMENT '活動訂單關聯表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_activity_order/';
3.3.22 優惠規則表(全量)
drop table if exists ods_activity_rule;
create external table ods_activity_rule(
`id` string COMMENT '編號',
`activity_id` string COMMENT '活動 ID',
`condition_amount` string COMMENT '滿減金額',
`condition_num` string COMMENT '滿減件數',
`benefit_amount` string COMMENT '優惠金額',
`benefit_discount` string COMMENT '優惠折扣',
`benefit_level` string COMMENT '優惠級別'
) COMMENT '優惠規則表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_activity_rule/';
3.3.23 編碼字典表(全量)
drop table if exists ods_base_dic;
create external table ods_base_dic(
`dic_code` string COMMENT '編號',
`dic_name` string COMMENT '編碼名稱',
`parent_code` string COMMENT '父編碼',
`create_time` string COMMENT '創建日期',
`operate_time` string COMMENT '操作日期'
) COMMENT '編碼字典表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_base_dic/';
3.3.24 ODS 層加載數據腳本

1)vim hdfs_to_ods_db.sh
在腳本中填寫如下內容

#!/bin/bash
APP=gmall
hive=/opt/modules/hive/bin/hive
# 如果是輸入的日期按照取輸入日期;如果沒輸入日期取當前時間的前一天
if [ -n "$2" ] ;then
do_date=$2
else
do_date=`date -d "-1 day" +%F`
fi
sql1="
load data inpath '/origin_data/$APP/db/order_info/$do_date' OVERWRITE into table
${APP}.ods_order_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/order_detail/$do_date' OVERWRITE into table
${APP}.ods_order_detail partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/sku_info/$do_date' OVERWRITE into table
${APP}.ods_sku_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/user_info/$do_date' OVERWRITE into table
${APP}.ods_user_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/payment_info/$do_date' OVERWRITE into table
${APP}.ods_payment_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/base_category1/$do_date' OVERWRITE into table
${APP}.ods_base_category1 partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/base_category2/$do_date' OVERWRITE into table
${APP}.ods_base_category2 partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/base_category3/$do_date' OVERWRITE into table
${APP}.ods_base_category3 partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/base_trademark/$do_date' OVERWRITE into table
${APP}.ods_base_trademark partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/activity_info/$do_date' OVERWRITE into table
${APP}.ods_activity_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/activity_order/$do_date' OVERWRITE into table
${APP}.ods_activity_order partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/cart_info/$do_date' OVERWRITE into table
${APP}.ods_cart_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/comment_info/$do_date' OVERWRITE into table
${APP}.ods_comment_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/coupon_info/$do_date' OVERWRITE into table
${APP}.ods_coupon_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/coupon_use/$do_date' OVERWRITE into table
${APP}.ods_coupon_use partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/favor_info/$do_date' OVERWRITE into table
${APP}.ods_favor_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/order_refund_info/$do_date' OVERWRITE into table
${APP}.ods_order_refund_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/order_status_log/$do_date' OVERWRITE into table
${APP}.ods_order_status_log partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/spu_info/$do_date' OVERWRITE into table
${APP}.ods_spu_info partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/activity_rule/$do_date' OVERWRITE into table
${APP}.ods_activity_rule partition(dt='$do_date');
load data inpath '/origin_data/$APP/db/base_dic/$do_date' OVERWRITE into table
${APP}.ods_base_dic partition(dt='$do_date');
"
sql2="
load data inpath '/origin_data/$APP/db/base_province/$do_date' OVERWRITE into table
${APP}.ods_base_province;
load data inpath '/origin_data/$APP/db/base_region/$do_date' OVERWRITE into table
${APP}.ods_base_region;
"
case $1 in
"first"){
$hive -e "$sql1"
$hive -e "$sql2"
};;
"all"){
$hive -e "$sql1"
};;
esac

2)修改權限

 chmod 770 hdfs_to_ods_db.sh

3)初次導入

hdfs_to_ods_db.sh first 2020-03-10

4)每日導入

hdfs_to_ods_db.sh all 2020-03-11

5)測試數據是否導入成功

select * from ods_order_detail where dt='2020-03-11';

結束語

至此,數倉概論以及ODS、DWD層已經描述和搭建已經完成!博主將會在下一章開啓DWD、DWS、DWT的數據清洗以及搭建的過程!


都看到這裏了,點贊評論一下吧!!!

在這裏插入圖片描述

點擊查看👇

敬請期待!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章