2.1 数据准备
(1)数据集下载与查看
cd /usr/local
ls
sudo mkdir bigdatacase
sudo chown -R hadoop:hadoop ./bigdatacase
cd bigdatacase
mkdir dataset
cd ~/下载
ls
unzip user.zip -d /usr/local/bigdatacase/dataset
cd /usr/local/bigdatacase/dataset
ls
(2)数据集预处理
sed -i ‘1d’ raw_user
head -5 raw_user.csv
gedit pre_deal.sh
bash ./pre_deal.sh raw_user.csv rawuser_table.txt
ls
Head -5 rawuser_table.txt
(3)把数据集导入HDFS中
start_all.sh
jps
hdfs dfs -mkdir -p /bigdatacase/dataset
Hdfs dfs -put /usr/local/bigdatacase/dataset/rawuser_table.txt /bigdatacase/dataset
hdfs dfs -ls /bigdatacase/dataset/
hdfs dfs -cat /bigdatacase/dataset/rawuser_table.txt | head -10
(4)在Hive上创建数据库
hive
create database bcd;
use bcd;
CREATE EXTERNAL TABLE bcd.bigdata_rawuser(id INT,uid STRING,item_id STRING,behavior_type INT,item_category STRING,visit_date DATE,province STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE
LOCATION '/bigdatacase/dataset';
select * from bigdata_rawuser limit 10;
2.2 Hive数据分析
(1)用户行为分析需求:2014-12-11~12号有多少条购买商品的记录
分析步骤
语句:select count(*) from bigdata_rawuser where visit_date >'2014-12-10' and visit_date <'2014-12-13' and behavior_type='4' limit 10;
- 结果截图:运行或存为表格后的查询显示
(2)用户行为分析需求:分析每月1-31号购买情况
- 语句:select count(distinct uid), day(visit_date) from bigdata_rawuser where behavior_type='4' group by day(visit_date);
- 结果截图:运行或存为表格后的查询显示
(3)自定义需求:2014-12-12号当天按省份统计买了超过四种商品的用户id和次数
- 语句:select uid,count(*),province from bigdata_rawuser where visit_date='2014-12-12' and behavior_type='4' group by uid,province having count(behavior_type='4')>5 limit 5;
- 结果截图:运行或存为表格后的查询显示