一、环境整合
1.1、环境配置
执行命令vi /etc/profile 配置hive和hbase的环境变量
1.2、导入依赖
由于 Hive 要连接到 HBase, 所以 Hive 需要知道 HBase 的一些jar 包,所以在 hive-env.sh
中添加一行如下配置:
export HIVE_AUX_JARS_PATH=/data/soft/hbase-2.3.2/lib
或是直接把jar拷贝到hive包/lib里面
cp /data/soft/hbase-2.3.2/lib/hbase-common-2.3.2.jar /data/soft/apache-hive-3.1.2-bin/lib
cp /data/soft/hbase-2.3.2/lib/hbase-server-2.3.2.jar /data/soft/apache-hive-3.1.2-bin/lib
cp /data/soft/hbase-2.3.2/lib/hbase-client-2.3.2.jar /data/soft/apache-hive-3.1.2-bin/lib
cp /data/soft/hbase-2.3.2/lib/hbase-protocol-2.3.2.jar /data/soft/apache-hive-3.1.2-bin/lib
cp /data/soft/hbase-2.3.2/lib/hbase-it-2.3.2.jar /data/soft/apache-hive-3.1.2-bin/lib
cp /data/soft/hbase-2.3.2/lib/hbase-hadoop2-compat-2.3.2.jar /data/soft/apache-hive-3.1.2-bin/lib
cp /data/soft/hbase-2.3.2/lib/hbase-hadoop-compat-2.3.2.jar /data/soft/apache-hive-3.1.2-bin/lib
1.3 修改相关配置文件
1.4 启动相关服务
二、关联Hive和HBase数据
建立 Hive 表,关联 HBase 表,插入数据到 Hive 表的同时能够影响 HBase表。
2.1 在 Hive 中创建表同时关联 HBase
CREATE TABLE hive_hbase_emp_table(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:ename,info:job,info:mgr,info:hiredate,info:sal,info:comm,info:deptno")
TBLPROPERTIES ("hbase.table.name" = "hbase_emp_table");
其中 org.apache.hadoop.hive.hbase.HBaseStorageHandler 类主要用于将hive与hbase关联,在hivez中创建的表会映射到hbase数据库中
在 Hive 和 HBase 中分别查看是否生成了相应的表.
2.2 创建Hive 临时中间表
由于不能将数据直接插入与hbase关联的hive表中,所以需要创建中间表
CREATE TABLE emp_tmp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
row format delimited fields terminated by '\t';
把数据导入临时表中
load data local inpath '/data/soft/hivedata/emp.txt' into table emp_tmp
2.3 通过insert命令将临时中间表的数据导入到hive_hbase_emp_table中
insert into table hive_hbase_emp_table select * from emp_tmp
2.4 查看数据是否同步
Hive : select * from hive_hbase_emp_table;
HBase: scan 'hbase_emp_table'