本次实验采用厦门大学林子雨教授团队开发的实验课程,在自己已安装的环境走了一遍,总体比较顺利,也遇到一些坑,记录一下,以免重入坑


一、大数据案例-步骤一:本地数据集上传到数据仓库Hive
1.实验数据集的下载:
百度网盘下载 http://pan.baidu.com/s/1nuOSo7B
user.zip  172M  包含了一个大规模数据集raw_user.csv(包含2000万条记录),和一个小数据集small_user.csv(只包含30万条记录)

2.下载后用到的相关命令

mkdir bigdatacase
 cd bigdatacase
 mkdir dataset
 unzip user.zip -d /root/bigdatacase/dataset
 cd /root/bigdatacase/dataset
 ls
 head -5 raw_user.csv
 head -5 small_user.csv

3.字段解析:
user_id(用户id)
item_id(商品id)
behaviour_type(包括浏览、收藏、加购物车、购买,对应取值分别是1、2、3、4)
user_geohash(用户地理位置哈希值,有些记录中没有这个字段值,所以后面我们会用脚本做数据预处理时把这个字段全部删除)
item_category(商品分类)
time(该记录产生时间)

----------------
数据集的预处理
1.删除文件第一行记录,即字段名称

cd /usr/local/bigdatacase/dataset
 sed -i '1d' raw_user.csv //1d表示删除第1行,同理,3d表示删除第3行,nd表示删除第n行
 sed -i '1d' small_user.csv
 head -5 raw_user.csv
 head -5 small_user.csv

2.对字段进行预处理
包括为每行记录增加一个id字段(让记录具有唯一性)、增加一个省份字段(用来后续进行可视化分析),并且丢弃user_geohash字段(后面分析不需要这个字段)。

pre_deal.sh
nano pre_deal.sh
 ----------------
 #!/bin/bash
 #下面设置输入文件,把用户执行pre_deal.sh命令时提供的第一个参数作为输入文件名称
 infile=$1
 #下面设置输出文件,把用户执行pre_deal.sh命令时提供的第二个参数作为输出文件名称
 outfile=$2
 #注意!!最后的$infile > $outfile必须跟在}’这两个字符的后面
 awk -F "," 'BEGIN{
         srand();
         id=0;
         Province[0]="山东";Province[1]="山西";Province[2]="河南";Province[3]="河北";Province[4]="陕西";Province[5]="内蒙古";Province[6]="上海市";
         Province[7]="北京市";Province[8]="重庆市";Province[9]="天津市";Province[10]="福建";Province[11]="广东";Province[12]="广西";Province[13]="云南"; 
         Province[14]="浙江";Province[15]="贵州";Province[16]="新疆";Province[17]="西藏";Province[18]="江西";Province[19]="湖南";Province[20]="湖北";
         Province[21]="黑龙江";Province[22]="吉林";Province[23]="辽宁"; Province[24]="江苏";Province[25]="甘肃";Province[26]="青海";Province[27]="四川";
         Province[28]="安徽"; Province[29]="宁夏";Province[30]="海南";Province[31]="香港";Province[32]="澳门";Province[33]="台湾";
     }
     {
         id=id+1;
         value=int(rand()*34);       
         print id"\t"$1"\t"$2"\t"$3"\t"$5"\t"substr($6,1,10)"\t"Province[value]
     }' $infile > $outfile

------------------

chmod +x pre_deal.sh
bash ./pre_deal.sh small_user.csv user_table.txt
[root@centos7 dataset]# head -10 user_table.txt
 1    10001082    285259775    1    4076    2014-12-08    辽宁
 2    10001082    4368907        1    5503    2014-12-12    甘肃
 3    10001082    4368907        1    5503    2014-12-12    香港
 4    10001082    53616768    1    9762    2014-12-02    河北
 5    10001082    151466952    1    5232    2014-12-12    湖南
 6    10001082    53616768    4    9762    2014-12-02    河南
 7    10001082    290088061    1    5503    2014-12-12    湖南
 8    10001082    298397524    1    10894    2014-12-12    内蒙古
 9    10001082    32104252    1    6513    2014-12-12    湖北
 10    10001082    323339743    1    10894    2014-12-12    山西

3.导入数据库

hdfs dfs -mkdir -p /bigdatacase/dataset   #创建hdfs目录
 hdfs dfs -put /root/bigdatacase/dataset/user_table.txt /bigdatacase/dataset
 hdfs dfs -cat /bigdatacase/dataset/user_table.txt | head -10cd /home/linbin/software/hive-1.1.0-cdh5.15.1
 ./bin/hive
 hive>  create database dblab;
 hive>  use dblab;
 hive>  CREATE EXTERNAL TABLE dblab.bigdata_user(id INT,uid STRING,item_id STRING,behavior_type INT,item_category STRING,visit_date DATE,province STRING) COMMENT 'Welcome to xmu dblab!' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE LOCATION '/bigdatacase/dataset';hive> select * from bigdata_user limit 5;
 OK
 1    10001082    285259775    1    4076    2014-12-08    辽宁
 2    10001082    4368907    1    5503    2014-12-12    甘肃
 3    10001082    4368907    1    5503    2014-12-12    香港
 4    10001082    53616768    1    9762    2014-12-02    河北
 5    10001082    151466952    1    5232    2014-12-12    湖南
 Time taken: 0.771 seconds, Fetched: 5 row(s)

----------------------------------------------
大数据案例-步骤二:Hive数据分析

hive>  select behavior_type from bigdata_user limit 10;
 hive> show create table bigdata_user;
 hive> desc bigdata_user;
 hive> select visit_date,item_category from bigdata_user limit 20;
 hive> select e.bh, e.it  from (select behavior_type as bh, item_category as it from bigdata_user) as e  limit 20;
 hive> select count(*) from bigdata_user;
 hive> select count(distinct uid) from bigdata_user;
 查询不重复的数据有多少条(为了排除客户刷单情况)
 hive> select count(*) from (select uid,item_id,behavior_type,item_category,visit_date,province from bigdata_user group by uid,item_id,behavior_type,item_category,visit_date,province having count(*)=1)a;
 查询2014年12月10日到2014年12月13日有多少人浏览了商品
 hive> select count(*) from bigdata_user where behavior_type='1' and visit_date<'2014-12-13' and visit_date>'2014-12-10';
 以月的第n天为统计单位,依次显示第n天网站卖出去的商品的个数
 hive> select count(distinct uid), day(visit_date) from bigdata_user where behavior_type='4' group by day(visit_date);
 取给定时间和给定地点,求当天发出到该地点的货物的数量
 hive> select count(*) from bigdata_user where province='江西' and visit_date='2014-12-12' and behavior_type='4';
 查询一件商品在某天的购买比例或浏览比例,两个查询结果数据相比较即可
 hive> select count(*) from bigdata_user where visit_date='2014-12-11'and behavior_type='4';
 hive> select count(*) from bigdata_user where visit_date ='2014-12-11';给定购买商品的数量范围,查询某一天在该网站的购买该数量商品的用户id,(购买5件以上的)
 hive>  select uid from bigdata_user where behavior_type='4' and visit_date='2014-12-12' group by uid having count(behavior_type='4')>5;
 某个地区的用户当天浏览网站的次数
 hive> create table scan(province STRING,scan INT) COMMENT 'This is the search of bigdataday' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE;//创建新的数据表进行存储
 hive> insert overwrite table scan select province,count(behavior_type) from bigdata_user where behavior_type='1' group by province;//导入数据
 hive> select * from scan;//显示结果
 上海市 8364
 云南  8454
 内蒙古 8172


...

------------------------------------
大数据案例-步骤三:Hive、MySQL、HBase数据互导
1、创建临时表user_action

hive> create table dblab.user_action(id STRING,uid STRING, item_id STRING, behavior_type STRING, item_category STRING, visit_date DATE, province STRING) COMMENT 'Welcome to XMU dblab! ' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE;
 另外一个终端查看
 # hdfs dfs -ls /user/hive/warehouse/dblab.db/
 复制数据
 hive>  INSERT OVERWRITE TABLE dblab.user_action select * from dblab.bigdata_user;
 hive>  select * from user_action limit 10;


-------sqoop1---------------------
使用Sqoop1将数据从hive导入MySQL

mysql创建数据库
 mysql> show databases; #显示所有数据库
 mysql> create database dblab; #创建dblab数据库
 mysql> use dblab; #使用数据库
 mysql> show variables like "char%";  #确认utf-8编码
 mysql> CREATE TABLE `dblab`.`user_action` (`id` varchar(50),`uid` varchar(50),`item_id` varchar(50),`behavior_type` varchar(10),`item_category` varchar(50), `visit_date` DATE,`province` varchar(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8; cd /home/linbin/software/hadoop-2.6.0-cdh5.15.1
 ./sbin/mr-jobhistory-daemon.sh start historyservercd /lib/sqoop
 ./bin/sqoop export --connect jdbc:mysql://localhost:3306/dblab --username root --password 123 --table user_action --export-dir '/user/hive/warehouse/dblab.db/user_action' --fields-terminated-by '\t'; #导入命令执行完后到mysql验证结果
 MariaDB [(none)]> use dblab
 Reading table information for completion of table and column names
 You can turn off this feature to get a quicker startup with -ADatabase changed
 MariaDB [dblab]> select * from user_action limit 5;
 +--------+-----------+-----------+---------------+---------------+------------+-----------+
 | id     | uid       | item_id   | behavior_type | item_category | visit_date | province  |
 +--------+-----------+-----------+---------------+---------------+------------+-----------+
 | 76285  | 101153614 | 146046848 | 1             | 5232          | 2014-12-15 | 广西      |
 | 1      | 10001082  | 285259775 | 1             | 4076          | 2014-12-08 | 辽宁      |
 | 151176 | 101982646 | 186364546 | 1             | 11763         | 2014-12-01 | 上海市    |
 | 225657 | 102865660 | 323237439 | 1             | 5027          | 2014-12-07 | 河南      |
 | 76286  | 101153614 | 261175516 | 1             | 5232          | 2014-12-13 | 辽宁      |
 +--------+-----------+-----------+---------------+---------------+------------+-----------+
 5 rows in set (0.06 sec)使用Sqoop将数据从MySQL导入HBase
  
 start-hbase.sh
 hbase shell
 hbase> create 'user_action', { NAME => 'f1', VERSIONS => 5}
 ./bin/sqoop  import  --connect jdbc:mysql://localhost:3306/dblab --username root --password 123 --table user_action --hbase-table user_action --column-family f1 --hbase-row-key id --hbase-create-table -m 1hbase(main):001:0> count 'user_action'
 Current count: 1000, row: 100897                                                       
 Current count: 2000, row: 101797   
 ...
 Current count: 299000, row: 99098                                                      
 Current count: 300000, row: 99999                                                      
 300000 row(s) in 17.4030 seconds=> 300000

可见已成功从mysql导入到hbase

scan 'user_action',{LIMIT=>1}  #只查询前面1行
hbase(main):002:0> scan 'user_action',{LIMIT=>1}
 ROW                    COLUMN+CELL                                                     
  1                     column=f1:behavior_type, timestamp=1583200443770, value=1       
  1                     column=f1:item_category, timestamp=1583200443770, value=4076    
  1                     column=f1:item_id, timestamp=1583200443770, value=285259775     
  1                     column=f1:province, timestamp=1583200443770, value=\xE8\xBE\xBD\
                        xE5\xAE\x81                                                     
  1                     column=f1:uid, timestamp=1583200443770, value=10001082          
  1                     column=f1:visit_date, timestamp=1583200443770, value=2014-12-08 
 1 row(s) in 0.0960 secondshbase(main):003:0>

使用HBase Java API把数据从本地导入到HBase中(略)

--------------------------------------------
大数据案例-步骤四:利用R进行数据可视化分析

# R
 > install.packages('RMySQL')
 > library(devtools)
 > devtools::install_github('ramnathv/htmlwidgets')
 > devtools::install_github('rstudio/htmltools')
 > devtools::install_github('taiyun/recharts@htmlwidgets')   #这个eMap画地图需要> library(RMySQL)
 > conn <- dbConnect(MySQL(),dbname='dblab',username='root',password='123',host="127.0.0.1",port=3306)
 > dbSendQuery(conn,'SET NAMES utf8') #设置数据库对应的编码,否则中文乱码
 > user_action <- dbGetQuery(conn,'select * from user_action')
 > summary(user_action$behavior_type)
    Length     Class      Mode 
    300000 character character 
 > summary(as.numeric(user_action$behavior_type))
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.000   1.000   1.000   1.105   1.000   4.000 > library(ggplot2)
 分析消费者对商品的行为
 > ggplot(user_action,aes(as.numeric(behavior_type)))+geom_histogram() #画图
 分析哪一类商品被购买总量前十的商品和被购买总量
 > temp <- subset(user_action,as.numeric(behavior_type)==4) # 获取子数据集
 > count <- sort(table(temp$item_category),decreasing = T) #排序
 > print(count[1:10]) # 获取第1到10个排序结果
 > result <- as.data.frame(count[1:10]) #将count矩阵结果转换成数据框
 > ggplot(result,aes(Var1,Freq,col=factor(Var1)))+geom_point()  #画图

大数据实时数据分析 课题 大数据分析实战案例_mysql

 

分析每年的哪个月份购买商品的量最多

> month <- substr(user_action$visit_date,6,7) # visit_date变量中截取月份
 > user_action <- cbind(user_action,month) # user_action增加一列月份数据
 > ggplot(user_action,aes(as.numeric(behavior_type),col=factor(month)))+geom_histogram()+facet_grid(.~month) #画图分析国内哪个省份的消费者最有购买欲望
 > library(recharts)
 > rel <- as.data.frame(table(temp$province))
 > provinces <- rel$Var1
 > x = c()
 > for(n in provinces){
 > x[length(x)+1] = nrow(subset(temp,(province==n)))
 > }
 > mapData <- data.frame(province=rel$Var1,count=x, stringsAsFactors=F)
 > print(mapData)
 > eMap(mapData, mapData$province,mapData$count)
 最后画地图的原文是eMap(mapData, namevar=~province, datavar = ~count) #画出中国地图


总出错,修改后即可