项目需求:统计每个区域下最受欢迎的产品TOP3 ==> TOPN
最终我们要得到哪些数据
一:离线典型的处理框架
人 ——京东页面 ——webserver(集群,这里记录你的 操作所以行为日志 )—— 通过flume采集——hadoop的hdfs 上面存储 ——数据分析 mp/hive/spark(这里的数据存储还是在hdfs上面)——你要做前端的展示 就要(通过 用Sqoop )数据存储在ROBMS /NoSQL上面 ——UI展示
二:前期准备工作
在之前的这篇博客中:https://blog.csdn.net/qq_43688472/article/details/94454720
我们已经介绍了我的用户行为数据都已经被记录下来了
1第一步清洗数据
我们要对用户行为数据 log.gif 进行清洗 筛选出我们需要的数据(因为数据量很大,我们只要根据我们的需求,拿出相应的数据就可以了,没有必要大规模处理,清洗过后的数据我们看着简单明了 更方便处理)清洗数据永远都是第一步
2第二步将数据形成表格
这里我把数据表格 分别放在mysql和hive中
[root@hadoop001 ~]# su - mysqladmin
Last login: Fri Jun 28 10:01:25 CST 2019 on pts/3
[mysqladmin@hadoop001 ~]$ mysql -uroot -p
mysql> create database ruozedata5;
Query OK, 1 row affected (0.00 sec)
mysql> use ruozedata5;
Database changed
mysql> show tables;
Empty set (0.00 sec)
创建产品表
mysql>CREATE TABLE `product_info` (
`product_id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`extend_info` varchar(1000) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这里要导入相应的数据
这里在hive里面有点击次数的数据
create table user_click(
user_id int,
session_id string,
action_time string,
city_id int,
product_id int
) partitioned by(date string)
row format delimited fields terminated by ',';
3.把相关的表格都放到一起
1)Hive中要创建city_info, product_info
create table city_info(
city_id int,
city_name string,
area string
)
row format delimited fields terminated by '\t';
create table product_info(
product_id int,
product_name string,
extend_info string
)
row format delimited fields terminated by '\t';
因为目前的数据,有的在hive有的在mysql中,首先把mysql中的数据导入到hive中 来
在这之前先把表格创建出来
2)MySQL==>Hive
sqoop import \
--connect jdbc:mysql://localhost:3306/ruozedata5 \
--username root \
--password root \
--table product_info \
--mapreduce-job-name FromMySQLToHDFS \
-m 1 \
--delete-target-dir \
--fields-terminated-by '\t' \
--null-non-string '0' \
--null-string '' \
--hive-import \
--hive-table product_info \
--hive-overwrite
在生产上面用shell脚本来封装的
三:操作(重点)
第一步:user_click join city_info
create table tmp_product_click_basic_info
as
select
u.product_id, u.city_id, c.city_name, c.area
from
(select product_id, city_id from user_click
where date='2016-05-05'
and product_id <> 0) u
join
(select city_id,city_name, area from city_info) c
on u.city_id = c.city_id;
各个区域各商品的点击次数
drop table tmp_area_product_click_count;
create table tmp_area_product_click_count as
select product_id,area,count(1) as click_count from tmp_product_click_basic_info group by product_id,area;
1 NC 8
1 NW 6
1 SC 25
create table tmp_area_product_click_count_full_info as
select a.product_id,b.product_name,a.click_count, a.area from tmp_area_product_click_count a join product_info b on a.product_id=b.product_id;
求每个区域下最受欢迎的top3
create table area_product_click_count_top3
as
select *,
case when click_count >=10 and click_count<20 then 'lower'
when click_count>=20 and click_count<30 then 'middle'
else 'highest'
end click_count_grade,
'2016-05-05' day
from (
select
product_id,product_name,click_count,area,
row_number() over(partition by area order by click_count desc) rank
from tmp_area_product_click_count_full_info
) t where t.rank <=3 ;
注意:要详细理解sql语句的意思,不行可以慢慢拆分来处理
最后在合在一起
四:将结果写到mysql中
这里也是用到Sqoop导到进去就可以了
五:UI展示
下面的UI展示,就交给前端人员吧
我的大数据开发就到这里为止了
六:优化
1.把mysql语句封装成shell脚本这样操作就很方便的
2.使用这个 hive -e
就不需要进入hive 在外面可以执行hive的相关语句
$hive -e "show databaases"
3.其中在shell语句中
get_json_object 不要使用这个来解析
因为这里是测试,可以数据量少可以使用
但是在大数据的环境下 数据量庞大,这里就不适用了
那怎么办呢
通过 自定义解析JSON的UDF函数 <== 使用Java开发Hive的UDF函数来搞定
这个在后面会介绍的