本文是基于實現(xiàn)第一階段的業(yè)務(wù)目標(biāo)后值纱,使用數(shù)據(jù)倉庫實現(xiàn)的自主更新可視化看板概作。
項目目錄
- 項目介紹
- 分析思路
- 數(shù)據(jù)自動化處理過程
- 可視化報表搭建
一亡哄、項目介紹
基于已經(jīng)實現(xiàn)第一階段的業(yè)務(wù)需求后叫倍,公司希望業(yè)務(wù)部門能夠?qū)崿F(xiàn)自主分析象颖,從而實現(xiàn)對市場的快速判斷佩厚,因此,要求數(shù)據(jù)部門和業(yè)務(wù)部門溝通需求的自主分析的數(shù)據(jù)指標(biāo)说订,從而實現(xiàn)可視化看板抄瓦。
二、分析思路
(一)項目操作流程
(二)觀察數(shù)據(jù)
根據(jù)業(yè)務(wù)需求陶冷,從mysql數(shù)據(jù)庫中梳理出三張表分析:
(三)數(shù)據(jù)指標(biāo)分析
三钙姊、數(shù)據(jù)自動化處理過程
(一)Sqoop抽取mysql數(shù)據(jù)到hive
將日期維度表,訂單明細(xì)表埂伦,每日新增用戶表中的數(shù)據(jù)抽取到Hive數(shù)據(jù)庫中
這里是導(dǎo)入訂單明細(xì)表(ods_sales_orders)煞额,這里用訂單明細(xì)表示范,其余兩張表和這個一樣沾谜。
hive -e "truncate table ods.ods_sales_orders"
sqoop import \
--hive-import \ #import 工具從RDBMS向hive的HDFS導(dǎo)入單獨的表
--connect "jdbc:mysql://106.13.128.83:3306/adventure_ods?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&dontTrackOpenResources=true&defaultFetchSize=50000&useCursorFetch=true" \
--driver com.mysql.jdbc.Driver \ #指定要使用的JDBC驅(qū)動程序類
--username frogdataXXXX \#連接mysql的用戶名
--password mimaXXXX \#連接mysql的密碼
--query \#構(gòu)建SQL語句執(zhí)行
"select * from ods_sales_orders where "'$CONDITIONS'" " \
--fetch-size 50000 \#一次從數(shù)據(jù)庫讀取50000個實例膊毁,就是50000條數(shù)據(jù)
--hive-table ods.ods_sales_orders \#導(dǎo)入到hive時用表名ods_sales_orders
--hive-drop-import-delims \#在導(dǎo)入數(shù)據(jù)到hive時,去掉數(shù)據(jù)中的\r\n\013\010這樣的字符
--delete-target-dir \#若目標(biāo)文件存在就刪除他
--target-dir /user/hadoop/sqoop/ods_sales_orders \#目標(biāo)HDFS目錄
-m 1 #遷移過程使用1個map
導(dǎo)入成功后基跑,可以查看一下表中的數(shù)據(jù)
(二)建立數(shù)據(jù)倉庫婚温,對數(shù)據(jù)進(jìn)行聚合
數(shù)據(jù)聚合的思路
在hive中編寫shell腳本,聚合生成dw_order_by_day媳否,dw_amount_diff栅螟,dw_customer_order表
以下是dw_order_by_day的聚合荆秦,其他兩張表步驟是一樣的
hive -e "drop table if exists ods.dw_order_by_day"
hive -e "
CREATE TABLE ods.dw_order_by_day(
create_date string,
is_current_year bigint,
is_last_year bigint,
is_yesterday bigint,
is_today bigint,
is_current_month bigint,
is_current_quarter bigint,
sum_amount double,
order_count bigint)
"
hive -e "
with dim_date as
(select create_date,
is_current_year,
is_last_year,
is_yesterday,
is_today,
is_current_month,
is_current_quarter
from ods.dim_date_df),
sum_day as
(select create_date,
sum(unit_price) as sum_amount,
count(customer_key) as order_count
from ods.ods_sales_orders
group by create_date)
insert into ods.dw_order_by_day
select b.create_date,
b.is_current_year,
b.is_last_year,
b.is_yesterday,
b.is_today,
b.is_current_month,
b.is_current_quarter,
a.sum_amount,
a.order_count
from sum_day as a
inner join dim_date as b
on a.create_date=b.create_date
"
查看聚合后的數(shù)據(jù)
四、在Linux上做定時部署
把定時更新的任務(wù)寫到schedule.py上力图,然后將我們寫好的schedule.py文件掛到后臺
(1)定時更新dw_order_by_day,dw_order_diff,dw_customer_order表的schedule.py文件步绸,導(dǎo)入schedule 模塊實現(xiàn)定時更新,設(shè)定時間是早上6:00更新 代碼如圖所示(部分):
(2)定時任務(wù)掛到linux服務(wù)器后臺搪哪,等待更新
nohup python3 schedule_job.py > schedule_job.log 2>&1 &
nohup :不掛斷地運(yùn)行命令
2>&1 將錯誤輸出到終端靡努,這里將輸出定向到日志文件
& 放在命令到結(jié)尾坪圾,表示后臺運(yùn)行
(3)驗證schedule程序是否順利掛在后臺
ps aux| grep schedule_job.py
五晓折、Sqoop從Hive導(dǎo)出數(shù)據(jù)到mysql
將數(shù)據(jù)從Hadoop(如hive等)導(dǎo)入關(guān)系型數(shù)據(jù)庫導(dǎo)中
步驟1:Sqoop與數(shù)據(jù)庫Server通信,獲取數(shù)據(jù)庫表的元數(shù)據(jù)信息兽泄;
步驟2:并行導(dǎo)入數(shù)據(jù):
將Hadoop上文件劃分成若干個split漓概;
每個split由一個Map Task進(jìn)行數(shù)據(jù)導(dǎo)入
sqoop從hive數(shù)據(jù)庫抽取數(shù)據(jù)到mysql數(shù)據(jù)庫,這里是將dw_order_by_day的數(shù)據(jù)抽取到mysql數(shù)據(jù)庫中,其余兩張表和它方法相同病梢。
sqoop export \
--connect "jdbc:mysql://域名/adventure_dw?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&dontTrackOpenResources=true&defaultfetchSize=50000&useCursorfetch=true" \
--username 數(shù)據(jù)庫賬號\ ##數(shù)據(jù)庫賬號
--password 密碼 \##數(shù)據(jù)庫密碼
--table dw_order_by_day \ ##mysql數(shù)據(jù)庫建好的表
--export-dir /user/hive/warehouse/ods.db/dw_order_by_day \ #hive數(shù)據(jù)庫數(shù)據(jù)路徑胃珍,這個用show create table ods.dw_order_by_day 查hive表的路徑
--columns ##抽取的列 create_date,is_current_year,is_last_year,is_yesterday,is_today,is_current_month,is_current_quart,sum_amount,order_count \
--fields-terminated-by '\001' \ ##hive中被導(dǎo)出的文件字段的分隔符```