Hive 和 HBase 整合

整合后的目标:

  1. 在 Hive 中创建的表能直接创建保存到 HBase 中。

  2. 往 Hive 中的表插入数据,数据会同步更新到 HBase 对应的表中。

  3. HBase 对应的列簇值变更,也会在 Hive 中对应的表中变更。

Hive 和 HBase 通信主要是依靠 $HIVE_HOME/lib 目录下的hive-hbase-handler-1.2.2.jar 来实现.

1 配置

Hive 配置文件修改

建议: HIVE_HOME 和 HBASE_HOME 都进行配置.

由于 Hive 要连接到 HBase, 所以 Hive 需要知道 HBase 的一些jar 包

所以在 hive-env.sh 中添加一行如下配置:

# 就是配置 Hive 需要的一些额外 jar 包的路径
export HIVE_AUX_JARS_PATH=/opt/module/hbase-1.3.2.1/lib

还要连接到 Zookeeper, 所以需要知道 Zookeeper 的地址

建议: HIVE_HOME 和 HBASE_HOME 都进行配置. 打开hive-site.xml 文件, 添加如下配置:

<!-- zookeeper 地址-->
<property>
    <name>hive.zookeeper.quorum</name>
    <value>hadoop201,hadoop202,hadoop203</value>
    <description>The list of ZooKeeper servers to talk to. This is only needed for read/write locks.</description>
</property>
<!-- zookeeper 端口号 -->
<property>
    <name>hive.zookeeper.client.port</name>
    <value>2181</value>
    <description>The port of ZooKeeper servers to talk to. This is only needed for read/write locks.</description>
</property>

更换:hive-hbase-handler-1.2.2.jar 文件

由于 Hive 自带的这个 jar 包与 HBase 不兼容, 所以替换为重新编译的.

路径在:$HIVE_HOME/lib 下

2 案例1

目标

建立 Hive 表,关联 HBase 表,插入数据到 Hive 表的同时能够影响 HBase表。


步骤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");

在 Hive 和 HBase 中分别查看是否生成了相应的表.

步骤2: 建立临时表

建立中间表, 并向中间表插入数据. 从中间表查询数据,并把数据插入到hive_hbase_emp_table表中

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 '/opt/module/datas/emp.txt' into table emp_tmp;

步骤3: 在 Hive 中向 hive_hbase_emp_table插入数据

insert into table hive_hbase_emp_table select * from emp_tmp;

步骤4: 查看数据是否同步

Hive :

select * from hive_hbase_emp_table;

HBase:

scan 'hbase_emp_table'

2.3 案例2

案例2以案例1为基础. 先完成案例1才能实现案例2

目标:

在 HBase 中已经存储了某一张表 hbase_emp_table,然后在 Hive 中创建一个外部表来关联 HBase 中的 hbase_emp_table 这张表,使之可以借助 Hive 来分析 HBase 这张表中的数据。


步骤1: 在 Hive 中创建表, 并关联 HBase 中的 hbase_emp_table 表

CREATE EXTERNAL TABLE relevance_hbase_emp(
    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");

步骤2: 关联后就可以使用Hive函数进行一些分析操作了

select count(*) from relevance_hbase_emp;

猜你喜欢

转载自blog.csdn.net/qq_43193797/article/details/87158208