ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口;目前elastic search已经可以很好的与hive数据库进行配合操作,将hive中的数据在elasticsearch中创建对应的索引,方便用户用restful api接口查询数仓hive中的数据,且查询的效率是秒级别的;以下是对如何将hive中的表在elastic search上创建索引做出详细的介绍
(1)在elasticsearch上创建索引;
我们这里可以采用curl的方式来处理,当然更好的方式是采用restful api的接口形式来进行处理,这里展示的是采用postman进行索引的创建操作;需要注意的是,我们采用的elasticsearch的版本是5.4.2,对于其他版本的可能不太适应;
这里我们采用put 请求创建索引
put请求在postman下面采用原生的json数据传递要创建索引的信息,以下是创建原生索引的json信息:
{
"mappings": {
"index_name": {
"properties": {
"tgyh_ptf_id": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
},
"ptf_id": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
}
}
index_name对应所填写的索引的名称,和put请求的index_name一致
然后,properties下面对应要创建索引的每列信息,相当于对应于hive表中的每列信息;
(2)在hive上创建外部的关联表
add jar /usr/hdp/2.5.0.0-1245/hive/lib/org.apache.commons.httpclient_3.1.0.v201012070820.jar;
add jar /usr/hdp/2.5.0.0-1245/hive/lib/elasticsearch-hadoop-6.1.0.jar;
CREATE EXTERNAL TABLE
es.index_name
(
ptf_id STRING,
tgyh_ptf_id STRING
)STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler' TBLPROPERTIES (
'es.resource' = 'index_name/index_name',
'es.index.auto.create' = 'true',
'es.nodes' = 'IP:9200'
);
在hive中创建外部的关联的表;
其中index_name和索引的名称是一致的;
(3)hive数据插入到elasticsearch中:
insert into es.index_name
select ptf_id
,tgyh_ptf_id
from xxxx.index_name;
将对应的表中数据插入到index_name索引当中,这里直接采用hivesql进行数据插入;
注:postman传递的创建json格式的数据有可能缺少括号,因为原始的数据进行了修改的缘故