Hive數(shù)據(jù)分析實(shí)驗(yàn)報告
實(shí)驗(yàn)要求
1 完成本地數(shù)據(jù)user_log文件上傳至HDFS中
2 完成HDFS文件上傳至Hive中
用戶行為日志user_log.csv,日志中的字段定義如下:
- user_id | 買家id
- item_id | 商品id
- cat_id | 商品類別id
- merchant_id | 賣家id
- brand_id | 品牌id
- month | 交易時間:月
- day | 交易事件:日
- action | 行為,取值范圍{0,1,2,3},0表示點(diǎn)擊,1表示加入購物車,2表示購買,3表示關(guān)注商品
- age_range | 買家年齡分段:1表示年齡<18,2表示年齡在[18,24],3表示年齡在[25,29],4表示年齡在[30,34],5表示年齡在[35,39],6表示年齡在[40,49],7和8表示年齡>=50,0和NULL則表示未知
- gender | 性別:0表示女性,1表示男性,2和NULL表示未知
- province| 收貨地址省份
3 Hive操作
(1)查看user_log表數(shù)據(jù)結(jié)構(gòu)
(2)查看user_log表簡單數(shù)據(jù)結(jié)構(gòu)
(3)查看日志前10個交易日志的商品品牌
(4)查詢前20個交易日志中購買商品時的時間和商品的種類
(5)用聚合函數(shù)count()計算出表內(nèi)有多少條行數(shù)據(jù)
(6)在函數(shù)內(nèi)部加上distinct,查出user_id不重復(fù)的數(shù)據(jù)有多少條
(7)排除顧客刷單(查詢不重復(fù)的數(shù)據(jù))
(8)查詢雙11當(dāng)天有多少人購買了商品
(9)品牌2661,當(dāng)天購買此品牌商品的數(shù)量
(10)查詢多少用戶當(dāng)天點(diǎn)擊了2661品牌的該店
(11)查詢雙十一當(dāng)天男女購買商品比例
(12)查詢某一天在該網(wǎng)站購買商品超過5次的用戶id
(13)創(chuàng)建姓名縮寫表 其中字段大于4條,并使查詢插入,最后顯示姓名縮寫表格數(shù)據(jù)
IP地址規(guī)劃表
實(shí)驗(yàn)步驟
1 數(shù)據(jù)集預(yù)處理
- 安裝unzip
yum install unzip
- 創(chuàng)建數(shù)據(jù)存放文件夾
mkdir /usr/local/dbtaobao/dataset
- 解壓數(shù)據(jù)集zip包
cp -r /mnt/hgfs/data_format.zip /usr/local/dbtaobao/dataset/
cd /usr/local/dbtaobao/dataset/
unzip data_format.zip
- 查看user_log.csv前5行數(shù)據(jù)
head -5 user_log.csv
- 刪除第一行
sed -i '1d' user_log.csv
-
提取10000條user_log中日期為11月11日的數(shù)據(jù),并存放于small_user_log中
-
- 創(chuàng)建腳本predeal.sh
infile=$1
outfile=$2
awk -F "," 'BEGIN{
id=0;
}
{
if($6=11 && $7=11){
id=id+1;
print $1","$2","$3","$4","$5","$6","$7","$8","$9","$10","$11","$12
if(id==10000){
exit
}
}
}' $infile > $outfile
-
- 為predeal.h提權(quán)
chmod +x ./predeal.sh
-
- 運(yùn)行腳本predeal.sh,查看輸出
./predeal ./user_log.csv ./small_user_log.csv
2 數(shù)據(jù)集上傳HDFS
- 在hdfs中創(chuàng)建存放user_log的文件夾
start-all.sh
hdfs dfs -mkdir -p /dbtaobao/dataset/user_log
- 向hdfs推送small_user_log.csv
hdfs dfs -put /usr/local/dbtaobao/dataset/small_user_log.csv /dbtaobao/dataset/user_log
- 查看上傳成功的數(shù)據(jù)文件前10行
hdfs dfs -cat /dbtaobao/dataset/user_log/small_user_log.csv | head -10
3 從HDFS中導(dǎo)出數(shù)據(jù)集至HIVE數(shù)據(jù)庫
- 創(chuàng)建HIVE數(shù)據(jù)庫dbtaobao
create database dbtaobao;
- 創(chuàng)建user_log表
create external table dbtaobao.user_log(user_id int,item_id int,cat_id int,merchant_id int,brand_id int,month string,day string,action int,age_range int,gender int,province string) comment 'Welcome to Alex dblab, now create dbtaobao.user_log!' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile location '/dbtaobao/dataset/user_log';
- 查看表user_log前十行數(shù)據(jù)
use dbtaobao;
select * from user_log limit 10;
4 Hive操作
(1)查看user_log表數(shù)據(jù)結(jié)構(gòu)
show create table user_log;
(2)查看user_log表簡單數(shù)據(jù)結(jié)構(gòu)
desc user_log;
(3)查看日志前10個交易日志的商品品牌
select brand_id from user_log limit 10;
(4)查詢前20個交易日志中購買商品時的時間和商品的種類
select month,day,brand_id from user_log limit 20;
(5)用聚合函數(shù)count()計算出表內(nèi)有多少條行數(shù)據(jù)
select count(*) from user_log;
Result : 10000
(6)在函數(shù)內(nèi)部加上distinct,查出user_id不重復(fù)的數(shù)據(jù)有多少條
select count(distinct user_id) from user_log;
Result : 358
(7)排除顧客刷單(查詢不重復(fù)的數(shù)據(jù))
select count(distinct user_id,item_id,cat_id,merchant_id,brand_id,month,day,action,age_range,gender,province) from user_log;
Result : 9944
(8)查詢雙11當(dāng)天有多少人購買了商品
select count(distinct user_id) from user_log where action='2';
Result : 358
(9)品牌2661,當(dāng)天購買此品牌商品的數(shù)量
select count(*) from user_log where brand_id='2661' and action='2';
Result : 3
(10)查詢多少用戶當(dāng)天點(diǎn)擊了2661品牌的該店
select count(distinct user_id) from user_log where brand_id='2661' and action='0';
Result : 1
(11)查詢雙十一當(dāng)天男女購買商品比例
select count(distinct user_id) from user_log where gender='0' and action='2';
select count(distinct user_id) from user_log where gender='1' and action='2';
Result : 238 (女)
Result : 214 (男)
男 女 比 例 = 214 / 238 = 89.916 % 男女比例 = 214 / 238 = 89.916\% 男女比例=214/238=89.916%
(12)查詢某一天在該網(wǎng)站購買商品超過5次的用戶id
select user_id from user_log where action='2' group by user_id having count(action='2')>5;
Result :
user_id
1321
6058
16464
18378
23786
26516
32569
35260
41494
47958
55440
61703
69247
70816
71744
84400
106446
106629
153790
161778
171909
173427
179194
186568
188977
196638
203651
211273
212058
212504
217844
219316
234456
242845
249869
251260
256190
261596
270040
272775
274559
278823
278884
283204
284990
289429
310348
310632
320313
328230
330576
332670
333389
345251
356220
356408
366342
370679
378206
379005
389295
396129
407719
409280
422917
(13)創(chuàng)建姓名縮寫表,其中字段大于4條,并使查詢插入,最后顯示姓名縮寫表格數(shù)據(jù)
- 創(chuàng)建表gr
create external table dbtaobao.GR(user_id int,item_id int,age_range int,gender int,province string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile;
文章來源:http://www.zghlxwxcb.cn/news/detail-458016.html
- 從表user_log中導(dǎo)入user_id,item_id,age_range,gender,province數(shù)據(jù)到表gr
insert into table gr select user_id,item_id,age_range,gender,province from user_log;
文章來源地址http://www.zghlxwxcb.cn/news/detail-458016.html
- 查詢表gr的前十條數(shù)據(jù)
select * from gr limit 10;
到了這里,關(guān)于Hive數(shù)據(jù)分析實(shí)驗(yàn)報告的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!