【原创】大叔经验分享(26)hive经过外部表读写elasticsearch数据

hive经过外部表读写elasticsearch数据,和读写hbase数据差很少,差异是须要下载elasticsearch-hadoop-hive-6.6.2.jar,而后使用其中的EsStorageHandler;html

Connect the massive data storage and deep processing power of Hadoop with the real-time search and analytics of Elasticsearch. The Elasticsearch-Hadoop (ES-Hadoop) connector lets you get quick insight from your big data and makes working in the Hadoop ecosystem even better.node

官方:https://www.elastic.co/products/hadoop
下载:https://www.elastic.co/downloads/hadoop服务器

 

目前最新的版本是6.6.2app

# wget https://artifacts.elastic.co/downloads/elasticsearch-hadoop/elasticsearch-hadoop-6.6.2.zip
# unzip elasticsearch-hadoop-6.6.2.zipelasticsearch

使用其中的elasticsearch-hadoop-6.6.2/dist/elasticsearch-hadoop-hive-6.6.2.jaride

add jar /path/to/elasticsearch-hadoop-hive-6.6.2.jar;oop

CREATE EXTERNAL TABLE hive_elasticsearch_table (
id string,
name string,
desc string
)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES(
'es.nodes' = '$es_server1:9200,$es_server2:9200',
'es.index.auto.create' = 'false',
'es.resource' = 'testdoc/testtype',
'es.read.metadata' = 'true',
'es.mapping.names' = 'id:_metadata._id, name:name, desc:desc');ui

 主要是配置es.nodes、es.resource和es.mapping.names,一个是es服务器地址,一个是index名和type名,一个是hive字段和es字段的一一映射,而后就能够在hive中读写es数据:server

select * from hive_elasticsearch_table limit 10;
insert into table hive_elasticsearch_table select '2', 'testname', 'testdesc';htm

可是这样发现id是被hash过的

+------------------------------+--------------------------------+--------------------------------+--+
| hive_elasticsearch_table.id | hive_elasticsearch_table.name | hive_elasticsearch_table.desc |
+------------------------------+--------------------------------+--------------------------------+--+
| 6mpoc2gBohlnD12tvBoF | testname | testdesc |
+------------------------------+--------------------------------+--------------------------------+--+

还须要再加一个es.mapping.id,定义哪一个字段是document的id

CREATE EXTERNAL TABLE hive_elasticsearch_table (
id string,
name string,
desc string
)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES(
'es.nodes' = '$es_server1:9200,$es_server2:9200',
'es.index.auto.create' = 'false',
'es.resource' = 'testdoc/testtype',
'es.read.metadata' = 'true',
'es.mapping.id' = 'id',
'es.mapping.names' = 'id:_metadata._id, name:name, desc:desc');

此次正常了

+------------------------------+--------------------------------+--------------------------------+--+
| hive_elasticsearch_table.id | hive_elasticsearch_table.name | hive_elasticsearch_table.desc |
+------------------------------+--------------------------------+--------------------------------+--+
| 6mpoc2gBohlnD12tvBoF | testname | testdesc |
| 4 | hello | world |
+------------------------------+--------------------------------+--------------------------------+--+

 

关于字段类型映射,详见:https://www.elastic.co/guide/en/elasticsearch/hadoop/current/mapping.html 

相关文章
相关标签/搜索