Hive提供了与HBase的集成,使得能够在HBase表上使用HQL语句进行查询 插入操作以及进行Join和Union等复杂查询、同时也可以将hive表中的数据映射到Hbase中。
1.1 hive整合hbase的搭建配置
说明:Hive是单节点,hbase为集群
修改hive-site.xml文件,添加zookeeper配置
<!--hive整合hbase -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>node1:2181,node2:2181,node3:2181</value>
</property>

修改hive-env.sh文件
export HADOOP_HOME=/opt/hadoop-2.7.5
export HBASE_HOME=/opt/hbase-1.2.8

到此配置完成
本案例参考:https://www.maiyewang.com/?p=32055
https://blog.csdn.net/aaronhadoop/article/details/28398157 Hive整合HBase——通过Hive读/写 HBase中的表 hive集群版
2.2 hive表映射到hbase表
2.2.1 准备数据
create table hv_source(id string,name string,age int,sex string,address string,hobby string) row format delimited fields terminated by "\t";
load data local inpath '/opt/hv_source.txt' into table hv_source;
2.2.2 创建关联表
create table hive_student(
id string,
name string,
age int,
sex string,
address string,
hobby string
)STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,user:name,user:age,data:sex,info:address,info:hobby")
TBLPROPERTIES ("hbase.table.name" = "hbase_student");
2.2.3 查看新创建的hive和hbase表
#查看hive表
hive (hv_user_profile)> select * from hive_student;
OK
id name age sex address hobby
Time taken: 1.192 seconds
#查看hbase表
hbase(main):007:0> list
TABLE
hbase_student
可以看到:二者均无数据。
2.2.4 向hive和hbase表中添加数据
#添加数据
hive (hv_user_profile)> insert overwrite table hive_student select * from hv_source;
WARNING: 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.
Query ID = www_20190618150114_34bc8cca-34cd-4019-82fd-341c17de6f48
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1560828210336_0001, Tracking URL = http://node2:8088/proxy/application_1560828210336_0001/
Kill Command = /opt/hadoop-2.7.5/bin/hadoop job -kill job_1560828210336_0001
Hadoop job information for Stage-3: number of mappers: 1; number of reducers: 0
2019-06-18 15:01:22,500 Stage-3 map = 0%, reduce = 0%
2019-06-18 15:01:29,835 Stage-3 map = 100%, reduce = 0%, Cumulative CPU 2.91 sec
MapReduce Total cumulative CPU time: 2 seconds 910 msec
Ended Job = job_1560828210336_0001
MapReduce Jobs Launched:
Stage-Stage-3: Map: 1 Cumulative CPU: 2.91 sec HDFS Read: 4938 HDFS Write: 0 SUCCESS
Total MapReduce CPU Time Spent: 2 seconds 910 msec
OK
id name age sex address hobby
Time taken: 16.858 seconds
hive (hv_user_profile)> select * from hv_student;
FAILED: SemanticException [Error 10001]: Line 1:14 Table not found 'hv_student'
#查看数据
hive (hv_user_profile)> select * from hive_student;
OK
id name age sex address hobby
1 liu 23 male 北京 音乐
2 jian 24 female 天津 读书
Time taken: 0.26 seconds, Fetched: 2 row(s)
hive (hv_user_profile)>
#查看hbase数据
hbase(main):002:0> scan 'hbase_student'
ROW COLUMN+CELL
1 column=data:sex, timestamp=1560841288585, value=male
1 column=info:address, timestamp=1560841288585, value=\xE5\x8C\x97\xE4\xBA\xAC
1 column=info:hobby, timestamp=1560841288585, value=\xE9\x9F\xB3\xE4\xB9\x90
1 column=user:age, timestamp=1560841288585, value=23
1 column=user:name, timestamp=1560841288585, value=liu
2 column=data:sex, timestamp=1560841288585, value=female
2 column=info:address, timestamp=1560841288585, value=\xE5\xA4\xA9\xE6\xB4\xA5
2 column=info:hobby, timestamp=1560841288585, value=\xE8\xAF\xBB\xE4\xB9\xA6
2 column=user:age, timestamp=1560841288585, value=24
2 column=user:name, timestamp=1560841288585, value=jian
2 row(s) in 0.1040 seconds

2.3 hbase表映射到hive表
说明hbase表先有数据,然后建立关系表,hbase表数据同步到hive表中。
如果你想要Hive去访问Hbase中已经存在的表,你可以创建外部表
hbase中已经存在数据表,hive和hbase做关联,需要在hive中创建外部表。
2.3.1 在hbase中建表添数据
hbase(main):003:0> create table 'hb_test','userinfo','userdata'
NoMethodError: undefined method `table' for #<Object:0x1bbddada>
hbase(main):004:0> create 'hb_test','userinfo','userdata'
0 row(s) in 1.2640 seconds
=> Hbase::Table - hb_test
hbase(main):005:0> put 'hb_test','rk01','userinfo:id','1'
0 row(s) in 0.0550 seconds
hbase(main):006:0> put 'hb_test','rk01','userinfo:name','admin'
0 row(s) in 0.0080 seconds
hbase(main):007:0> put 'hb_test','rk01','userdata:age','23'
0 row(s) in 0.0080 seconds
hbase(main):008:0> put 'hb_test','rk02','userinfo:id','2'
0 row(s) in 0.0090 seconds
hbase(main):009:0> put 'hb_test','rk02','userinfo:name','mb'
0 row(s) in 0.0070 seconds
hbase(main):010:0> put 'hb_test','rk02','userdata:age','38'
0 row(s) in 0.0070 seconds
hbase(main):011:0> put 'hb_test','rk02','userdata:id','3'
0 row(s) in 0.0080 seconds
2.3.2 建立关系表
CREATE EXTERNAL TABLE hv_test(key string,id int,name string comment ‘xingming’,age int) STORED BY
'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES
("hbase.columns.mapping" = ":key,userinfo:id,userinfo:name,userdata:age")
TBLPROPERTIES("hbase.table.name" = "hb_test");
如果要增加字段的注释,那么在字段后面添加comment ‘你要描述的’。
hbase.columns.mapping 中的值分别对应hbase表中的 列族:列的形式,其中 :key是固定写法,对应hbase中的rowkey
hbase.table.name hbase中的表名
org.apache.hadoop.hive.hbase.HBaseStorageHandler 使用的处理器
这样hive就和hbase中已经存在的表进行了关联。
Hive中的字段列要和hbase中的“列族:列”的形式一一对应。

2.3.3 在hive和hbase中查看数据
hbase(main):018:0> scan 'hb_test'
ROW COLUMN+CELL
rk01 column=userdata:age, timestamp=1560847189568, value=23
rk01 column=userinfo:id, timestamp=1560847172589, value=1
rk01 column=userinfo:name, timestamp=1560847181718, value=admin
rk02 column=userdata:age, timestamp=1560847213360, value=38
rk02 column=userinfo:id, timestamp=1560847197465, value=2
rk02 column=userinfo:name, timestamp=1560847205512, value=mb
rk03 column=userinfo:id, timestamp=1560848101350, value=3
3 row(s) in 0.0110 seconds

#hive中查看
hive (hv_user_profile)> select * from hv_test;
OK
key id name age
rk01 1 admin 23
rk02 2 mb 38
rk03 3 NULL NULL
Time taken: 0.225 seconds, Fetched: 3 row(s)
我们就可以使用 Hive 来分析 HBase 中的数据了。
2.4 总结
1.使用 hive-hbase-handler-xxx.jar 包实现 Hive 与 HBase 关联。
2.Hive 表与 HBase 表关联后,数据可以在 Hive 端插入,也可在 HBase 中插入。
3.在上述示例中,我们使用的 insert 命令向 Hive 表中插入数据。对于批量数据的插入,还是建议使用 load 命令,但对于 Hive 外部表来说,不支持 load 命令。我们可以先创建一个 Hive 内部表,将数据 load 到该表中,最后将查询内部表的所有数据都插入到与 HBase 关联的 Hive 外部表中,就可以了,相当于中转一下
本文介绍了Hive与HBase的集成,可在HBase表上用HQL进行复杂查询。详细说明了Hive整合HBase的搭建配置,包括修改相关文件。还阐述了Hive表与HBase表相互映射的操作,如准备数据、创建关联表、添加数据等,最后总结了关联实现方式及数据插入建议。

9528

被折叠的 条评论
为什么被折叠?



