Hive整合HBase——通过Hive读/写 HBase中的表


写在前面一:

本文将Hive与HBase整合在一起,使Hive可以读取HBase中的数据,让Hadoop生态系统中最为常用的两大框架互相结合,相得益彰。


写在前面二:

使用软件说明


约定所有软件的存放目录:

/home/yujianxin

一、Hive整合HBase原理

Hive与HBase整合的实现是利用两者本身对外的API接口互相进行通信,相互通信主要是依靠hive-hbase-handler-0.9.0.jar工具类,如下图


Hive与HBase通信示意图


二、具体步骤

安装前说明

1、关于Hadoop、HBase、Hive集群的搭建,请参考本人博文“基于Hadoop的数据分析综合管理平台之Hadoop、HBase完全分布式集群搭建”

2、本文中Hadoop、HBase、Hive安装路径


2.1、拷贝jar包
删除$HIVE_HOME/lib/下HBase、Zookeeper相关jar

rm -rf $HIVE_HOME/lib/zookeeper-*.jar
rm -rf $HIVE_HOME/lib/hbase*.jar
重新拷贝
cp $HBASE_HOME/hbase-0.94.7-security.jar $HIVE_HOME/lib/ 
cp $HBASE_HOME/lib/zookeeper-3.4.5.jar $HIVE_HOME/lib/

2.2、修改$HIVE_HOME/conf/hive-site.xml
mkdir $HIVE_HOME/logs
在尾部添加

<property>
<name>hive.querylog.location</name>
<value>/home/yujianxin/hive/hive-0.9.0/logs</value>
</property>
<property>
<name>hive.aux.jars.path</name>
<value>
file:///home/yujianxin/hive/hive-0.9.0/lib/hive-hbase-handler-0.9.0.jar,
file:///home/yujianxin/hive/hive-0.9.0/lib/hbase-0.94.7-security.jar,
file:///home/yujianxin/hive/hive-0.9.0/lib/zookeeper-3.4.5.jar	
</value>
</property>
修改
<property>
  <name>hive.zookeeper.quorum</name>
  <value>master,slave1,slave2</value>
</property>
2.3、拷贝hbase-0.94.7-security.jar到所有hadoop节点(包括master)的hadoop/lib下
cp $HBASE_HOME/hbase-0.94.7-security.jar $HADOOP_HOME/lib
2.4、拷贝hbase/conf下的hbase-site.xml文件到所有hadoop节点(包括master)的hadoop/conf下
cp $HBASE_HOME/conf/hbase-site.xml  $HADOOP_HOME/conf

三、启动、使用配置后Hive,测试是否配置成功

3.1、启动Hive

集群方式启动

hive --auxpath /home/yujianxin/hive/hive-0.9.0/lib/hive-hbase-handler-0.9.0.jar,/home/
yujianxin/hive/hive-0.9.0/lib/hbase-0.94.7-security.jar,/home/yujianxin/hive/hive-0.9.
0/lib/zookeeper-3.4.5.jar

可以将此启动Hive与HBase整合的命令写成Shell脚本,设置成开机启动


3.2、在Hive中创建HBase识别的表

CREATE TABLE hbase_hive_1(key int, value string) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val") 
TBLPROPERTIES ("hbase.table.name" = "xyz");

hbase.table.name 定义在hbase中的table名称

多列时,data:1,data:2
多列族时,data1:1,data2:1
hbase.columns.mapping 定义在hbase的列族,里面的:key 是固定值而且要保证在表pokes中的foo字段是唯一值

创建有分区的表

CREATE TABLE hbase_hive_2(key int, value string)  
partitioned by (day string) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val") 
TBLPROPERTIES ("hbase.table.name" = "xyz2");

分别查看Hive、HBase中建立的表

    

3.3、导入数据

新建hive的数据表

create table pokes(foo int,bar string)
row format delimited fields terminated by ',';
批量导入数据


load data local inpath '/home/yujianxin/temp/data1.txt' overwrite into table pokes;
使用sql导入hbase_table_1
SET hive.hbase.bulk=true;
insert overwrite table hbase_hive_1 select * from pokes;
导入有分区的表
insert overwrite table hbase_hive_2  partition (day='2012-01-01') select * from pokes;

往Hive中插入数据同时会插入到HBase中

3.4、分别查看Hive、HBase中的数据


OK,到此Hive、HBase整合成功。

——————————————————————————————————————————————————————————————————

下面再给出较复杂的测试例子

情况一、对于在hbase已经存在的表,在hive中使用CREATE EXTERNAL TABLE来建立联系

create external table hive_test (key int,gid map<string,string>,sid map<string,string>,uid map<string,string>) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
WITH SERDEPROPERTIES ("hbase.columns.mapping" ="a:,b:,c:")  
TBLPROPERTIES  ("hbase.table.name" = "test1");


查询gid字段中value值


Hive成功读取到HBase中的数据

情况二、如果hbase表test2中的字段为user:gid,user:sid,info:uid,info:level


在hive中建表语句为

CREATE EXTERNAL TABLE hive_test_2(key int,user map<string,string>,info map<string,string>) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
WITH SERDEPROPERTIES ("hbase.columns.mapping" ="user:,info:")  
TBLPROPERTIES  ("hbase.table.name" = "test2");

Hive成功读取到HBase中的数据


相关内容