1. 程式人生 > >從Hive導入數據到ES

從Hive導入數據到ES

倒排索引 ref com blog eat 數據 oop red stc

大數據方興未艾,Hive在業界,是大數據的標配了。因此hive數據添加到ES的應用場景還是比較常見的。
學習ES官方的es-hadoop, 有從hive導數據到ES. 實驗可行。
hive的版本: hive-1.1.0-cdh5.9.0

具體的步驟如下:
step1 將elasticsearch-hadoop-hive-version.jar添加到hive

wget https://artifacts.elastic.co/downloads/elasticsearch-hadoop/elasticsearch-hadoop-6.3.0.zip
unzip elasticsearch-hadoop-6.3.0.zip
hdfs dfs -mkdir /user/test/es_hadoop/
hdfs dfs -put elasticsearch-hadoop-hive-6.3.0.jar /user/test/es_hadoop/
ADD JAR hdfs://test/user/test/es_hadoop/elasticsearch-hadoop-hive-6.3.0.jar;

step2 創建Hive表:

CREATE EXTERNAL TABLE elastic_table(
   uuid string,
   key1 int,
   key2 int,
   day string
)
STORED BY ‘org.elasticsearch.hadoop.hive.EsStorageHandler‘
TBLPROPERTIES(‘es.resource‘=‘index/type‘,
‘es.nodes‘=‘serverIP:port‘,
‘es.index.auto.create‘=‘TRUE‘,
‘es.mapping.id‘ = ‘uuid‘
);

step3 添加數據

INSERT OVERWRITE TABLE elastc_table
SELECT uuid, key1,key2, day FROM source s;

為了避免客戶端版本的問題,es-hadoop使用es的restfull接口導入數據,該接口使用的是Http協議。

通常使用ES, 首當其沖的問題就是: 如何快速將海量數據導入ES? 由於ES的數據需要建立倒排索引,所以導入數據到ES的瓶頸往往在ES這裏。

本文記錄了將Hive表的數據導入ES的方法。這裏背後隱藏了mapreduce,即集群的威力。 這裏有個系列博客,講述如何最大限度的挖掘ES索引數據的性能,立足點是ES。

https://qbox.io/blog/series/how-to-maximize-elasticsearch-indexing-performance

作者總結有3點:

  1. 根據應用場景創建mapping, 去除不必要的字段,如_all, _source;
    這裏是從應用場景下手,以避免存儲不必要的信息來提升索引數據的性能。

  2. 修改es/lucene默認的設置,比如
    refresh_interval,
    index.number_of_replicas,
    index.merge.scheduler.max_thread_count,
    index.translog.interval,
    indices.memory.index_buffer_size
    index.index_concurrency
    等參數。 這裏是從集群的角度進行調優, 通常用於大批量導入數據到ES。

  3. 如果前面兩種還是沒能解決問題,那就需要對集群進行橫向擴展了,比如增加集群的分片數量。
    集群大了後,各個結點的功能就需要單一化,專註化了。

比如節點只承擔數據相關的任務。

node.master: false
node.data: true
node.ingest: false

bulk api的批量值需要實驗,找到最佳參數。建議bulk的大小在5M~10M.

使用SSD硬盤。索引數據時,副本數設置為0。

參考:
http://note4code.com/2016/06/17/hive-%E5%90%91-elasticsearch-%E5%AF%BC%E5%87%BA%E6%95%B0%E6%8D%AE/

從Hive導入數據到ES