天天看點

Hive汽車銷售資料分析系統實戰開發:

Hive汽車銷售資料分析系統實戰開發:

汽車銷售(Atuo Sales)是消費者支出的重要組成成分,同時能很好的反映出消費者對經濟前景的信心。通常,汽車銷售情況我們了解一個國家經濟循環強弱情況的第一手資料,早于其他個人消費資料的公布。是以,汽車銷售為随後的零售額和個人消費支出提供了很好的預示作用,汽車消費額占零售額的25%和整個銷售總額的8%。另外,汽車銷售可作為預示經濟衰退和複蘇的早期信号。

[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-5xiJ47Wg-1607946899632)(C:\Users\Lenovo\Pictures\Saved Pictures\1.jpg)]

第一步:

建立資料庫:

create database  if not exists cars;      

使用資料庫:

use cars;      

使用rz命名上次資料檔案到虛拟機上:

[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-lBocdDcY-1607946899636)(C:\Users\Lenovo\Pictures\Saved Pictures\2.jpg)]

建立表car:
create external table car(
province string comment '省',
month int comment '月',
city string comment '市',
district string comment '區縣',
year int comment '年',
model string comment '車輛型号',
manufacturer string comment '制造商',
brand string comment '品牌',
vehicletype string comment '車輛類型',
ownership string comment '所有權',
nature string comment '使用性質',
quantity int comment '數量',
enginemodel string comment '發動機型号',
displacement int comment '排量',
power int comment '功率',
fuel string comment '燃料種類',
length1 int comment '車長',
width1 int comment '車寬',
height1 int comment '車高',
length2 int comment '廂長',
width2 int comment '廂寬',
height2 int comment '廂高',
numberofaxles int comment '軸數',
wheelbase int comment '軸距',
frontwheelbase int comment '前輪距',
tirespecification string comment '輪胎規格',
tirenumber int comment '輪胎數',
totalquality int comment '總品質',
completequality int comment '整備品質',
approvedquality int comment '核定載品質',
approvedpassenger string comment '核定載客',
tractionquality int comment '準牽引品質',
chassisenterprise string comment '底盤企業',
chassisbrand string comment '底盤品牌',
chassismodel string comment '底盤型号',
engineenterprise string comment '發動機企業',
vehiclename string comment '車輛名稱',
age int comment '年齡',
gender string comment '性别'
)
row format delimited fields terminated by ',';

加載資料:
load data local inpath '/hivedata/car.csv' into table cars;
#load data local inpath '自己存放資料檔案的地方' into table car;      

如果你的資料庫沒有改的話會出現中文亂碼問題:

[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-A78rdlZj-1607946899638)(C:\Users\Lenovo\Pictures\Saved Pictures\3.jpg)]

下述連接配接可以解決:

另外一種方法就是:

檔案如果是.csv字尾的,在window打開的話,會不是utf-8格式,你用記事本打開,

把檔案改成.txt格式就可以在window下變成utf-8格式了,可以通過rz指令上傳。

在hive打開用select*from查詢時就不會出現亂碼了。

資料分析

1.乘用車輛和商用車輛的銷售數量和比例

select '非營運',sum(if(a.nature='非營運',a.cnt,0)),'營運',sum(if(a.nature!='非營運',a.cnt,0)) 
from 
(select nature,count(*) as cnt 
from cars group by nature having nature is not null and nature!='') a;      
Total MapReduce CPU Time Spent: 6 seconds 810 msec
OK
非營運 66478 營運  3884
Time taken: 32.365 seconds, Fetched: 1 row(s)      

2.山西省2013年每個月的汽車銷售數量比例

select  month,c1.ss/c2.sum 
from 
(select month,sum(quantity) as ss 
from cars where province = '山西省' and year = '2013' group by month ) c1,
(select sum(quantity) as sum 
from cars where province = '山西省' and year = '2013') c2;      
Total MapReduce CPU Time Spent: 9 seconds 150 msec
OK
1 0.14799181376311077
2 0.05831272561894204
3 0.09306159574770473
4 0.06587362496802251
5 0.0732071288479577
6 0.05547028225462608
7 0.06323015263920867
8 0.06378442909525028
9 0.06948352804070379
10  0.1044882180722549
11  0.10053722179585571
12  0.1045592791563628
Time taken: 53.486 seconds, Fetched: 12 row(s)      

3.買車的男女比例和男女對車的品牌的選擇

select '男性',B.man/(B.man+B.woman),'女性', B.woman/(B.man+B.woman) 
from
(select '男性',sum(if(A.gender='男性',A.cnt,0)) as man,'女性',sum(if(A.gender='女性',A.cnt,0)) as woman 
from
(select gender,count(*) as cnt
from cars where gender is not null and gender != '' group by gender) A) B;      
Total MapReduce CPU Time Spent: 5 seconds 880 msec
OK
男性  0.7010659323952227  女性  0.29893406760477725
Time taken: 34.488 seconds, Fetched: 1 row(s)      
select gender,brand,count(*) as cnt
from cars
where  gender is not null and gender != ''  and age is not null
group by gender,brand
having brand is not null and brand !=' '
order by cnt desc
limit 5;      
Total MapReduce CPU Time Spent: 5 seconds 330 msec
OK
男性  五菱  28208
女性  五菱  12004
男性  長安  3679
男性  東風  3214
男性  五菱宏光  2331
Time taken: 33.615 seconds, Fetched: 5 row(s)      

4.車的所有權,車輛型号,車輛類型

select a.cnt,count(*) 
from 
(select concat(model,ownership,vehicletype) as cnt from cars) a 
group by a.cnt;      
ZK6726DX3機關大型專用校車 1
ZK6726DXA9機關大型專用校車  4
ZK6729D2機關大型普通客車  2
ZK6729DB機關大型普通客車  16
ZK6731DG1機關大型普通客車 6
ZK6731NG1機關大型普通客車 24
ZK6750D2機關大型普通客車  17
......      

5.不同車型在一個月的銷售量

select month,vehicletype,count(*) from cars group by vehicletype,month having month is not null and vehicletype is null and vehicletype != '';      

6.不同品牌車銷售情況,統計發動機和燃料種類

select brand,enginemodel,fuel,count(*) from cars group by brand,enginemodel,fuel;      

7.統計五菱某一年每月的銷售量

select brand,month,count(*) from cars group by brand,month having brand='五菱';      
Total MapReduce CPU Time Spent: 3 seconds 940 msec
OK
五菱  1 5589
五菱  2 2226
五菱  3 3557
五菱  4 2389
五菱  5 3351
五菱  6 2302
五菱  7 2893
五菱  8 2980
五菱  9 3422
五菱  10  5278
五菱  11  4809
五菱  12  4963
Time taken: 16.416 seconds, Fetched: 12 row(s)      

U Time Spent: 3 seconds 940 msec

OK

五菱 1 5589

五菱 2 2226

五菱 3 3557

五菱 4 2389

五菱 5 3351

五菱 6 2302

五菱 7 2893

五菱 8 2980

五菱 9 3422

五菱 10 5278

五菱 11 4809

五菱 12 4963

Time taken: 16.416 seconds, Fetched: 12 row(s)

我的一下查詢的截圖:

Hive汽車銷售資料分析系統實戰開發:
Hive汽車銷售資料分析系統實戰開發:

繼續閱讀