1. 程式人生 > >Hive2.3.2整合HBase2的環境配置以及測試

Hive2.3.2整合HBase2的環境配置以及測試

Hive與HBase整合的實現是利用兩者本身對外的API介面互相通訊來完成的,其具體工作交由Hive的lib目錄中的hive-hbase-handler-*.jar工具類來實現,通訊原理如下圖所示。


Hive整合HBase後的使用場景:

(一)通過Hive把資料載入到HBase中,資料來源可以是檔案也可以是Hive中的表。

(二)通過整合,讓HBase支援JOIN、GROUP等SQL查詢語法。

(三)通過整合,不僅可完成HBase的資料實時查詢,也可以使用Hive查詢HBase中的資料完成複雜的資料分析。

Hive內建服務

Hive自帶了許多服務,可在執行時通過service選項來明確指定使用什麼服務,或通過--service help來檢視幫助。下面介紹最常用的一些服務。

(1)CLI:這是Hive的命令列介面,用的比較多。這是預設的服務,直接可以在命令列裡面使用。

(2)hiveserver:這個可以讓Hive以提供Trift服務的伺服器形式來執行,可以允許許多不同語言編寫的客戶端進行通訊。可以通過設定HIVE_PORT環境變數來設定伺服器所監聽的埠號,在預設的情況下,埠為 10000。最新版本(hive1.2.1)用hiveserver2取代了原有的hiveserver。

(3)hwi:它是Hive的Web介面,是hive cli的一個web替換方案。

(4)jar:與Hadoop jar等價的Hive介面,這是執行類路徑中同時包含Hadoop和Hive類的Java應用程式的簡便方式。

(5)Metastore:用於連線元資料庫(如mysql)。在預設情況下,Metastore和Hive服務執行在同一個程序中,埠號為9083。使用這個服務,可以讓Metastore作為一個單獨的程序執行,我們可以通過METASTORE_PORT來指定監聽的埠號。

Hive對映HBase表

 1. 如果hbase是叢集,需要修改hive-site.xml檔案配置
 <property>
    <name>hive.zookeeper.quorum</name>
    <value>10.116.33.109,10.27.185.72,10.25.203.67 </value>
    <description>
      List of ZooKeeper servers to talk to. This is needed for:
      1. Read/write locks - when hive.lock.manager is set to
      org.apache.hadoop.hive.ql.lockmgr.zookeeper.ZooKeeperHiveLockManager,
      2. When HiveServer2 supports service discovery via Zookeeper.
      3. For delegation token storage if zookeeper store is used, if
      hive.cluster.delegation.token.store.zookeeper.connectString is not set
      4. LLAP daemon registry service
    </description>
  </property>

2.將hbase lib目錄下的所有檔案複製到hive lib目錄中 注:先刪除hive/lib目錄下hbase開頭的jar包,否則會出現後面的錯誤。

cd /data/spark/hbase-2.0.0-alpha4/lib
cp * /data/spark/apache-hive-2.3.2-bin/lib


3.在hive中建立對映表
$ hive shell
> create table hive_hbase_test(key int,value string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping"=":key,cf1:val") tblproperties("hbase.table.name"="hive_hbase_test");


4. 在hbase中檢視是否存在對映表
$ hbase shell
> list

Hive Hbase測試

1.Hive 匯入資料,Hbase檢視資料

vim test.txt

1      zhangsan
2      lisi
3      wangwu
4 老司機

資料列以tab 分割


Hive 建立表,並加裝外部資料

create table test(key int,value string) row format delimited fields terminated by '\t';
load data local inpath'/data/spark/apache-hive-2.3.2-bin/test.txt' overwrite into table test;
select * from test;


將hive的test表中的資料載入到hive_hbase_test表

insert overwrite table hive_hbase_test select * from test;
select * from hive_hbase_test;

測試伺服器記憶體不夠,最後匯入沒有成功。


2.通過Hbase put新增資料,Hive檢視新增資料。

put 'hive_hbase_test','100','cf1:val','laosiji'

Hive 檢視

select * from hive_hbase_test;

說明Hive和Hbase資料完全一致,類似資料庫中的表和檢視關係,如果是外部表,二者之間資料則不是關聯的。

通過結合,可以設計一個高速寫入,後面接入實時分析的海量資料分析系統。

這種組合可以有多種解決方案,Kafka+Spark/jStorm


建立外部表

create external table hive_hbase_test(key int,value string)stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping"=":key,cf1:val") tblproperties("hbase.table.name"="hive_hbase_test");


未刪除hive/lib目錄下的jar

報錯
Caused by: java.lang.NoSuchMethodError: org.apache.hadoop.hbase.HBaseConfiguration.createClusterConf(Lorg/apache/hadoop/conf/Configuration;Ljava/lang/String;Ljava/lang/String;)Lorg/apache/hadoop/conf/Configuration;
at org.apache.hadoop.hbase.mapreduce.TableOutputFormat.setConf(TableOutputFormat.java:226)
at org.apache.hive.common.util.ReflectionUtil.setConf(ReflectionUtil.java:101)
at org.apache.hive.common.util.ReflectionUtil.newInstance(ReflectionUtil.java:87)
at org.apache.hadoop.hive.ql.io.HiveFileFormatUtils.getHiveOutputFormat(HiveFileFormatUtils.java:302)
at org.apache.hadoop.hive.ql.io.HiveFileFormatUtils.getHiveOutputFormat(HiveFileFormatUtils.java:292)
at org.apache.hadoop.hive.ql.exec.FileSinkOperator.createHiveOutputFormat(FileSinkOperator.java:1168)
... 38 more

記憶體空間不夠報錯:

Diagnostic Messages for this Task:
Error: org.apache.hadoop.hbase.client.HTable.<init>(Lorg/apache/hadoop/conf/Configuration;Ljava/lang/String;)V


2018-04-02 17:24:09,498 INFO  [ca66ff2f-3431-4765-b550-28f7ed1fb9ae main] impl.YarnClientImpl: Killed application application_1522660651503_0001
FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
MapReduce Jobs Launched: 
2018-04-02 17:24:09,512 WARN  [ca66ff2f-3431-4765-b550-28f7ed1fb9ae main] mapreduce.Counters: Group FileSystemCounters is deprecated. Use org.apache.hadoop.mapreduce.FileSystemCounter instead
Stage-Stage-3: Map: 1   HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec


參考:

Hive安裝及與HBase的整合

大資料學習系列之五 ----- Hive整合HBase圖文詳解

hiveserver2和zookeeper的HA搭建