(8)用戶行爲數據寬表導入腳本8

3.5.3用戶行爲數據寬表導入腳本

1)在/home/atguigu/bin目錄下創建腳本dws_db_wide.sh
[atguigu@hadoop102 bin]$ vim dws_db_wide.sh
在腳本中填寫如下內容
#!/bin/bash

定義變量方便修改

APP=gmall
hive=/opt/module/hive/bin/hive

如果是輸入的日期按照取輸入日期;如果沒輸入日期取當前時間的前一天

if [ -n “$1” ] ;then
do_date=$1
else
do_date=date -d "-1 day" +%F
fi

sql="

with
tmp_order as
(
select
user_id,
sum(oi.total_amount) order_amount,
count() order_count
from "APP".dwdorderinfooiwheredateformat(oi.createtime,yyyyMMdd)=APP".dwd_order_info oi where date_format(oi.create_time,'yyyy-MM-dd')='do_date’
group by user_id
) ,
tmp_payment as
(
select
user_id,
sum(pi.total_amount) payment_amount,
count(
) payment_count
from "APP".dwdpaymentinfopiwheredateformat(pi.paymenttime,yyyyMMdd)=APP".dwd_payment_info pi where date_format(pi.payment_time,'yyyy-MM-dd')='do_date’
group by user_id
),
tmp_comment as
(
select
user_id,
count(*) comment_count
from "APP".dwdcommentlogcwheredateformat(c.dt,yyyyMMdd)=APP".dwd_comment_log c where date_format(c.dt,'yyyy-MM-dd')='do_date’
group by user_id
)

Insert overwrite table "APP".dwsuseractionpartition(dt=APP".dws_user_action partition(dt='do_date’)
select
user_actions.user_id,
sum(user_actions.order_count),
sum(user_actions.order_amount),
sum(user_actions.payment_count),
sum(user_actions.payment_amount),
sum(user_actions.comment_count)
from
(
select
user_id,
order_count,
order_amount,
0 payment_count,
0 payment_amount,
0 comment_count
from tmp_order

union all
select

user_id,
0 order_count,
0 order_amount,
payment_count,
payment_amount,
0
from tmp_payment

union all
select

user_id,
0 order_count,
0 order_amount,
0 payment_count,
0 payment_amount,
comment_count
from tmp_comment
) user_actions
group by user_id;
"

hivee"hive -e "sql"
2)增加腳本執行權限
[atguigu@hadoop102 bin]$ chmod 777 dws_db_wide.sh
3)執行腳本導入數據
[atguigu@hadoop102 bin]$dws_db_wide.sh 2019-02-11
4)查看導入數據
hive (gmall)>
select * from dws_user_action where dt=‘2019-02-11’ limit 2;

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