打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Hive进行ETL

一、数据清洗介绍 
二、Hive数据清洗 
1.本文使用的是一数据集为user.zip,包含了一个大规模数据集raw_user.csv(包含2000万条记录),和一个小数据集small_user.csv(只包含30万条记录)。小数据集small_user.csv是从大规模数据集raw_user.csv中抽取的一小部分数据。之所以抽取出一少部分记录单独构成一个小数据集,是因为,在第一遍跑通整个实验流程时,会遇到各种错误,各种问题,先用小数据集测试,可以大量节约程序运行时间。等到第一次完整实验流程都顺利跑通以后,就可以最后用大规模数据集进行最后的测试。 
解压后查看前五行

[hadoop@master dataset]$ unzip user.zip 
Archive:  user.zip  inflating: raw_user.csv            
  inflating: small_user.csv          
[hadoop@master dataset]$ ls
raw_user.csv  small_user.csv  user.zip
[hadoop@master dataset]$ head -5 raw_user.csv 
user_id,item_id,behavior_type,user_geohash,item_category,time10001082,285259775,1,97lk14c,4076,2014-12-08 1810001082,4368907,1,,5503,2014-12-12 1210001082,4368907,1,,5503,2014-12-12 1210001082,53616768,1,,9762,2014-12-02 1512345678910111213

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

数据集的预处理 
1.删除文件第一行记录,即字段名称 
raw_user和small_user中的第一行都是字段名称,我们在文件中的数据导入到数据仓库Hive中时,不需要第一行字段名称,因此,这里在做数据预处理时,删除第一行

[hadoop@master dataset]$ sed -i '1d' raw_user.csv 
[hadoop@master dataset]$ sed -i '1d' small_user.csv 
[hadoop@master dataset]$ head -5 raw_user.csv 
10001082,285259775,1,97lk14c,4076,2014-12-08 1810001082,4368907,1,,5503,2014-12-12 1210001082,4368907,1,,5503,2014-12-12 1210001082,53616768,1,,9762,2014-12-02 1510001082,151466952,1,,5232,2014-12-12 11[hadoop@master dataset]$ 
12345678910

接下来的操作中,我们都是用small_user.csv这个小数据集进行操作,这样可以节省时间。等所有流程都跑通以后,你就可以使用大数据集raw_user.csv去测试一遍了。

2.对字段进行预处理 
下面对数据集进行一些预处理,包括为每行记录增加一个id字段(让记录具有唯一性)、增加一个省份字段(用来后续进行可视化分析),并且丢弃user_geohash字段(后面分析不需要这个字段)。 
下面我们要建一个脚本文件pre_deal.sh,请把这个脚本文件放在dataset目录下,和数据集small_user.csv放在同一个目录下:

#!/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

上面的代码的基本形式是:

awk -F "," '处理逻辑' $infile > $outfile
使用awk可以逐行读取输入文件,并对逐行进行相应操作。其中,-F参数用于指出每行记录的不同字段之间用什么字符进行分割,这里是用逗号进行分割。处理逻辑代码需要用两个英文单引号引起来。 $infile是输入文件的名称,我们这里会输入raw_user.csv,$outfile表示处理结束后输出的文件名称,我们后面会使用user_table.txt作为输出文件名称。

在上面的pre_deal.sh代码的处理逻辑部分,srand()用于生成随机数的种子,id是我们为数据集新增的一个字段,它是一个自增类型,每条记录增加1,这样可以保证每条记录具有唯一性。我们会为数据集新增一个省份字段,用来进行后面的数据可视化分析,为了给每条记录增加一个省份字段的值,这里,我们首先用Province[]数组用来保存全国各个省份信息,然后,在遍历数据集raw_user.csv的时候,每当遍历到其中一条记录,使用value=int(rand()*34)语句随机生成一个0-33的整数,作为Province省份值,然后从Province[]数组当中获取省份名称,增加到该条记录中。

substr($6,1,10)这个语句是为了截取时间字段time的年月日,方便后续存储为date格式。awk每次遍历到一条记录时,每条记录包含了6个字段,其中,第6个字段是时间字段,substr($6,1,10)语句就表示获取第6个字段的值,截取前10个字符,第6个字段是类似”2014-12-08 18″这样的字符串(也就是表示2014年12月8日18时),substr($6,1,10)截取后,就丢弃了小时,只保留了年月日。
另外,在print id”\t”$1″\t”$2″\t”$3″\t”$5″\t”substr($6,1,10)”\t”Province[value]这行语句中,我们丢弃了每行记录的第4个字段,所以,没有出现$4。我们生成后的文件是“\t”进行分割,这样,后续我们去查看数据的时候,效果让人看上去更舒服,每个字段在排版的时候会对齐显示,如果用逗号分隔,显示效果就比较乱。

最后,保存pre_deal.sh代码文件,退出vim编辑器。1234567891011121314151617181920212223242526272829303132

运行shell文件

[hadoop@master dataset]$ chmod +x ./predeal.sh
[hadoop@master dataset]$ ./predeal.sh ./user_log.csv ./small_user_log.csv12

简单分析: 
1、这里我们要在数据库dblab中创建一个外部表bigdata_user,它包含字段(id, uid, item_id, behavior_type, item_category, date, province),请在hive命令提示符下输入如下命令:

[hadoop@master /]$ hive
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/app/hive/apache-hive-2.1.1-bin/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/app/hadoop/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Logging initialized using configuration in file:/app/hive/apache-hive-2.1.1-bin/conf/hive-log4j2.properties Async: true
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
hive> create database dblab;
OK
Time taken: 1.713 seconds
hive> use dblab;
OK
Time taken: 0.039 seconds
hive> show databases;
OK
dblabdefaultTime taken: 0.631 seconds, Fetched: 2 row(s)
hive> use dblab;
OK
Time taken: 0.043 seconds
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'
    > ;
OK
Time taken: 0.757 seconds
hive> select * from bigint
bigint    bigint(   
hive> select * from bigdata_user limit 10;
OK1   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  江西
Time taken: 2.656 seconds, Fetched: 10 row(s)
hive> select behavior_type from bigdata_user limit 10;
OK1111141111Time taken: 0.216 seconds, Fetched: 10 row(s)
hive> show create table bigint
bigint    bigint(   
hive> show create table bigdata_user;
OK
CREATE EXTERNAL TABLE `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 SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 
  'field.delim'='\t', 
  'serialization.format'='\t') 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'LOCATION  'hdfs://master:8020/bigdatacase/dataset'TBLPROPERTIES (  'numFiles'='1', 
  'totalSize'='15590804', 
  'transient_lastDdlTime'='1528339771')
Time taken: 0.316 seconds, Fetched: 24 row(s)
hive> desc bigdata_user;
OK
id                      int                                         uid                     string                                      item_id                 string                                      behavior_type           int                                         item_category           string                                      visit_date              date                                        province                string                                      Time taken: 0.124 seconds, Fetched: 7 row(s)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495

2、查看日志前10个交易日志的商品品牌

hive> select behavior_type from bigdata_user limit 10;
OK111114111112345678910111213

3、如果要查出每位用户购买商品时的多种信息,输出语句格式为 select 列1,列2,….,列n from 表名;比如我们现在查询前20位用户购买商品时的时间和商品的种类

hive> select visit_date,item_category from bigdata_user limit 20;
OK2014-12-08  40762014-12-12  55032014-12-12  55032014-12-02  97622014-12-12  52322014-12-02  97622014-12-12  55032014-12-12  108942014-12-12  65132014-12-12  108942014-12-12  28252014-11-28  28252014-12-15  32002014-12-03  105762014-11-20  105762014-12-13  105762014-12-08  105762014-12-14  70792014-12-02  66692014-12-12  5232Time taken: 0.713 seconds, Fetched: 20 row(s)123456789101112131415161718192021222324

4、有时我们在表中查询可以利用嵌套语句,如果列名太复杂可以设置该列的别名,以简化我们操作的难度,以下我们可以举个例子:

hive> select e.bh,e.it from (select behavior_type as bh,item_category as it from bigdata_user) as e limit 20;
OK1   40761   55031   55031   97621   52324   97621   55031   108941   65131   108941   28251   28251   32001   105761   105761   105761   105761   70791   66691   5232Time taken: 0.547 seconds, Fetched: 20 row(s)123456789101112131415161718192021222324

5、用聚合函数count()计算出表内有多少条行数据

hive> select count(*) from bigdata_user;//用聚合函数count()计算出表内有多少条行数据 Total MapReduce CPU Time Spent: 0 msec
OK300000Time taken: 6.418 seconds, Fetched: 1 row(s)12345

6.在函数内部加上distinct,查出uid不重复的数据有多少条

hive> select count(distinct uid) from bigdata_user;//在函数内部加上distinct,查出uid不重复的数据有多少条Total MapReduce CPU Time Spent: 0 msec
OK270Time taken: 4.689 seconds, Fetched: 1 row(s)12345

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

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;
Total MapReduce CPU Time Spent: 0 msec
OK284183Time taken: 16.05 seconds, Fetched: 1 row(s)12345

8.以关键字的存在区间为条件的查询 
使用where可以缩小查询分析的范围和精确度,下面用实例来测试一下。 
(1)查询2014年12月10日到2014年12月13日有多少人浏览了商品

select count(*) from bigdata_user where behavior_type='1' and visit_date <'2014-12-13' and visit_date>'2014-12-10';Total MapReduce CPU Time Spent: 0 msec
OK
26329
Time taken: 3.058 seconds, Fetched: 1 row(s)123456

(2)以月的第n天为统计单位,依次显示第n天网站卖出去的商品的个数

select count(distinct uid),day(visit_date) from bigdata_user where behavior_type='4' group by day(visit_date);Total MapReduce CPU Time Spent: 0 msec
OK
37  1
48  2
42  3
38  4
42  5
33  6
42  7
36  8
34  9
40  10
43  11
98  12
39  13
43  14
42  15
44  16
42  17
66  18
38  19
50  20
33  21
34  22
32  23
47  24
34  25
31  26
30  27
34  28
39  29
38  30
Time taken: 2.378 seconds, Fetched: 30 row(s)12345678910111213141516171819202122232425262728293031323334

(3)关键字赋予给定值为条件,对其他数据进行分析 
取给定时间和给定地点,求当天发出到该地点的货物的数量

select count(*) from bigdata_user where visit_date='2014-12-12' and behavior_type='4' and province='江西';Total MapReduce CPU Time Spent: 0 msec
OK
7
Time taken: 3.421 seconds, Fetched: 1 row(s)12345

9.根据用户行为分析 
(1)查询一件商品在某天的购买比例或浏览比例

select count(*) from bigdata_user where visit_date='2014-12-11' and behavior_type='4';//查询有多少用户在2014-12-11购买了商品12
select count(*) from bigdata_user where visit_date='2014-12-11'//查询有多少用户在2014-12-11点击了该店12

两个相除得到某件商品购买率 
(2)查询某个用户在某一天点击网站占该天所有点击行为的比例(点击行为包括浏览,加入购物车,收藏,购买)

select count(*) from bigdata_user where uid=10001082 and visit_date='2014-12-12';//查询用户10001082在2014-12-12点击网站的次数12
select count(*) from bigdata_user where visit_date='2014-12-12';1

两者相除 
(3)给定购买商品的数量范围,查询某一天在该网站的购买该数量商品的用户id

select uid from bigdata_user where visit_date='2014-12-12' and behavior_type='4' group by uid having count(behavior_type='4')>5;1

(4)实时查询,某个地区的用户当天浏览网站的次数

create table scan(province string,scan int) comment 'This is the search of bigdataday' row format delimited fields terminated by '\t' stored as textfile;//创建新的数据表insert overwrite table scan select province ,count (behavior_type) from bigdata_user where behavior_type='1' group by province;//导入数据select * from scan;上海市 8364
云南  8454
内蒙古 8172
北京市 8258
台湾  8382
吉林  8272
四川  8359
天津市 8478
宁夏  8205
安徽  8205
山东  8236
山西  8503
广东  8228
广西  8358
新疆  8316
江苏  8226
江西  8403
河北  8363
河南  8382
浙江  8310
海南  8391
湖北  8183
湖南  8368
澳门  8264
甘肃  8415
福建  8270
西藏  8347
贵州  8505
辽宁  8292
重庆市 8506
陕西  8379
青海  8427
香港  8386
黑龙江 8309
Time taken: 0.248 seconds, Fetched: 34 row(s)1234567891011121314151617181920212223242526272829303132333435363738

10.创建临时表user_action

create table dblab.user_action(id string ,uid string,item_id string,behavior_type string,visit_date date,province string) comment 'Welcome to xmu dblab!' row format delimited fields terminated by '\t' stored as textfile; 1

11.这个命令执行完以后,Hive会自动在HDFS文件系统中创建对应的数据文件“/user/hive/warehouse/dblab.db/user_action”。 
我们可以新建一个终端,执行命令查看一下,确认这个数据文件在HDFS中确实被创建了,请在新建的终端中执行下面命令:

./bin/hdfs dfs -ls /user/hive/warehouse/dblab.db/user_action-rwxr-xr-x   1 hadoop supergroup   15590786 2016-11-27 21:57 /user/hive/warehouse/dblab.db/user_action/000000_012

这说明,这个数据文件在HDFS中确实被创建了。注意,这个HDFS中的数据文件,在我们后面的“使用HBase Java API把数据从本地导入到HBase中”操作中会使用到。 
12.将bigdata_user表中的数据插入到user_action(执行时间:10秒左右) 
我们已经在Hive中的dblab数据库中创建了一个外部表bigdata_user,下面把dblab.bigdata_user数据插入到dblab.user_action表中,命令如下:

insert overwrite table dblab.user_action select * from dblab.bigdata_user;select * from user_action limit 10;12

三、使用sqoop将数据从Hive导入mysql 
启动hadoop集群,Mysql服务 
(1)登录mysql

[hadoop@master hadoop-2.7.3]$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 1234567891011

(2)创建数据库

mysql> create database dblab;mysql> show databases;
+--------------------+| Database           |
+--------------------+| information_schema |
| dblab              |
| hive               |
| mysql              |
+--------------------+
4 rows in set (0.00 sec)mysql> use dblab;
Database changed1234567891011121314

请使用下面命令查看数据库的编码:

mysql> show variables like 'character%';
+--------------------------+----------------------------+| Variable_name            | Value                      |
+--------------------------+----------------------------+| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       || character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+8 rows in set (0.00 sec)123456789101112131415

(3)创建表 
下面在MySQL的数据库dblab中创建一个新表user_action,并设置其编码为utf-8:

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=utf-8;1

(4)导入数据(执行时间:20秒左右) 
注意,刚才已经退出MySQL,回到了Shell命令提示符状态。下面就可以执行数据导入操作。

./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';1

字段解释:

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

16/11/28 09:18:12 INFO mapreduce.Job: Job job_local1006738657_0001 completed successfully16/11/28 09:18:12 INFO mapreduce.Job: Counters: 20
    File System Counters
        FILE: Number of bytes read=72216458
        FILE: Number of bytes written=73973600
        FILE: Number of read operations=0
        FILE: Number of large read operations=0
        FILE: Number of write operations=0
        HDFS: Number of bytes read=38989562
        HDFS: Number of bytes written=0
        HDFS: Number of read operations=78
        HDFS: Number of large read operations=0
        HDFS: Number of write operations=0
    Map-Reduce Framework
        Map input records=300000
        Map output records=300000
        Input split bytes=696
        Spilled Records=0
        Failed Shuffles=0
        Merged Map outputs=0
        GC time elapsed (ms)=145
        Total committed heap usage (bytes)=511180800
    File Input Format Counters 
        Bytes Read=0
    File Output Format Counters 
        Bytes Written=016/11/28 09:18:12 INFO mapreduce.ExportJobBase: Transferred 37.1833 MB in 29.2772 seconds (1.27 MB/sec)16/11/28 09:18:12 INFO mapreduce.ExportJobBase: Exported 300000 records.12345678910111213141516171819202122232425262728

(4)查看MySQL中user_action表数据

mysql> select * from user_action limit 10;1
+--------+-----------+-----------+---------------+---------------+------------+-----------+| id     | uid       | item_id   | behavior_type | item_category | visit_date | province  |
+--------+-----------+-----------+---------------+---------------+------------+-----------+| 225653 | 102865660 | 164310319 | 1             | 5027          | 2014-12-08 | 香港      |
| 225654 | 102865660 | 72511722  | 1             | 1121          | 2014-12-13 | 天津市    |
| 225655 | 102865660 | 334372932 | 1             | 5027          | 2014-11-30 | 江苏      |
| 225656 | 102865660 | 323237439 | 1             | 5027          | 2014-12-02 | 广东      |
| 225657 | 102865660 | 323237439 | 1             | 5027          | 2014-12-07 | 山西      |
| 225658 | 102865660 | 34102362  | 1             | 1863          | 2014-12-13 | 内蒙古    |
| 225659 | 102865660 | 373499226 | 1             | 12388         | 2014-11-26 | 湖北      |
| 225660 | 102865660 | 271583890 | 1             | 5027          | 2014-12-06 | 山西      |
| 225661 | 102865660 | 384764083 | 1             | 5399          | 2014-11-26 | 澳门      || 225662 | 102865660 | 139671483 | 1             | 5027          | 2014-12-03 | 广东      |
+--------+-----------+-----------+---------------+---------------+------------+-----------+10 rows in set (0.00 sec)123456789101112131415

从Hive导入数据到MySQL中,成功! 
四、使用Sqoop将数据从MySQL导入HBase 
1、启动Hadoop集群、MySQL服务、HBase服务

start-hbase.sh1
start-shell1

2.创建表user_action

create 'user_action' ,{NAME=>'f1',VERSIONS=>5}1

HBase中创建了一个user_action表,这个表中有一个列族f1(你愿意把列族名称取为其他名称也可以,比如列族名称为userinfo),历史版本保留数量为5。 
3.导入数据

import source destination 
export destination source

./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 -11

注意:IP部分改为本机IP地址或localhost。同时,HBase只支持十六进制存储中文。 
命令解释如下:

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

执行上面的sqoop import命令以后,会得到类似下面的结果(省略了很多非重要的屏幕信息):

16/11/28 10:03:40 INFO mapreduce.Job: Counters: 20
    File System Counters
        FILE: Number of bytes read=39428226
        FILE: Number of bytes written=40095167
        FILE: Number of read operations=0
        FILE: Number of large read operations=0
        FILE: Number of write operations=0
        HDFS: Number of bytes read=0
        HDFS: Number of bytes written=0
        HDFS: Number of read operations=0
        HDFS: Number of large read operations=0
        HDFS: Number of write operations=0
    Map-Reduce Framework
        Map input records=300000
        Map output records=300000
        Input split bytes=87
        Spilled Records=0
        Failed Shuffles=0
        Merged Map outputs=0
        GC time elapsed (ms)=1018
        Total committed heap usage (bytes)=127795200
    File Input Format Counters 
        Bytes Read=0
    File Output Format Counters 
        Bytes Written=016/11/28 10:03:40 INFO mapreduce.ImportJobBase: Transferred 0 bytes in 69.7527 seconds (0 bytes/sec)16/11/28 10:03:40 INFO mapreduce.ImportJobBase: Retrieved 300000 records.123456789101112131415161718192021222324252627

4.查看HBase中user_action表数据

habse> scan 'user_action',{LIMIT=>10}  #只查询前面10行1
 1                                     column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 1                                     column=f1:item_category, timestamp=1480298573684, value=4076                                                  
 1                                     column=f1:item_id, timestamp=1480298573684, value=285259775                                                   
 1                                     column=f1:province, timestamp=1480298573684, value=\xE5\xB9\xBF\xE4\xB8\x9C                                   
 1                                     column=f1:uid, timestamp=1480298573684, value=10001082                                                        
 1                                     column=f1:visit_date, timestamp=1480298573684, value=2014-12-08                                               
 10                                    column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 10                                    column=f1:item_category, timestamp=1480298573684, value=10894                                                 
 10                                    column=f1:item_id, timestamp=1480298573684, value=323339743                                                   
 10                                    column=f1:province, timestamp=1480298573684, value=\xE5\xB1\xB1\xE4\xB8\x9C                                   
 10                                    column=f1:uid, timestamp=1480298573684, value=10001082                                                        
 10                                    column=f1:visit_date, timestamp=1480298573684, value=2014-12-12                                               
 100                                   column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 100                                   column=f1:item_category, timestamp=1480298573684, value=10576                                                 
 100                                   column=f1:item_id, timestamp=1480298573684, value=275221686                                                   
 100                                   column=f1:province, timestamp=1480298573684, value=\xE6\xB9\x96\xE5\x8C\x97                                   
 100                                   column=f1:uid, timestamp=1480298573684, value=10001082                                                        
 100                                   column=f1:visit_date, timestamp=1480298573684, value=2014-12-02                                               
 1000                                  column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 1000                                  column=f1:item_category, timestamp=1480298573684, value=3381                                                  
 1000                                  column=f1:item_id, timestamp=1480298573684, value=168463559                                                   
 1000                                  column=f1:province, timestamp=1480298573684, value=\xE5\xB1\xB1\xE8\xA5\xBF                                   
 1000                                  column=f1:uid, timestamp=1480298573684, value=100068031                                                       
 1000                                  column=f1:visit_date, timestamp=1480298573684, value=2014-12-02                                               
 10000                                 column=f1:behavior_type, timestamp=1480298575888, value=1                                                     
 10000                                 column=f1:item_category, timestamp=1480298575888, value=12488                                                 
 10000                                 column=f1:item_id, timestamp=1480298575888, value=45571867                                                    
 10000                                 column=f1:province, timestamp=1480298575888, value=\xE6\xB9\x96\xE5\x8C\x97                                   
 10000                                 column=f1:uid, timestamp=1480298575888, value=100198255                                                       
 10000                                 column=f1:visit_date, timestamp=1480298575888, value=2014-12-05                                               
 100000                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100000                                column=f1:item_category, timestamp=1480298594850, value=6580                                                  
 100000                                column=f1:item_id, timestamp=1480298594850, value=78973192                                                    
 100000                                column=f1:province, timestamp=1480298594850, value=\xE5\xB1\xB1\xE4\xB8\x9C                                   
 100000                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100000                                column=f1:visit_date, timestamp=1480298594850, value=2014-11-29                                               
 100001                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100001                                column=f1:item_category, timestamp=1480298594850, value=3472                                                  
 100001                                column=f1:item_id, timestamp=1480298594850, value=34929314                                                    
 100001                                column=f1:province, timestamp=1480298594850, value=\xE5\x8C\x97\xE4\xBA\xAC\xE5\xB8\x82                       
 100001                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100001                                column=f1:visit_date, timestamp=1480298594850, value=2014-12-15  
 100002                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100002                                column=f1:item_category, timestamp=1480298594850, value=10392                                                 
 100002                                column=f1:item_id, timestamp=1480298594850, value=401104894                                                   
 100002                                column=f1:province, timestamp=1480298594850, value=\xE6\xB1\x9F\xE8\xA5\xBF                                   
 100002                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100002                                column=f1:visit_date, timestamp=1480298594850, value=2014-11-29                                               
 100003                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100003                                column=f1:item_category, timestamp=1480298594850, value=5894                                                  
 100003                                column=f1:item_id, timestamp=1480298594850, value=217913901                                                   
 100003                                column=f1:province, timestamp=1480298594850, value=\xE9\xBB\x91\xE9\xBE\x99\xE6\xB1\x9F                       
 100003                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100003                                column=f1:visit_date, timestamp=1480298594850, value=2014-12-04                                               
 100004                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100004                                column=f1:item_category, timestamp=1480298594850, value=12189                                                 
 100004                                column=f1:item_id, timestamp=1480298594850, value=295053167                                                   
 100004                                column=f1:province, timestamp=1480298594850, value=\xE6\xB5\xB7\xE5\x8D\x97                                   
 100004                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100004                                column=f1:visit_date, timestamp=1480298594850, value=2014-11-26                                               
10 row(s) in 2.1550 seconds12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061

五、使用HBase Java API把数据从本地导入到HBase中 
1、数据准备 实际上,我们也可以编写Java程序,直接从HDFS中读取数据加载到HBase。但是,这里我们展示的是如何用JAVA程序把本地数据导入到HBase中。你只要把程序做简单修改,就可以实现从HDFS中读取数据加载到HBase。 
首先,请将之前的user_action数据从HDFS复制到Linux系统的本地文件系统中,命令如下:

cd /app/appJar/dataset
hdfs dfs -get /user/hive/warehouse/dblab.db/user_action . #将HDFS上的user_action数据复制到本地当前目录,注意'.'表示当前目录cat ./user_action/* | head -10   #查看前10行数据cat ./user_action/part-00000* >user_action.output
 > user_action.output #将part-00000*文件复制一份重命名为user_action.output,*表示通配符head user_action.output  #查看user_action.output前10行1234567

2、编写数据导入程序 
编写Java程序实现HBase数据导入功能 
编写ImportHBase程序,并打包成可执行jar包,命名为ImportHBase.jar。 
然后,请在“/home/bduser/guoyg/”目录下面新建一个appJar子目录,用来存放ImportHBase.jar。

drwxr-xr-x 2 bduser bduser   6 6月  19 14:36 appJar
drwxrwxr-x 3 bduser bduser 143 6月  19 14:32 dataset123

3、数据导入 
现在开始执行数据导入操作。 
使用Java程序将数据从本地导入HBase中,导入前,请先清空user_action表。 
请在之前已经打开的HBase Shell窗口中(也就是在“hbase>”命令提示符下)执行下面操作:

hbase> truncate 'user_action'Truncating 'user_action' table (it may take a while):
 - Disabling table...
 - Truncating table...0 row(s) in 4.0120 seconds//删除以后再查看就没有记录了hbase> scan 'user_action',{LIMIT=>10}
ROW                                    COLUMN+CELL                                                                                                   
0 row(s) in 0.4010 seconds123456789

运行hadoop jar程序

hadoop jar /home/bduser/guoyg/appJar/ImportHBase.jar HBaseImportTest /home/bduser/guoyg/dataset/user_action.output1

这个命令大概会执行3分钟左右,执行过程中,屏幕上会打印出执行进度,每执行1万条,对打印出一行信息,所以,整个执行过程屏幕上显示如下信息:

100002000030000400005000060000700008000090000100000110000120000130000140000150000160000170000180000190000200000210000220000230000240000250000260000270000280000290000300000Total Time: 259001 ms12345678910111213141516171819202122232425262728293031

4、查看HBase中user_action表数据

hbase>scan 'user_action',{LIMIT=>10}1
 1                                     column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 1                                     column=f1:item_category, timestamp=1480298573684, value=4076                                                  
 1                                     column=f1:item_id, timestamp=1480298573684, value=285259775                                                   
 1                                     column=f1:province, timestamp=1480298573684, value=\xE5\xB9\xBF\xE4\xB8\x9C                                   
 1                                     column=f1:uid, timestamp=1480298573684, value=10001082                                                        
 1                                     column=f1:visit_date, timestamp=1480298573684, value=2014-12-08                                               
 10                                    column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 10                                    column=f1:item_category, timestamp=1480298573684, value=10894                                                 
 10                                    column=f1:item_id, timestamp=1480298573684, value=323339743                                                   
 10                                    column=f1:province, timestamp=1480298573684, value=\xE5\xB1\xB1\xE4\xB8\x9C                                   
 10                                    column=f1:uid, timestamp=1480298573684, value=10001082                                                        
 10                                    column=f1:visit_date, timestamp=1480298573684, value=2014-12-12                                               
 100                                   column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 100                                   column=f1:item_category, timestamp=1480298573684, value=10576                                                 
 100                                   column=f1:item_id, timestamp=1480298573684, value=275221686                                                   
 100                                   column=f1:province, timestamp=1480298573684, value=\xE6\xB9\x96\xE5\x8C\x97                                   
 100                                   column=f1:uid, timestamp=1480298573684, value=10001082                                                        
 100                                   column=f1:visit_date, timestamp=1480298573684, value=2014-12-02                                               
 1000                                  column=f1:behavior_type, timestamp=1480298573684, value=1                                                     
 1000                                  column=f1:item_category, timestamp=1480298573684, value=3381                                                  
 1000                                  column=f1:item_id, timestamp=1480298573684, value=168463559                                                   
 1000                                  column=f1:province, timestamp=1480298573684, value=\xE5\xB1\xB1\xE8\xA5\xBF                                   
 1000                                  column=f1:uid, timestamp=1480298573684, value=100068031                                                       
 1000                                  column=f1:visit_date, timestamp=1480298573684, value=2014-12-02                                               
 10000                                 column=f1:behavior_type, timestamp=1480298575888, value=1                                                     
 10000                                 column=f1:item_category, timestamp=1480298575888, value=12488                                                 
 10000                                 column=f1:item_id, timestamp=1480298575888, value=45571867                                                    
 10000                                 column=f1:province, timestamp=1480298575888, value=\xE6\xB9\x96\xE5\x8C\x97                                   
 10000                                 column=f1:uid, timestamp=1480298575888, value=100198255                                                       
 10000                                 column=f1:visit_date, timestamp=1480298575888, value=2014-12-05                                               
 100000                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100000                                column=f1:item_category, timestamp=1480298594850, value=6580                                                  
 100000                                column=f1:item_id, timestamp=1480298594850, value=78973192                                                    
 100000                                column=f1:province, timestamp=1480298594850, value=\xE5\xB1\xB1\xE4\xB8\x9C                                   
 100000                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100000                                column=f1:visit_date, timestamp=1480298594850, value=2014-11-29                                               
 100001                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100001                                column=f1:item_category, timestamp=1480298594850, value=3472                                                  
 100001                                column=f1:item_id, timestamp=1480298594850, value=34929314                                                    
 100001                                column=f1:province, timestamp=1480298594850, value=\xE5\x8C\x97\xE4\xBA\xAC\xE5\xB8\x82                       
 100001                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100001                                column=f1:visit_date, timestamp=1480298594850, value=2014-12-15  
 100002                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100002                                column=f1:item_category, timestamp=1480298594850, value=10392                                                 
 100002                                column=f1:item_id, timestamp=1480298594850, value=401104894                                                   
 100002                                column=f1:province, timestamp=1480298594850, value=\xE6\xB1\x9F\xE8\xA5\xBF                                   
 100002                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100002                                column=f1:visit_date, timestamp=1480298594850, value=2014-11-29                                               
 100003                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100003                                column=f1:item_category, timestamp=1480298594850, value=5894                                                  
 100003                                column=f1:item_id, timestamp=1480298594850, value=217913901                                                   
 100003                                column=f1:province, timestamp=1480298594850, value=\xE9\xBB\x91\xE9\xBE\x99\xE6\xB1\x9F                       
 100003                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100003                                column=f1:visit_date, timestamp=1480298594850, value=2014-12-04                                               
 100004                                column=f1:behavior_type, timestamp=1480298594850, value=1                                                     
 100004                                column=f1:item_category, timestamp=1480298594850, value=12189                                                 
 100004                                column=f1:item_id, timestamp=1480298594850, value=295053167                                                   
 100004                                column=f1:province, timestamp=1480298594850, value=\xE6\xB5\xB7\xE5\x8D\x97                                   
 100004                                column=f1:uid, timestamp=1480298594850, value=101480065                                                       
 100004                                column=f1:visit_date, timestamp=1480298594850, value=2014-11-26                                               
10 row(s) in 0.6380 seconds12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061

至此使用Hive进行ETL结束。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
BigData:大数据开发的简介、核心知识(linux基础+Java/Python编程语言+Hadoop{HDFS、HBase、Hive}+Docker)、经典场景应用之详细攻略
Sqoop
Spark SQL 在字节跳动的优化实践
收藏!6道常见hadoop面试题及答案解析
Hbase与Hive的区别与联系
Impala之01-基本介绍
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服