hive-2.1.1整合hbase-2.1.0

一、环境准备

1、软件版本

  • hadoop-2.7.4
  • hbase-2.1.0
  • hive-2.1.1

2、hbase与hive的版本兼容

  • hive0.90 与 hbase0.92是兼容的,早期的hive版本与hbase0.89/0.90兼容
  • hive1.x 与 hbase0.98.x或更低版本是兼容的
  • hive2.x 与 hbase1.x及hbase1.x更高版本兼容

提示:hive 1.x 与 hbase 1.x整合时,需要自己编译

二、hive与hbase整合环境配置

1、修改hive-site.xml文件,添加配置属性(zookeeper的地址)

  <property>
         <name>hive.zookeeper.quorum</name>
         <value>node01,node02,node03</value>
  </property>
  <property>
        <name>hive.server2.enable.doAs</name>
        <value>false</value>
        <description>
      Setting this property to true will have HiveServer2 execute
      Hive operations as the user making the calls to it.
        </description>
  </property>

注意:hive.server2.enable.doAs必须配置,否则无法使用官方推荐的hiveserver2 / beeline的方式操作,在利用HQL语句创建HBase时会出现异常,解决方案:https://blog.csdn.net/jinYwuM/article/details/83506749

2、拷贝hive的jar包

由于hive与hbase通信主要是依靠hive_hbase-handler.jar工具包,因此需要将hive中的jar包拷贝到$HBASE_HOME/lib下

cp hive-hbase-handler-2.1.1.jar /export/servers/hbase-2.1.0/lib/

 三、整合后功能测试

采用hiveserver2 / beeline的方式启动

1、首先启动Hbase集群

 

2、启动hive

 

 3、利用HQL语句创建hbase 表

创建Hbase表的语法介绍

# Hive中的表名test_tb
CREATE TABLE test_tb(key int, value string) 
# 指定存储处理器
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
# 声明列族,列名
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val") 
# hbase.table.name声明HBase表名,为可选属性默认与Hive的表名相同
# hbase.mapred.output.outputtable指定插入数据时写入的表,如果以后需要往该表插入数据就需要指定该值
TBLPROPERTIES ("hbase.table.name" = "test_tb", "hbase.mapred.output.outputtable" = "test_tb");  

4、插入数据并查询

put 'test_tb','98','cf1:val','val_98'
put 'test_tb','99','cf1:val','val_99'
put 'test_tb','100','cf1:val','val_100'

 5、查询Hive及Hbase表中数据

1)、HBase表数据

2)、Hive表数据

提示:还有一种hive与hbase映射方式,Hive映射HBase中已经存在的表,即使用HQL创建hive外部表,映射HBase已存在的表,进行关联操作。

四、使用java连接hive操作hbase

1、依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-metastore</artifactId>
            <version>2.1.0</version>
        </dependency>

    </dependencies>

2、Java代码

package com.theone.hive;

import java.sql.*;

public class Hive_Hbase {

    private static String Driver = "org.apache.hive.jdbc.HiveDriver";
    private static String URL = "jdbc:hive2://192.168.17.101:10000/hivedb";
    private static String name = "root";
    private static String password = "123";

    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(Driver);
            Connection connection = DriverManager.getConnection(URL, name, password);
            Statement statement = connection.createStatement();
            String sql = "select * from test_tb";
            ResultSet res = statement.executeQuery(sql);
            while (res.next()) {
                System.out.println(res.getInt(1) + "\t==> " + res.getString(2));
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

3、运行结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值