1. 程式人生 > >hive-2.1.1整合hbase-2.1.0

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、執行結果