用Python、Hive分析Adventure Works Cycles公司

Adventure Works Cycles公司數(shù)據(jù)分析

1 背景

????????????Adventure Works Cycles是AdventureWorks樣本數(shù)據(jù)庫(kù)所虛構(gòu)的公司断部,這是一家大型跨國(guó)制造公司。該公司生產(chǎn)和銷(xiāo)售金屬和復(fù)合材料自行車(chē)到北美,歐洲和亞洲的商業(yè)市場(chǎng)慎式。

Adventure Works Cycle這家公司的客戶主要有兩種:

???????????? 個(gè)體:這些客戶購(gòu)買(mǎi)商品是通過(guò)網(wǎng)上零售店鋪

?????????????商店:這些是從Adventure Works Cycles銷(xiāo)售代表處購(gòu)買(mǎi)轉(zhuǎn)售產(chǎn)品的零售店或批發(fā)店

這家公司主要有下面四個(gè)產(chǎn)品線:

??????????????Adventure Works Cycles 生產(chǎn)的自行車(chē)

??????????????自行車(chē)部件,例如車(chē)輪,踏板或制動(dòng)組件

??????????????從供應(yīng)商處購(gòu)買(mǎi)的自行車(chē)服裝瘪吏,用于轉(zhuǎn)售給Adventure Works Cycles的客戶癣防。

??????????????從供應(yīng)商處購(gòu)買(mǎi)的自行車(chē)配件,用于轉(zhuǎn)售給Adventure Works Cycles客戶掌眠。

2?項(xiàng)目目的

? ? ? ? ? ? ??將數(shù)據(jù)導(dǎo)入Hive數(shù)據(jù)庫(kù)

??????????????探索數(shù)據(jù)庫(kù)并羅列分析指標(biāo)

??????????????匯總數(shù)據(jù)建立數(shù)據(jù)倉(cāng)庫(kù)(銷(xiāo)售主題)

??????????????powerbi可視化

? ????????????制作11月自行車(chē)業(yè)務(wù)分析報(bào)告

項(xiàng)目成果部分圖片:

ppt成果圖片:PPT—11月自行車(chē)業(yè)務(wù)分析報(bào)告

3?數(shù)據(jù)處理

3.1?數(shù)據(jù)來(lái)源

項(xiàng)目數(shù)據(jù)描述:數(shù)據(jù)來(lái)源于adventure Works Cycles公司的的樣本數(shù)據(jù)庫(kù)蕾盯,包括了公司4大應(yīng)用場(chǎng)景的數(shù)據(jù):Sales、Finance蓝丙、Product级遭、Manufacture

3.2?數(shù)據(jù)理解

3.3?數(shù)據(jù)導(dǎo)入?

現(xiàn)有資料是一個(gè)從sqlserver導(dǎo)出的.sql文件,里面有表名渺尘、表字段以及每個(gè)表的csv數(shù)據(jù)挫鸽,利用文件讀寫(xiě)+正則表達(dá)式解析出 hive 建表語(yǔ)句,然后利用 pandas 指定編碼讀取再保存為 csv 文件

1鸥跟、在hive中創(chuàng)建一個(gè)基礎(chǔ)數(shù)據(jù)層庫(kù)adventure_cj掠兄,用來(lái)存放基礎(chǔ)表數(shù)據(jù)

2、在python中利用正則表達(dá)式循環(huán)讀取文件锌雀,獲取表名和字段名

讀取文件

得到以下數(shù)據(jù)

3蚂夕、解析 table_info 字典,用來(lái)創(chuàng)建表

4腋逆、導(dǎo)入數(shù)據(jù)到hive

5婿牍、用遍歷文件和pandas 讀取數(shù)據(jù)使之轉(zhuǎn)換成指定 utf8 編碼格式的代碼

得到utf8編碼格式的csv文件并以|分隔

6、建立數(shù)據(jù)倉(cāng)庫(kù)匯總層

(1)創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù) adventure_dw_cj惩歉,為了方便查閱將adventure_cj中的基礎(chǔ)表遷移過(guò)來(lái)

use adventure_dw_cj;

# 銷(xiāo)售地區(qū)維度表

create table dimsalesterritory as select * from adventure_cj.dimsalesterritory;

# 網(wǎng)絡(luò)銷(xiāo)售表

create table FactInternetSales as select * from adventure_cj.FactInternetSales;

# 產(chǎn)品維度表

create table DimProduct as select * from adventure_cj.DimProduct;

(2)建立事實(shí)表 fact_time 等脂,對(duì)比去年撑蚌、上個(gè)月的銷(xiāo)售額上遥、銷(xiāo)售額數(shù)量的不同

use adventure_dw_cj;

create table fact_time as

SELECT

a.*,

? b.amount? ? ? AS amount_last_year,

? b.order_number AS order_number_last_year,

? c.amount? ? ? AS amount_last_month,

? c.order_number AS order_number_last_month,

? round(((a.amount-c.amount)/c.amount)*100,2)? ? ? ? ? ? ? ? ? AS amount_comp_last_month,

? round(((a.order_number-c.order_number)/c.order_number)*100,2) AS order_number_comp_last_month,

? round(((a.amount-b.amount)/b.amount)*100,2) AS amount_comp_last_year,

? round(((a.order_number-b.order_number)/b.order_number)*100,2) AS order_number_comp_last_year

FROM? (

? SELECT

? ? SalesTerritoryKey,

? ? concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)) AS orderdate,

? ? year(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)))? ? ? ? AS time_YEAR,

? ? QUARTER(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)))? ? ? AS time_QUARTER,

? ? MONTH(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)))? ? ? ? AS time_MONTH,

? ? WEEKofyear(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)))? AS time_WEEK,

? ? count( SalesAmount )? ? ? ? ? ? ? ? ? ? ? ? ? AS order_number,

? ? round(count(SalesAmount)*(0.9+rand ()*0.4),2) AS order_number_forcost,

? ? round( sum( SalesAmount ), 2 )? ? ? ? ? ? ? ? AS amount,

? ? round(sum(SalesAmount)*(0.9+rand ()*0.4),2)? AS amount_forcost,

? ? round(sum(SalesAmount)/count(SalesAmount),2)? AS customerunitprice,

? ? round( avg( TotalProductCost ), 2 )? ? ? ? ? AS per_productcost,

? ? round( avg( TaxAmt ), 2 )? ? ? ? ? ? ? ? ? ? AS per_tax,

? ? round( avg( freight ), 2 )? ? ? ? ? ? ? ? ? ? AS avg_freight

? FROM

? ? adventure_cj.FactInternetSales

? GROUP BY

? ? SalesTerritoryKey,

? ? OrderDateKey

? ) a

? LEFT JOIN (

? ? SELECT

? ? ? SalesTerritoryKey,

? ? ? OrderDateKey,

? ? ? date_add(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)), 365 ) AS orderdate,

? ? ? count( SalesAmount )? ? ? ? ? ? AS order_number,

? ? ? round( sum( SalesAmount ), 2 )? AS amount

? ? FROM

? ? ? adventure_cj.FactInternetSales

? ? GROUP BY

? ? ? SalesTerritoryKey,

? ? ? OrderDateKey

? ) b

ON a.SalesTerritoryKey = b.SalesTerritoryKey? AND a.orderdate = b.orderdate

LEFT JOIN (

? ? SELECT

? ? ? SalesTerritoryKey,

? ? ? OrderDateKey,

? ? ? date_add(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)),30) AS orderdate,

? ? ? count( SalesAmount ) AS order_number,

? ? ? round( sum( SalesAmount ), 2 ) AS amount

? ? FROM

? ? ? adventure_cj.FactInternetSales

? ? GROUP BY

? ? ? SalesTerritoryKey,

? ? ? OrderDateKey

? ) c

ON a.SalesTerritoryKey = c.SalesTerritoryKey? AND a.orderdate = c.orderdate

WHERE

? a.orderdate <= current_date()

ORDER BY

? a.SalesTerritoryKey,

? a.orderdate;

(3)創(chuàng)建事實(shí)表 Factinternet,對(duì)比銷(xiāo)售額争涌、銷(xiāo)售數(shù)量與目標(biāo)的完成率分析

use adventure_dw_cj;

create table Factinternet

as

select a.*,round(a.order_number/a.order_number_forcost,2) as order_number_forcost_comp,

round(a.order_number/a.order_number_forcost,2) as amount_forcost_comp

from (

SELECT

? ? a.OrderDatekey as orderdate,

? ? year(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)))? ? ? AS time_YEAR,

? ? QUARTER(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)))? ? AS time_QUARTER,

? ? MONTH(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)))? ? ? AS time_MONTH,

? ? WEEKofyear(concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2))) AS time_WEEK,

? ? a.SalesTerritoryKey,

? ? b.ProductSubcategoryKey,

? ? count( a.CustomerKey ) AS order_number,

? ? round(count(a.CustomerKey)* ( 0.9+rand ( ) * 0.4 ), 2 ) AS order_number_forcost,

? ? round( sum( a.SalesAmount ), 2 ) AS Amount,

? ? round(sum(SalesAmount)*(0.9+rand ()*0.4),2) AS amount_forcost,

? ? round(sum(a.SalesAmount)/count(a.SalesAmount),2) AS customerunitprice,

? ? round( avg( a.TotalProductCost ), 2 ) AS per_productcost,

? ? round( avg( a.TaxAmt ), 2 ) AS per_tax,

? ? round( avg( a.freight ), 2 ) AS avg_freight

FROM

? ? adventure_cj.FactinternetSales a

LEFT JOIN adventure_ods_lan.DimProduct b ON a.ProductKey = b.ProductKey

where concat_ws('-',substr(orderdatekey,0,4),substr(orderdatekey,5,2),substr(orderdatekey,7,2)) <= current_date()

GROUP BY

? ? a.OrderDatekey,

? ? a.SalesTerritoryKey,

? ? b.ProductSubcategoryKey) a;

7粉楚、每日定時(shí)更新數(shù)據(jù)

模擬基礎(chǔ)數(shù)據(jù)更新,把每天新的銷(xiāo)售數(shù)據(jù)入庫(kù)到基礎(chǔ)庫(kù)中亮垫,并且更新匯總層的數(shù)據(jù)

0 1 * * * sh /root/adventure_cj/update_shell/update_data_everyday.sh? ?#每天1點(diǎn)0分執(zhí)行這個(gè)腳本

4?PowerBI展示數(shù)據(jù)

借助ODBC使用powerBI連接Hive數(shù)據(jù)庫(kù)

目的是從以日模软、月、季饮潦、年為時(shí)間維度燃异,銷(xiāo)售區(qū)域來(lái)分析Adventure Works Cycles這家公司的銷(xiāo)售額、銷(xiāo)量继蜡、客單價(jià)回俐、平均稅費(fèi)逛腿、平均運(yùn)費(fèi)、銷(xiāo)售額完成率仅颇、平均成本鳄逾、銷(xiāo)售大區(qū)占比以及產(chǎn)品銷(xiāo)量

展示連接如下:Adventure_Work


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市灵莲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌殴俱,老刑警劉巖政冻,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異线欲,居然都是意外死亡明场,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)李丰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)苦锨,“玉大人,你說(shuō)我怎么就攤上這事趴泌≈凼妫” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵嗜憔,是天一觀的道長(zhǎng)秃励。 經(jīng)常有香客問(wèn)我,道長(zhǎng)吉捶,這世上最難降的妖魔是什么夺鲜? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮呐舔,結(jié)果婚禮上币励,老公的妹妹穿的比我還像新娘。我一直安慰自己珊拼,他們只是感情好食呻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著澎现,像睡著了一般搁进。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上昔头,一...
    開(kāi)封第一講書(shū)人閱讀 51,274評(píng)論 1 300
  • 那天饼问,我揣著相機(jī)與錄音,去河邊找鬼揭斧。 笑死莱革,一個(gè)胖子當(dāng)著我的面吹牛峻堰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播盅视,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼捐名,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了闹击?” 一聲冷哼從身側(cè)響起镶蹋,我...
    開(kāi)封第一講書(shū)人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎赏半,沒(méi)想到半個(gè)月后贺归,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡断箫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年拂酣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仲义。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡婶熬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出埃撵,到底是詐尸還是另有隱情赵颅,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布暂刘,位于F島的核電站性含,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鸳惯。R本人自食惡果不足惜商蕴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望芝发。 院中可真熱鬧绪商,春花似錦、人聲如沸辅鲸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)独悴。三九已至例书,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間刻炒,已是汗流浹背决采。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留坟奥,地道東北人树瞭。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓拇厢,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親晒喷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子孝偎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

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

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類(lèi): pyspark.sql...
    mpro閱讀 9,451評(píng)論 0 13
  • 關(guān)系型數(shù)據(jù)庫(kù)和SQL SQL語(yǔ)言的三個(gè)部分DML:Data Manipulation Language,數(shù)據(jù)操縱語(yǔ)...
    Awey閱讀 1,947評(píng)論 0 13
  • 乾三連凉敲,坤六斷衣盾,震仰盂,艮覆碗爷抓, 離中虛势决,坎中滿,兌上缺废赞,巽下斷。
    東海翁閱讀 2,265評(píng)論 0 5
  • 年初給自己預(yù)定的每日晨跑的目標(biāo)叮姑,現(xiàn)在回頭看是慘不忍睹唉地。每天沒(méi)有跑步的清晨一過(guò),然后都在給自己打雞血:明天早晨一定要...
    firekeeprunning閱讀 275評(píng)論 1 0
  • 讀書(shū)之用 著名作家群嗤、北大教授曹文軒有一次在給北大中文系新生的入學(xué)演講中,談到他對(duì)閱讀的理解和感悟兵琳。閱讀是一種人生方...
    皋辜格琢閱讀 1,444評(píng)論 4 4