數(shù)倉項(xiàng)目02:DWD明細(xì)粒度事實(shí)層

2. 第二層DW主題層

2.1 DWD明細(xì)層

2.1.1 用戶主題

DWD明細(xì)粒度事實(shí)層Hive數(shù)據(jù)庫建庫建表:
創(chuàng)建Hive庫并進(jìn)入:
create database if not exists dwd_nshop;
use dwd_nshop;

在DWD層開始入庫之前,我們有一張通用字典表,在mysql已經(jīng)導(dǎo)入,為后面的數(shù)據(jù)篩選做條件過濾:


2.1.1.1 用戶啟動(dòng)日志表【DWD】
CREATE external TABLE
IF NOT EXISTS dwd_nshop.dwd_nshop_actlog_launch (
user_id string COMMENT '用戶id',
device_num string COMMENT '設(shè)備號(hào)',
device_type string COMMENT '設(shè)備類型',
os string COMMENT '手機(jī)系統(tǒng)',
os_version string COMMENT '手機(jī)系統(tǒng)版本',
manufacturer string COMMENT '手機(jī)制造商',
carrier string COMMENT '電信運(yùn)營商',
network_type string COMMENT '網(wǎng)絡(luò)類型',
area_code string COMMENT '地區(qū)編碼',
launch_time_segment string COMMENT '啟動(dòng)時(shí)間段',
ct BIGINT COMMENT '產(chǎn)生時(shí)間'
) partitioned BY (bdp_day string) stored AS parquet location '/data/nshop/dw/user/dwd_nshop_actlog_launch/';

從ods層事件表里選擇action=02(啟動(dòng))的事件數(shù)據(jù)導(dǎo)入即可:

insert overwrite table dwd_nshop.dwd_nshop_actlog_launch partition(bdp_day="20200618")
select
customer_id,
device_num,
device_type,
os,
os_version,
manufacturer,
carrier,
network_type,
area_code,
from_unixtime(cast(ct/1000 as int),'HH') as launch_time_segment,
ct
from
ods_nshop.ods_nshop_01_useractlog
where
bdp_day = "20200618"
and 
action = "02"
2.1.1.2 用戶產(chǎn)品瀏覽表【DWD】
CREATE external TABLE
IF NOT EXISTS dwd_nshop.dwd_nshop_actlog_pdtview (
user_id string COMMENT '用戶id',
device_num string COMMENT '設(shè)備號(hào)',
device_type string COMMENT '設(shè)備類型',
os string COMMENT '手機(jī)系統(tǒng)',
os_version string COMMENT '手機(jī)系統(tǒng)版本',
manufacturer string COMMENT '手機(jī)制造商',
carrier string COMMENT '電信運(yùn)營商',
network_type string COMMENT '網(wǎng)絡(luò)類型',
area_code string COMMENT '地區(qū)編碼',
target_id string COMMENT '產(chǎn)品ID',
ct BIGINT COMMENT '產(chǎn)生時(shí)間'
) partitioned BY (bdp_day string) stored AS parquet location '/data/nshop/dw/user/dwd_nshop_actlog_pdtview/'

從ods層的事件表里篩選出action為07或08的數(shù)據(jù)予借,即為用戶瀏覽數(shù)據(jù):

insert overwrite table dwd_nshop.dwd_nshop_actlog_pdtview partition(bdp_day="20200618")
select
customer_id,
device_num,
device_type,
os,
os_version,
manufacturer,
carrier,
network_type,
area_code,
get_json_object(extinfo,'$.target_id')as target_id,
ct
from
ods_nshop.ods_nshop_01_useractlog
where
bdp_day = "20200618"
and 
action in('07','08')
2.1.1.3 用戶產(chǎn)品查詢表【DWD】
CREATE external TABLE
IF NOT EXISTS dwd_nshop.dwd_nshop_actlog_pdtsearch (
user_id string COMMENT '用戶id',
device_num string COMMENT '設(shè)備號(hào)',
device_type string COMMENT '設(shè)備類型',
os string COMMENT '手機(jī)系統(tǒng)',
os_version string COMMENT '手機(jī)系統(tǒng)版本',
manufacturer string COMMENT '手機(jī)制造商',
carrier string COMMENT '電信運(yùn)營商',
network_type string COMMENT '網(wǎng)絡(luò)類型',
area_code string COMMENT '地區(qū)編碼',
target_order string COMMENT '查詢排序方式',
target_keys string COMMENT '查詢內(nèi)容',
target_id string COMMENT '產(chǎn)品ID',
ct BIGINT COMMENT '產(chǎn)生時(shí)間'
) partitioned BY (bdp_day string) stored AS parquet location '/data/nshop/dw/user/dwd_nshop_actlog_pdtsearch/';

查看公共字典表:




我們知道轴或,選擇action為05(交互)和event_type為01(瀏覽)或04(滑動(dòng))的行為是用戶查詢行為,因此寫出下列sql語句

insert overwrite table dwd_nshop.dwd_nshop_actlog_pdtsearch partition(bdp_day="20200618")
select
customer_id,
device_num,
device_type,
os,
os_version,
manufacturer,
carrier,
network_type,
area_code,
get_json_object(extinfo,'$.target_order')as target_order,
get_json_object(extinfo,'$.target_keys')as target_keys,
get_json_object(extinfo,'$.target_id')as target_id,
ct
from
ods_nshop.ods_nshop_01_useractlog
--lateral view explode(split(regexp_replace(get_json_object(extinfo,'$target_ids'),'[\\[\\"\\]]',''),','))t as target_id
where
bdp_day = "20200618"
and 
action = '05'
and
event_type in('01','04') 
2.1.1.4 用戶產(chǎn)品關(guān)注表【DWD】
CREATE external TABLE
IF NOT EXISTS dwd_nshop.dwd_actlog_product_comment (
user_id string COMMENT '用戶id',
device_num string COMMENT '設(shè)備號(hào)',
device_type string COMMENT '設(shè)備類型',
os string COMMENT '手機(jī)系統(tǒng)',
os_version string COMMENT '手機(jī)系統(tǒng)版本',
manufacturer string COMMENT '手機(jī)制造商',
carrier string COMMENT '電信運(yùn)營商',
network_type string COMMENT '網(wǎng)絡(luò)類型',
area_code string COMMENT '地區(qū)編碼',
target_id string COMMENT '產(chǎn)品ID',
ct BIGINT COMMENT '產(chǎn)生時(shí)間'
) partitioned BY (bdp_day string) stored AS parquet location '/data/nshop/dw/user/dwd_actlog_product_comment/';

選擇action ='05'(交互)昌跌,event_type='02'(點(diǎn)擊),并且target_action='01'(店鋪關(guān)注)的事件即可:

with log_attend as(
select
customer_id,
device_num,
device_type,
os,
os_version,
manufacturer,
carrier,
network_type,
area_code,
get_json_object(extinfo,'$.target_action')as target_action,
get_json_object(extinfo,'$.target_id')as target_id,
extinfo,
ct,
bdp_day
from
ods_nshop.ods_nshop_01_useractlog
where
bdp_day = "20200618"
and 
action ='05'
and 
event_type='02'
)

insert overwrite table dwd_nshop.dwd_actlog_product_comment partition(bdp_day="20200618")
select
customer_id,
device_num,
device_type,
os,
os_version,
manufacturer,
carrier,
network_type,
area_code,
target_id,
ct
from 
log_attend where target_action='01'
2.1.2 交易主題
2.1.2.1 交易訂單明細(xì)流水表【DWD】
CREATE external TABLE
IF NOT EXISTS dwd_nshop.dwd_nshop_orders_details (
order_id string COMMENT '訂單ID',
order_status INT COMMENT '訂單狀態(tài):5已收貨(完成)|6投訴 7退貨',
supplier_code string COMMENT '店鋪ID',
product_code string COMMENT '商品ID',
customer_id string COMMENT '用戶id',
device_type string COMMENT '設(shè)備類型',
os string COMMENT '手機(jī)系統(tǒng)',
os_version string COMMENT '手機(jī)系統(tǒng)版本',
manufacturer string COMMENT '手機(jī)制造商',
carrier string COMMENT '電信運(yùn)營商',
user_areacode string COMMENT '用戶所在地區(qū)',
consignee_zipcode string COMMENT '收貨人地址',
pay_type string COMMENT '支付類型:線上支付 10 網(wǎng)上銀行 11 微信 12 支付寶 | 線下支 付(貨到付款) 20 ',
pay_count INT COMMENT '支付次數(shù)',
product_price DECIMAL (5, 1) COMMENT '購買商品單價(jià)',
weighing_cost DECIMAL (2, 1) COMMENT '商品加權(quán)價(jià)格',
district_money DECIMAL (4, 1) COMMENT '優(yōu)惠金額',
is_activity int COMMENT '1:參加活動(dòng)|0:沒有參加活動(dòng)',
order_ctime BIGINT COMMENT '創(chuàng)建時(shí)間'
) partitioned BY (bdp_day string) stored AS parquet location '/data/nshop/dwd/order/dwd_nshop_orders_details/';

訂單明細(xì)流水表的數(shù)據(jù)主要是從ods層的訂單詳細(xì)表里抽取照雁,結(jié)合公共產(chǎn)品維度表查出產(chǎn)品相關(guān)信息避矢,另外還結(jié)合訂單支付記錄表的數(shù)據(jù),查出支付相關(guān)信息囊榜,sql語句如下:

with tborder as(
  select
    order_id,
    order_status,
    customer_id,
    consignee_zipcode,
    pay_type,
    order_ctime,
    from_unixtime(cast(order_ctime/1000 as bigint),'yyyyMMdd') as bdp_day
  from ods_nshop.ods_02_orders
  where 
    from_unixtime(cast(order_ctime/1000 as bigint),'yyyyMMdd') = '20200618'
),
tbdetail as(
  select 
    a.order_id,
    a.product_id,
    a.product_cnt,
    a.weighing_cost,
    a.district_money,
    a.is_activity,
    b.supplier_code,
    b.product_price,
    from_unixtime(cast(a.order_detail_ctime/1000 as bigint),'yyyyMMdd') as bdp_day
  from ods_nshop.ods_02_order_detail a
  join ods_nshop.dim_pub_product b 
  on a.product_id=b.product_code 
  where   
from_unixtime(cast(a.order_detail_ctime/1000 as bigint),'yyyyMMdd')='20200618'
),
pays as (
    select 
        count(*) as pay_count,
        order_id,
        from_unixtime(cast(pay_ctime/1000 as bigint),'yyyyMMdd') as bdp_day
    from  ods_nshop.ods_02_orders_pay_records 
    where 
       from_unixtime(cast(pay_ctime/1000 as bigint),'yyyyMMdd')='20200618'
    group by 
     order_id,
    from_unixtime(cast(pay_ctime/1000 as bigint),'yyyyMMdd')
)
insert overwrite table dwd_nshop.dwd_nshop_orders_details partition (bdp_day)
select 
    tborder.order_id,
    tborder.order_status,
    tbdetail.supplier_code,
    tbdetail.product_id,
    tborder.customer_id,
    tborder.consignee_zipcode,
    tborder.pay_type,
    pays.pay_count,
    tbdetail.product_price,
    tbdetail.product_cnt,
    tbdetail.weighing_cost,
    tbdetail.district_money,
    tbdetail.is_activity,
    tborder.order_ctime,
    tborder.bdp_day
from tborder join tbdetail 
on tborder.order_id=tbdetail.order_id
join pays 
on tbdetail.order_id=pays.order_id

2.2.1 訂單支付表【DWD】

CREATE external TABLE
IF NOT EXISTS dwd_nshop.dwd_order_pay (
pay_id string COMMENT '訂單支付記錄ID',
order_id string COMMENT '訂單ID',
customer_id string COMMENT '用戶ID',
user_id string COMMENT '用戶id',
os string COMMENT '手機(jī)系統(tǒng)',
os_version string COMMENT '手機(jī)系統(tǒng)版本',
manufacturer string COMMENT '手機(jī)制造商',
carrier string COMMENT '電信運(yùn)營商',
network_type string COMMENT '網(wǎng)絡(luò)類型',
area_code string COMMENT '地區(qū)編碼',
longitude string COMMENT '經(jīng)度',
latitude string COMMENT '緯度',
page_id string COMMENT '行為所在頁',
target_action string COMMENT '參考點(diǎn)擊目標(biāo)類型說明',
target_id string COMMENT '訂單id',
target_price DOUBLE COMMENT '訂單價(jià)格',
target_status string COMMENT '支付狀態(tài):0 未支付| 1 支付中 | 2 已支付',
target_type string COMMENT '支付類型:線上支付 10 網(wǎng)上銀行 11 微信 12 支付寶 | 線 下支付(貨到付款) 20 ',
ct BIGINT COMMENT '產(chǎn)生時(shí)間'
) partitioned BY (bdp_day string) stored AS parquet location '/data/nshop/dwd/order/dwd_order_pay/'
2.1.3 營銷活動(dòng)主題

廣告投放數(shù)據(jù)表【DWD】

CREATE external TABLE
IF NOT EXISTS dwd_nshop.dwd_nshop_releasedatas (
customer_id string COMMENT '用戶id',
device_num string COMMENT '設(shè)備號(hào)',
device_type string COMMENT '設(shè)備類型',
os string COMMENT '手機(jī)系統(tǒng)',
os_version string COMMENT '手機(jī)系統(tǒng)版本',
manufacturer string COMMENT '手機(jī)制造商',
area_code string COMMENT '地區(qū)編碼',
release_sid string COMMENT '投放請求id',
release_ip string COMMENT '投放方ip',
release_session string COMMENT '投放會(huì)話id',
release_sources string COMMENT '投放渠道',
release_category string COMMENT '投放瀏覽產(chǎn)品分類',
release_product string COMMENT '投放瀏覽產(chǎn)品',
release_product_page string COMMENT '投放瀏覽產(chǎn)品頁',
ct BIGINT COMMENT '創(chuàng)建時(shí)間'
) partitioned BY (bdp_day string) stored AS parquet location '/data/nshop/dwd/release/dwd_nshop_releasedatas/'

這里需要關(guān)聯(lián)兩張中間表,一個(gè)是產(chǎn)品表亥宿,查出產(chǎn)品分類卸勺,一個(gè)是頁面布局表,選擇產(chǎn)品頁烫扼,兩張表結(jié)構(gòu)如下:


頁面布局表

插入數(shù)據(jù):

with tbrelease as(
select
c.customer_id,
r.device_num ,
r.device_type ,
r.os , 
r.os_version ,
r.manufacturer ,
r.area_code ,
r.release_sid ,
parse_url(concat("http://127.0.0.1:8088/release?",'',r.release_params),'QUERY','ip')as release_ip,
r.release_session,
r.release_sources,   
parse_url(concat("http://127.0.0.1:8088/release?",'',r.release_params),'QUERY','productPage')as release_product_page,
r.ct
from ods_nshop.ods_01_releasedatas r
join ods_nshop.ods_02_customer c
on r.customer_id=c.customer_id 
where 
bdp_day='20200618'
)
insert overwrite table dwd_nshop.dwd_nshop_releasedatas partition(bdp_day='20200618')
select 
a.customer_id,
a.device_num ,
a.device_type ,
a.os , 
a.os_version ,
a.manufacturer, 
a.area_code ,
a.release_sid ,
a.release_ip,
a.release_session,
a.release_sources, 
f.category_code release_category,
p.page_target release_product,
a.release_product_page,
a.ct
from tbrelease a
join dim_nshop.dim_pub_page p 
on a.release_product_page=p.page_code and p.page_type='4'
join dim_nshop.dim_pub_product f
on p.page_code=f.product_code;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末曙求,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子映企,更是在濱河造成了極大的恐慌悟狱,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件堰氓,死亡現(xiàn)場離奇詭異挤渐,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)双絮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門浴麻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人囤攀,你說我怎么就攤上這事软免。” “怎么了焚挠?”我有些...
    開封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵膏萧,是天一觀的道長。 經(jīng)常有香客問我蝌衔,道長榛泛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任胚委,我火速辦了婚禮挟鸠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘亩冬。我一直安慰自己艘希,他們只是感情好硼身,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著覆享,像睡著了一般佳遂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上撒顿,一...
    開封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天丑罪,我揣著相機(jī)與錄音,去河邊找鬼凤壁。 笑死吩屹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的拧抖。 我是一名探鬼主播煤搜,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼唧席!你這毒婦竟也來了擦盾?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤淌哟,失蹤者是張志新(化名)和其女友劉穎迹卢,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體徒仓,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡腐碱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蓬衡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喻杈。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖狰晚,靈堂內(nèi)的尸體忽然破棺而出筒饰,到底是詐尸還是另有隱情,我是刑警寧澤壁晒,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布瓷们,位于F島的核電站,受9級(jí)特大地震影響秒咐,放射性物質(zhì)發(fā)生泄漏谬晕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一携取、第九天 我趴在偏房一處隱蔽的房頂上張望攒钳。 院中可真熱鬧,春花似錦雷滋、人聲如沸不撑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽焕檬。三九已至姆坚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間实愚,已是汗流浹背兼呵。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留腊敲,地道東北人击喂。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像碰辅,于是被迫代替她去往敵國和親茫负。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容