版本说明:
HDP:3.0.1.0
Hive:3.1.0
HBase:2.0.0
之前学习 HBase 就有疑惑,HBase 虽然可以存储数亿或数十亿行数据,但是对于数据分析来说,不太友好,只提供了简单的基于 Key 值的快速查询能力,没法进行大量的条件查询。
不过,Hive 与 HBase 的整合可以实现我们的这个目标。不仅如此,还能通过 Hive 将数据批量地导入到 HBase 中。
Hive 与 HBase 整合的实现是利用两者本身对外的 API 接口互相通信来完成的,其具体工作交由 Hive 的 lib 目录中的 hive-hbase-handler-xxx.jar 工具类来实现对 HBase 数据的读取。
Hive 与 HBase 整合的适用场景:
1、通过 Hive 与 HBase 整合,可以将 HBase 的数据通过 Hive 来分析,让 HBase 支持 JOIN、GROUP 等 SQL 查询语法。
2、实现将批量数据导入到 HBase 表中。
需要有以下依赖,ambari 已经为我们做好了这一切:
注意,这里与HDP 2.x不同:在 HDP 3.0 中对 Hive-3.1.0 的更改是所有 StorageHandler必须标记为“外部”,没有 StorageHandler 创建的非外部表。如果在创建 Hive 表时存在相应的 HBase 表,它将模仿“外部”表的 HDP 2.x 语义。如果在创建 Hive 表时不存在相应的 HBase 表,则它将模仿非外部表的 HDP 2.x 语义。
总结: 不管 HBase 表是否存在,在 Hive 中都要使用 external 表来与 HBase 表进行关联,如果关联的 HBase 表不存在,Hive 会自动创建Hbase 表。
1. HBase表不存在
CREATE EXTERNAL TABLE hive_table (key int, value string) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val") TBLPROPERTIES ("hbase.table.name" = "default:hbase_table");
这里简单说一下建表时的参数:
此时,hive_table 与 hbase_table 都是空的。我们准备一些数据:
insert into hive_table (key, value) values(1, "www.ymq.io");
insert 语句会触发 map 任务,如下图所示:
任务完成之后,Hive 与 HBase 表中就都存在数据了。
# hive_table 表数据 +-----------------+-------------------+ | hive_table.key | hive_table.value | +-----------------+-------------------+ | 1 | www.ymq.io | +-----------------+-------------------+ # hbase_table表数据 hbase(main):002:0> scan 'hbase_table' ROW COLUMN+CELL 1 column=cf1:val, timestamp=1558710260266, value=www.ymq.io 1 row(s) Took 0.2400 seconds
当将 hive_table 表删除,对应的 hbase_table 表不受影响,里面依旧有数据。当删除 hbase_table 表后,再查询 hive_table 表数据,会报错:Error: java.io.IOException: org.apache.hadoop.hbase.TableNotFoundException: hbase_table (state=,code=0),这是正常的。
注意!注意!注意: 在上述示例中,我们使用的 insert 命令向 Hive 表中插入数据。对于批量数据的插入,还是建议使用 load 命令,已经为大家精心准备了大数据的系统学习资料,从Linux-Hadoop-spark-......,需要的小伙伴可以点击但对于 Hive 外部表来说,不支持 load 命令。我们可以先创建一个 Hive 内部表,将数据 load 到该表中,最后将查询内部表的所有数据都插入到与 Hbase 关联的 Hive 外部表中,就可以了,相当于中转一下。
2. HBase表已存在
创建 HBase 表:
create 'default:people', {NAME=>'basic_info'}, {NAME=>'other_info'}
插入一些数据:
put 'people', '00017','basic_info:name','tom' put 'people', '00017','basic_info:age','17' put 'people', '00017','basic_info:sex','man' put 'people', '00017','other_info:telPhone','176xxxxxxxx' put 'people', '00017','other_info:country','China' put 'people', '00023','basic_info:name','mary' put 'people', '00023','basic_info:age',23 put 'people', '00023','basic_info:sex','woman' put 'people', '00023','basic_info:edu','college' put 'people', '00023','other_info:email','[email protected]' put 'people', '00023','other_info:country','Japan' put 'people', '00018','basic_info:name','sam' put 'people', '00018','basic_info:age','18' put 'people', '00018','basic_info:sex','man' put 'people', '00018','basic_info:edu','middle' put 'people', '00018','other_info:telPhone','132xxxxxxxx' put 'people', '00018','other_info:country','America' put 'people', '00026','basic_info:name','Sariel' put 'people', '00026','basic_info:age',26 put 'people', '00026','basic_info:edu','college' put 'people', '00026','other_info:telPhone','178xxxxxxxx' put 'people', '00026','other_info:email','[email protected]' put 'people', '00026','other_info:country','中国'
再创建一个简单的 Hive 外部表,语法与之前的一致:
create external table people ( id int, name string, age string, sex string, edu string, country string, telPhone string, email string ) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties ("hbase.columns.mapping" = " :key, basic_info:name, basic_info:age, basic_info:sex, basic_info:edu, other_info:country, other_info:telPhone, other_info:email ") tblproperties("hbase.table.name" = "default:people");
查询全部数据:
select * from people;
条件查询:
# 根据性别查询 select * from people where sex = 'man'; # 根据年龄查询 select * from people where age > 18;
这样,我们就可以使用 Hive 来分析 HBase 中的数据了。
码字不易,如果您觉得文章写得不错,请关注作者~ 您的关注是我写作的最大动力
友情提示:原文排版精美,可点击分享链接查看。
码字不易,如果感觉本文对您有帮助,请点赞或订阅支持一下,您的支持是我坚持写作最大的动力,谢谢!