为了账号安全,请及时绑定邮箱和手机立即绑定

如何将Hive与HBase整合联用

版本说明:

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 已经为我们做好了这一切:

  • 已有 HDFS、MapReduce、Hive、Zookeeper、HBase 环境。
  • 确保 Hive 的 lib 目录下有 hive-hbase-handler-xxx.jar、Zookeeper jar、HBase Server jar、HBase Client jar 包。

四、使用HBase Hive集成

注意,这里与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");

这里简单说一下建表时的参数:

  • hbase.columns.mapping 是必须的,这将会和 HBase 表的列族进行验证。
  • hbase.table.name 属性是可选的,默认指定 HBase 表名与 Hive 表名一致。

此时,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 命令,但对于 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','cdsvo@163.com'
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','12345@126.com'
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 中的数据了。

六、总结

  • 使用 hive-hbase-handler-xxx.jar 包实现 Hive 与 HBase 关联。
  • Hive 读取的是 HBase 表最新的数据。
  • 通过 Hive 创建的 HBase 表的值默认只有一个 VERSION ,可之后再修改 HBase 表值的最大 VERSION 数。
  • Hive 只显示与 HBase 对应的列值,而那些没有对应的 HBase 列在 Hive 表中不显示。
  • Hive 表与 HBase 表关联后,数据可以在 Hive 端插入,也可在 HBase 中插入。
  • 创建 Hive 外部表与 HBase 的关联,可实现将 Hive 数据导入到 HBase 中。该方式是利用两者本身对外的 API 接口互相通信来完成的,在数据量不大(4T以下)的情况下可以选择该方式导入数据。

点关注,不迷路

好了各位,以上就是这篇文章的全部内容了,能看到这里的人呀,都是 人才

白嫖不好,创作不易。 各位的支持和认可,就是我创作的最大动力,我们下篇文章见!

如果本篇博客有任何错误,请批评指教,不胜感激 !

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消