用户双11购物行为分析

用户双11购物行为系统

课题名称:用户双11购物行为系统

2018年6月

(一)课题概述

(一)课题概述
搭建hbase集群。
根据需求对用户进行查询分析,按照一定规则进行过滤。
做出可视化界面。

(二)非关系型数据库集群的搭建–hbase的搭建

(二)非关系型数据库集群的搭建–hbase的搭建

(1) 架构设计

(1) 架构设计
在这里插入图片描述
在这里插入图片描述

服务器的IP:192.168.56.30
端口:3306
元数据数据存位置:/user/hive/warehouse

此部分主要描述系统的架构规划,比如服务器的IP,功能,端口和数据存位置。)

(2)服务器的搭建

1、修改hbase-env.sh

1、修改、usr/local/hbase/conf/hbase-env.sh,修改内容如下:

export JAVA_HOME=/usr/local/jdk1.7.0_45

修改hbase-env.sh的最后一行

export HBASE_MANAGES_ZK=false

在这里插入图片描述

2、修改hbase-site.xml

2、修改$HBASE_HOME/conf/hbase-site.xml,修改内容如下:

<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop0:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop0,hadoop1,hadoop2</value>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>

在这里插入图片描述

3、修改regionservers文件

3、修改regionservers文件(存放的region server的hostname),内容修改为hadoop1、hadoop2在这里插入图片描述

4、修改各个节点 hadoop.env.sh

4、在每一个节点上修改 /usr/local/hadoop/etc/hadoop/hadoop.env.sh
加入
export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/usr/local/hbase/lib/*
在这里插入图片描述

5、修改各个节点上的/etc/profile

5、修改各个节点上的/etc/profile

export SQOOP_HOME=/usr/local/sqoop
export HADOOP_HOME=/usr/local/hadoop
export JAVA_HOME=/usr/local/jdk1.7.0_45
export HBASE_HOME=/usr/local/hbase
export HIVE_HOME=/usr/local/hive
export ZOOKEEPER_HOME=/usr/local/zk
export PATH=.:$ZOOKEEPER_HOME/bin:$SQOOP_HOME/bin:$HIVE_HOME/bin:$HBASE_HOME/bin:$HADOOP_HOME/sbin:$HADOOP_HOME/bin:$JAVA_HOME/bin:$PATH
scp  /etc/profile   hadoop1:/etc/profile
scp  /etc/profile   hadoop2:/etc/profile

6、把主节点上的hbase复制到从节点上


6、把主节点上的hbase复制到从节点上

scp  -r  /usr/local/hbase  hadoop1:/uisr/local/
scp  -r  /usr/local/hbase  hadoop2:/uisr/local/

7、启动hbase

7、启动hbase(hbase需在zookeeper已启动的基础上启动)
执行命令start-hbase.sh
进入hbase的shell命令: hbase shell
主节点:在这里插入图片描述

从节点:在这里插入图片描述

(3)数据库及数据表的创建

(3) 数据库及数据表的创建

create 'user_log', { NAME => 'f1', VERSIONS => 5}

(三)把数据从hive导到mysql中

(三)把数据从hive导到mysql中

(1)本地数据处理

(1)本地数据处理
cd /usr/local
mkdir dbtaobao //下面创建一个dataset目录,用于保存数据集
mkdir dataset //下面就可以解压缩data_format.zip文件
unzip data_format.zip -d /usr/local/dbtaobao/dataset
cd /usr/local/dbtaobao/dataset
看到在dataset目录下有三个文件:test.csv、train.csv、user_log.csv
head -5 user_log.csv在这里插入图片描述

(2)数据集的预处理

(2)数据集的预处理
cd /usr/local/dbtaobao/dataset
//下面删除user_log.csv中的第1行

sed -i '1d' user_log.csv //1d表示删除第1行,同理,3d表示删除第3行,nd表示删除第n行
//下面再用head命令去查看文件的前5行记录,就看不到字段名称这一行了
head -5 user_log.csv

在这里插入图片描述

(3)启动HDFS

(3)启动HDFS

Start-all.sh 
hdfs dfs -mkdir -p dbtaobao/dataset/user_log 
hdfs dfs -put /usr/local/dbtaobao/dataset/user_log.csv 

/dbtaobao/dataset/user_log
//把本地数据上传到hdfs上

hdfs dfs -cat /dbtaobao/dataset/user_log/small_user_log.csv | head -10//查看前10条

(4)创建外部表:

(4)创建外部表:
启动mysql:需要借助于MySQL保存Hive的元数据,所以,请首先启动MySQL数据库

mysql -u root -p

启动hive

 create database fhl;
use  fhl;

创建外部表:

 Hive>CREATE EXTERNAL TABLE fhl.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 xmu dblab,Now create dbtaobao.user_log!' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION '/dbaobao/dataset/user_log';
Hive>select * from user_log limit 10;

在这里插入图片描述
在这里插入图片描述

(四)hive数据分析

(四)hive数据分析

(1)查看表结构

(1)查看表结构
hive> show create table user_log; – 查看user_log表的各种属性;在这里插入图片描述

hive> desc user_log;//查看表结构

在这里插入图片描述

(2)简单查询

(2)简单查询

hive> select brand_id from user_log limit 10; -- 查看日志前10个交易日志的商品品牌

在这里插入图片描述

(3)查询条数统计分析

(3)查询条数统计分析

hive> select month,day,cat_id from user_log limit 20;

在这里插入图片描述

 Hive>select ul.at, ul.ci  from (select action as at, cat_id as ci from user_log) as ul limit 20;

在这里插入图片描述

hive> select count(*) from user_log; -- 用聚合函数count()计算出表内有多少条行数据

在这里插入图片描述

hive> select count(distinct user_id) from user_log; -- 在函数内部加上distinct,查出user_id不重复的数据有多少条

在这里插入图片描述

查询不重复的数据有多少条(为了排除客户刷单情况) **

hive> select count(*) from (select user_id,item_id,cat_id,merchant_id,brand_id,month,day,action from user_log group by user_id,item_id,cat_id,merchant_id,brand_id,month,day,action having count(*)=1)a;

Hive

(4)关键字条件查询分析

(4)关键字条件查询分析
1.以关键字的存在区间为条件的查询
使用where可以缩小查询分析的范围和精确度,下面用实例来测试一下。
(1)查询双11那天有多少人购买了商品

hive> select count(distinct user_id) from user_log where action='2';

2.关键字赋予给定值为条件,对其他数据进行分析

		取给定时间和给定品牌,求当天购买的此品牌商品的数量
3.hive> select count(*) from user_log where action='2' and brand_id=2661;

(5)根据用户行为分析

(5)根据用户行为分析

1.查询一件商品在某天的购买比例或浏览比例

hive> select count(distinct user_id) from user_log where action='2'; -- 查询有多少用户在双11购买了商品
hive> select count(distinct user_id) from user_log; -- 查询有多少用户在双11点击了该店

根据上面语句得到购买数量和点击数量,两个数相除即可得出当天该商品的购买率。
2.查询双11那天,男女买家购买商品的比例

hive> select count(*) from user_log where gender=0; --查询双11那天女性购买商品的数量
hive> select count(*) from user_log where gender=1; --查询双11那天男性购买商品的数量

上面两条语句的结果相除,就得到了要要求的比例。
3.给定购买商品的数量范围,查询某一天在该网站的购买该数量商品的用户id

hive> select user_id from user_log where action='2' group by user_id having count(action='2')>5;查询某一天在该网站购买商品超过5次的用户id

在这里插入图片描述

(6)不同的品牌的浏览次数

(6)不同的品牌的浏览次数

hive> create table scan(brand_id INT,scan INT) COMMENT 'This is the search of bigdatataobao' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE; -- 创建新的数据表进行存储
hive> insert overwrite table scan select brand_id,count(action) from user_log where action='2' group by brand_id; --导入数据

在这里插入图片描述
hive> select * from scan; -- 显示结果在这里插入图片描述

(五)使用sqoop将数据从hive导入MySQL

(五)使用sqoop将数据从hive导入MySQL

(1)预操作:

(1)预操作:
cd /usr/local/hive
./bin/hive #启动Hive
Shell 命令
通过上述过程,我们就完成了MySQL、Hadoop和Hive三者的启动。
启动成功以后,就进入了“hive>”命令提示符状态,可以输入类似SQL语句的HiveQL语句。

然后,在“hive>”命令提示符状态下执行下面命令:

1、创建临时表inner_user_log和inner_user_info

hive> create table inner_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 XMU dblab! Now create inner table inner_user_log ' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

hive
这个命令执行完以后,Hive会自动在HDFS文件系统中创建对应的数据文件“/user/hive/warehouse/dbtaobao.db/inner_user_log”。

2、将user_log表中的数据插入到inner_user_log,
在[大数据案例-步骤一:本地数据集上传到数据仓库Hive(待续)]中,我们已经在Hive中的dbtaobao数据库中创建了一个外部表user_log。下面把dbtaobao.user_log数据插入到dbtaobao.inner_user_log表中,命令如下:

hive> INSERT OVERWRITE TABLE dbtaobao.inner_user_log select * from dbtaobao.user_log;

(2)导入

(2)导入

hive> select * from inner_user_log limit 10;
启动mysql
mysql -u root -p
mysql> show databases; #显示所有数据库
mysql> create database fhl; #创建fhl数据库
mysql> use fhl; #使用数据库

在这里插入图片描述
mysql> show variables like “char%”;//查看数据库的编码:

在mysql中创建表:
mysql> CREATE TABLE user_log (user_id varchar(20),item_id varchar(20),cat_id varchar(20), merchant_id  varchar(20),brand_id  varchar(20), month varchar(6),day  varchar(6),action varchar(6),age_range  varchar(6),gender  varchar(6),province  varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

sqoop导入:
sqoop export --connect jdbc:mysql://192.168.56.30:3306/fhl --username root --password 123456 --table user_log --export-dir '/user/hive/warehouse/fhl.db/inner_user_log' --fields-terminated-by ',';

字段解释:
./bin/sqoop export ##表示数据从 hive 复制到 mysql 中
–connect jdbc:mysql://localhost:3306/dbtaobao
–username root #mysql登陆用户名
–password root #登录密码
–table user_log #mysql 中的表,即将被导入的表名称
–export-dir ‘/user/hive/warehouse/dbtaobao.db/user_log ‘ #hive 中被导出的文件
–fields-terminated-by ‘,’ #Hive 中被导出的文件字段的分隔符

Mysql>select * from user_log limit 10;//查看前10条记录
Mysql>select count(*) from user_log;//查看记录条数在这里插入图片描述

(六)把数据从mysql导到hbase中

(六)把数据从mysql导到hbase中

(1)导数据

(1)导数据

habse>create 'user_log', { NAME => 'f1', VERSIONS => 5}
把数据从mysql导到hbase中
#sqoop  import  --connect jdbc:mysql://192.168.56.30:3306/fhl --username root 	--password 123456 --table user_log  --hbase-table user_log --column-family f1 	--hbase-row-key user_id --hbase-create-table -m 1

在这里插入图片描述

字段解释:
./bin/sqoop import --connect jdbc:mysql://1912.168.56.30:3306/dblab
–username root
–password 123456
–table user_log
–hbase-table user_log #HBase中表名称
–column-family f1 #列簇名称
–hbase-row-key user_id #HBase 行键
–hbase-create-table #是否在不存在情况下创建表
-m 1 #启动 Map 数量

(2)基本查询

(2)基本查询
hbase>scan ‘user_log’,{LIMIT=>10} #只查询前面10行
在这里插入图片描述

Hbase>count ‘user_log’在这里插入图片描述

(七)Web应用编写

(七)Web应用编写

web页面

 Web页面

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码:

代码:在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(八)个人总结

(八)个人总结
通过完成为这次的大作业,我收获颇多,这一路上困难重重。举步维艰,有时候一个报错,能困我两三天。安装的工具的时候漏洞百出,可能多一个符号,可能少一个符号。再或者忘了配置某个地方,都会给使用带来很大的困难。在安装mysql时,mysql会莫名其妙的起不来,经过多次查询资料才解决这个问题。但这也不是最大的挑战。最大的挑战是hbase,在安装的时候,不知为何hbase起不来,我只好删掉高版本的,装了一个低版本的,然后起来了,自以为这关能过了,可谁知更大的挑战正在等着我,在我准备用sqoop把数据从mysql导导hbase中时,hbase连不上zookeeper了!我就只好一直百度,然后发现hbase自带的zookeeper和我自己安装的zookeepr同时起来了。2181端口被其中一个占用了。我回头检查了一下,发现hbase中自带zookpeer忘了关闭了,那我就把hbase中自带zookeepr关闭呗,可关闭之后以还是连不上我自己安装的zookeepr。说是数据太大,mapredce等不了这么长时间,然后死掉了!我查询很多资料,有的说让格式化namenode,我就照做了,可是格式化完,起不了datanode,然后又去查询为什么起不了datanode,最后修正过来了。之后又遇到了很多很的报错,我就忍着不让自己生气,今天终于完成了!我很激动!
虽然,这次作业困难重重,但是我真的收获了许多许多,我学会了怎么查报错日志,学会了怎么看报错。这期间我也寻求了很多人的帮助,非常感谢他们,也非常感谢自己没有被困难吓退。但最应该感谢的还是能有这次锻炼的机会!

猜你喜欢

转载自blog.csdn.net/qq_41919792/article/details/106902550