测试用的版本:hive 2.3.0 hbase2.0.2
1.将hbase lib中的部分jar拷贝到hive的lib下
需要拷贝的jar:
hbase-client-2.0.2.jar
hbase-common-2.0.2.jar
hbase-hadoop2-compat-2.0.2.jar
hbase-hadoop-compat-2.0.2.jar
hbase-it-2.0.2.jar
hbase-protocol-2.0.2.jar
hbase-server-2.0.2.jar
htrace-core-3.2.0-incubating.jar
也可以不拷贝,创建软连接:
$ ln -s $HBASE_HOME/lib/hbase-client-2.0.2.jar $HIVE_HOME/lib/hbase-client-2.0.2.jar
$ ln -s $HBASE_HOME/lib/hbase-common-2.0.2.jar $HIVE_HOME/lib/hbase-common-2.0.2.jar
$ ln -s $HBASE_HOME/lib/hbase-hadoop2-compat-2.0.2.jar $HIVE_HOME/lib/hbase-hadoop2-compat-2.0.2.jar
$ ln -s $HBASE_HOME/lib/hbase-hadoop-compat-2.0.2.jar $HIVE_HOME/lib/hbase-hadoop-compat-2.0.2.jar
$ ln -s $HBASE_HOME/lib/hbase-it-2.0.2.jar $HIVE_HOME/lib/hbase-it-2.0.2.jar
$ ln -s $HBASE_HOME/lib/hbase-protocol-2.0.2.jar $HIVE_HOME/lib/hbase-protocol-2.0.2.jar
$ ln -s $HBASE_HOME/lib/hbase-hadoop2-compat-1.3.1.jar $HIVE_HOME/lib/hbase-server-2.0.2.jar
$ ln -s $HBASE_HOME/lib/htrace-core-3.2.0-incubating.jar $HIVE_HOME/lib/htrace-core-3.2.0-incubating.jar
2.在 hive-site.xml 中修改 zookeeper 的属性
<property>
<name>hive.zookeeper.quorum</name>
<value>linux01,linux02,linux03</value>
</property>
<property>
<name>hive.zookeeper.client.port</name>
<value>2181</value>
</property>
3.前两步准备工作做好后,就可以测试了
案例1:在 在 Hive 中创建表同时关联 HBase
(1)创建表:
create table if not exists hive_hbase_student_info
(
id int,
name string,
sex string
)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" =
":key,info:name,info:sex")
TBLPROPERTIES ("hbase.table.name" = "hbase_student_table");
创建成功后,在hive中可以看到hive_hbase_student_info,hbase中可以看到hbase_student_table
(2)在 Hive 中创建临时中间表,用于 load 文件中的数据
create table if not exists student_info
(
id int,
name string,
sex string
)
row format delimited fields terminated by '|' lines terminated by '\n';
(3)向 向 Hive 中间表中 load 数据
load data local inpath '/opt/datas/student_info.txt' into table student_info;
(4)通过 insert 命令将中间表中的数据导入到 Hive 关联 HBase
insert into table hive_hbase_student_info select * from student_info;
(5)查看hive表中的数据
select * from hive_hbase_student_info;
OK
hive_hbase_student_info.id hive_hbase_student_info.name hive_hbase_student_info.sex
1 小明 男
2 小红 女
Time taken: 0.199 seconds, Fetched: 2 row(s)
(6)查看hbase表中的数据
hbase(main):012:0> scan 'hbase_student_table'
ROW COLUMN+CELL
1 column=info:name, timestamp=1543908756083, value=\xE5\xB0\x8F\xE6\x98\x8E
1 column=info:sex, timestamp=1543908756083, value=\xE7\x94\xB7
2 column=info:name, timestamp=1543908756083, value=\xE5\xB0\x8F\xE7\xBA\xA2
2 column=info:sex, timestamp=1543908756083, value=\xE5\xA5\xB3
2 row(s)
Took 0.0406 seconds
案例二:
在 HBase 中已经存储了某一张表 hbase_student_table,然后在 Hive 中创建一
个外部表来关联 HBase 中的 hbase_student_table这张表,使之可以借助 Hive
来分析 HBase 这张表中的数据。
(1)hive中创建外部表
create external table if not exists relevance_student_info
(
id int,
name string,
sex string
)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" =
":key,info:name,info:sex")
TBLPROPERTIES ("hbase.table.name" = "hbase_student_table");
(2)关联后就可以使用HQL和hive中的函数来操作了
select * from relevance_student_info;
OK
relevance_student_info.id relevance_student_info.name relevance_student_info.sex
1 小明 男
2 小红 女
Time taken: 0.324 seconds, Fetched: 2 row(s)
4.student_info.txt的数据
1|小明|男
2|小红|女
student_info.txt编码格式为UTF-8-BOM