参考资料:https://dblab.xmu.edu.cn/blog/1005/
1、Hive数据导入
(1)将small_user.csv文件导入到hive中
(2)根据导入数据格式特征,创建名为user的外部表
create external table if not exists myuser
(
`user_id` int,
`item_id` int,
`behaviour_type` int,
`user_geohash` string,
`item_category` string,
`time` string
)
row format delimited fields terminated by ',';
文件导入到hive中
load data local inpath '/home/hadoop/small_user.csv' overwrite into table myuser;
数据字段解释:
user_id(用户id)
item_id(商品id)
behaviour_type(包括浏览、收藏、加购物车、购买,对应取值分别是1、2、3、4)
user_geohash(用户地理位置哈希值,有些记录中没有这个字段值,可以删除此字段)
item_category(商品分类)
time(该记录产生时间)
2、Hive数据分析
(1)查询前10条数据
select * from myuser limit 10;
select behaviour_type from myuser limit 10;(指定列)
(2)查询2014年11月18日到2014年11月20日有多少人浏览了商品
select count(*) from myuser where behaviour_type='1' and `time`<'2014-11-18' and `time`>'2014-11-20';
(3)查询2014年12月12日“95qobv6”当天发出到该地点的货物的数量
select count(*) from myuser where user_geohash='95qobv6' and `time`='2014-12-12' and behaviour_type='4';
(4)查询2014年12月12日这一天在该网站浏览商品超过5次的用户id
select user_id from myuser where behaviour_type='4' and `time`='2014-12-12' group by uesr_id having count(behaviour_type='4')>5;
(5)统计2014年11月18日一天的用户行为,即分别有多少人浏览、收藏、加购物车、购买商品。
select count(*) from myuser where behaviour_type=1 and `time`='2014-11-18 02';
select count(*) from myuser where behaviour_type=2 and `time`='2014-11-18 02';
select count(*) from myuser where behaviour_type=3 and `time`='2014-11-18 02';
select count(*) from myuser where behaviour_type=4 and `time`='2014-11-18 02';
select behaviour_type, count(behaviour_type) count_tape from myuser where `time`= '2014-11-18 02' group by behaviour_type;
(6)统计2014年12月12日商品购买率最高的5件商品
select item_id from myuser where `time`= '2014-11-18 11'and behaviour_type='4' order by behaviour_type limit 5;
- 统计2014年12月12日购物力最强的前10位客户
select user_id from myuser where `time`= '2014-12-12 01'and behaviour_type='4' order by behaviour_type limit 10;
(8)统计网站每个月卖出的商品的个数
(9)查询2014年12月12日所有地区用户行为分布情况