使用hive向elasticsearch中寫數據

在某些場景下,咱們須要直接將hive中的數據直接推到elasticsearch。下面我來介紹如何實現。首先你要肯定本身的elasticsearch的版本,版主的是elasticsearch-2.4.1 因此要用2.4.1以上的jar包:elasticsearch-hadoop-2.4.3.jar,這個jar包可在maven庫中查到http://mvnrepository.com/
準備工做作好咱們就開始實現了。第一步,建立你想要的索引curl -XPOST "http://192.168.4.217:9200/test_tag"
第二步,建立hive外部表:node

hive -e"
add jar hdfs:/opt/data/jar/elasticsearch-hadoop-2.4.3.jar;
add jar hdfs:/opt/data/jar/org.apache.commons.httpclient_3.1.0.v201012070820.jar;
--org.apache.commons.httpclient_3.1.0.v201012070820.jar這個jar包通常是不用加的,除非在報httpclient相關錯誤的時候才加

use ods;
create external table if not exists app.test_tag(
complaint_id string,
tag_name string,
classification string,
sub_classification string
) 
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler' 
TBLPROPERTIES('es.nodes'='192.168.4.217','es.resource' = 'test_tag/defect_recommend_tag');
--es.resource斜線前面爲索引,斜線後面爲mapping名稱。mapping能夠不用事先建好,當數據插入時會自動根據數據類型建立mapping。

insert overwrite table app.test_tag
select complaint_id,tag_name,classification,sub_classification from app.src_test_tag;"

程序執行完畢,就能夠在es裏看到數據了。apache

相關文章
相關標籤/搜索