hive向elasticsearch写数据

一、添加依赖包

首先确定elasticsearch的版本,若es版本为6.1.2,可用jar包:elasticsearch-hadoop-6.1.2.jar,网上有说只要高于6.1.2版本的jar包即可(自行验证)。当报httpclient相关错误时,还需要添加org.apache.commons.httpclient这个jar包。
首先进入hive,然后通过下面命令添加依赖包。

add jar yourPath/elasticsearch-hadoop-6.1.2.jar;

当然还有其它添加方式,具体可查看https://www.elastic.co/guide/en/elasticsearch/hadoop/current/hive.html#hive

二、创建hive外部表:

create external table if not exists es_cmb_test(
ptf_id string,
ptf_name string,
bill_date string,
acc_status string
) 
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler' 
TBLPROPERTIES(
'es.resource' = 'test/es_cmb_test', 
'es.nodes'='192.168.1.1',
'es.port'='9200',
'es.mapping.id' = 'ptf_id',
'es.index.auto.create' = 'true',
'es.write.operation'='upsert');

TBLPROPERTIES后面为设置ES的属性,例如:
通过’es.mapping.id’ = ‘ptf_id’ 指定id。
通过’es.write.operation’=’upsert’ 来执行插入或者更新操作(如果id存在)。
详情可查https://www.elastic.co/guide/en/elasticsearch/hadoop/current/configuration.html

三、插入数据

insert overwrite table es_cmb_test
    select 
        ptf_id,
        ptf_name,
        bill_date,
        acc_status 
    from test.cmb_test;

有人做过实验,得到如下结论:
测试重要结论:
1.elasticsearch字段较多时,可以建立多个hive映射表分别进行写入
2.hive无法删除elasticsearch中的记录,只能插入和更新
3.hive的insert into和insert overwrite操作elasticsearch时结果是一样的

参考

1、https://www.elastic.co/guide/en/elasticsearch/hadoop/current/hive.html
2、http://blog.csdn.net/ltlf_21/article/details/78614970

你可能感兴趣的:(大数据技术)