hive 数据连接 Elasticsearch 外连接
目的:将hive 中的数据,插入es 索引
第一步,将 elasticsearch-hadoop-6.8.1.zip 下载放到 /opt/cloudera/parcels/CDH/lib/hive/lib/ 下,下载地址:https://www.elastic.co/cn/downloads/hadoop
解压 unzip elasticsearch-hadoop-6.8.1.zip 生成 elasticsearch-hadoop-6.8.1 目录
第二步:在hive shell 中 增加 jar 会话session 级别
add jar /opt/cloudera/parcels/CDH/lib/hive/lib/elasticsearch-hadoop-6.8.1/dist/elasticsearch-hadoop-6.8.1.jar;
第三步:创建和es 字段相对应的hive外部表,和es关联
CREATE EXTERNAL TABLE dc_external.test_es(
`id` int,
`industry` int,
`industry_name` string,
`industry_one` int,
`industry_three` int,
`lic_no_end_date` string,
`lic_no_start_date` string
)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES(
'es.nodes' = '192.168.135.100:9200,192.168.135.101:9200,192.168.135.102:9200',
'es.nodes.wan.only'='true',
'es.net.http.auth.user'='es',
'es.net.http.auth.pass'='ABCD',
'es.index.auto.create' = 'true',
'es.resource' = 'test/_doc',
'es.mapping.id' = 'id',
'es.write.operation'='upsert'
);
es 参数意义:
es.nodes :es 节点列表
es.net.http.auth.user :es 用户名 (没有的可以不写)
es.net.http.auth.pass:es 密码 (没有的可以不写)
es.index.auto.create:true/flase 索引是否自动创建 (设置为false,索引提前创建好)
es.resource:es名称/类型
es.mapping.id:hive 用哪个字段 当做 es document id 的值 (可以不写)
es.write.operation:写入es的类型 upsert 执行插入或者更新操作(如果id存在)
还有一些配置根据自己需要去官网取 https://www.elastic.co/guide/en/elasticsearch/hadoop/current/configuration.html
第四步:插入 数据
#关闭hive 推测机制
SET hive.mapred.reduce.tasks.speculative.execution = false;
SET mapreduce.map.speculative = false;
SET mapreduce.reduce.speculative = false;
insert overwrite table dc_external.test_es
select
id,
industry,
industry_name,
industry_one,
industry_three,
(case when lic_no_end_date != '' then lic_no_end_date else null end) as lic_no_end_date,
(case when lic_no_start_date != '' then lic_no_start_date else null end) as lic_no_start_date
from dw_dwd.tset;
查询es数据OK.
期间遇见问题:
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org.elasticsearch.hadoop.EsHadoopIllegalArgumentException: Cannot detect ES version - typically this happens if the network/Elasticsearch cluster is not accessible or when targeting a WAN/Cloud instance without the proper setting 'es.nodes.wan.only'
原因:/opt/cloudera/parcels/CDH/lib/hive/lib/ 目录下,还有之前的一个 elasticsearch-hadoop-5.6.1 的老版本依赖。移动到别的目录,重新执行,OK.
这里这个时间字段,在hive中存储的是string 类型,但是es中需要是date类型,并且hive中的一些数据类型并没有完全和es中的类型对应上,所以插入完后,需要根据正确的es模板中的类型,把当前数据reindex,数据类型更正。如果时间字段为 ‘’ 字符串那么,es 进行reindex 时会报类型转换错误。所以换为了 null 没问题。