解决ES-Hive在开启Xpack下的数据互通问题

公共步骤

环境信息

  • Elasticsearch 7.3.x
  • Hadoop 3.1.x
  • Hive 3.1.x
  • CentOS 7.4 (x86-64)

Hive的Add jar

先将两个依赖包elasticsearch-hadoop-7.3.3.jarcommons-httpclient-3.1.jar上传至HDFS某个路径。第一个jar是核心jar,第二个jar是运行时有HttpClient相关报错才添加的。

后台启动hive命令行之后,需要将es-hadoop的两个相关jar添加到hive依赖中,才能正常操作。

否则不能建表,不能删除,不能查询更不能写入数据!

add jar hdfs://manager80.bigdata:8020/user/es/hadoop/elasticsearch-hadoop-7.3.3.jar;
add jar hdfs://manager80.bigdata:8020/user/es/hadoop/commons-httpclient-3.1.jar;

非Xpack模式

ES裸奔(没有https也没有用户密码),这种模式在网络上资料比较多,也非常容易成功复现,但在生产环境没什么价值。

创建ES外部表的步骤示例:

CREATE EXTERNAL TABLE elastic_test_table(
id bigint,
name string,
length decimal(4,2),
birth_day string,
create_time string
)
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES(
'es.index.auto.create'='true',
'es.index.read.missing.as.empty'='true',
'es.mapping.id'='id',
'es.resource'='es_test_table/_doc',
'es.nodes'='manager80.bigdata',
'es.port'='9200',
'es.nodes.wan.only'='true',
'es.nodes.discovery' = 'false',
'es.read.metadata'='true'
)

## 然后写入几条测试数据
insert into elastic_test_table values(11,"zhangsan",20.01,"20230108","20230206");
insert into elastic_test_table values(21,"lisi",20.02,"20230107","20230206");
insert into elastic_test_table values(31,"wangwu",20.03,"20230106","20230206");
insert into elastic_test_table values(41,"laoliu",20.06,"20230105","20230205");

写入成功后,前往ES查看对应的索引es_test_table已经自动创建,且已经写入了上述4条数据,这就算冒烟测试ok了。

Xpack模式

ES-Hadoop官方建议同时开启ES的https和密码,以保障传输层安全和用户授权访问。但官网没有给出具体的示例,踩了不少坑浪费了时间。其他博客有只写es的用户名和密码的,这种写法跑不通。根据以往经验这还需要truststore及其密码。

问题是hive classpath里找不到证书文件,相关报错如下:

Caused by: org.elasticsearch.hadoop.rest.EsHadoopNoNodesLeftException: Connection error (check network and/or proxy settings)- all nodes failed; tried [[https://manager80.bigdata:9200, https://worker82.bigdata:9200, https://master81.bigdata:9200]]
        at org.elasticsearch.hadoop.rest.NetworkClient.execute(NetworkClient.java:152) ~[?:?]
        at org.elasticsearch.hadoop.rest.RestClient.execute(RestClient.java:424) ~[?:?]
ERROR : Failed to execute tez graph.
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'

Caused by: org.elasticsearch.hadoop.EsHadoopIllegalArgumentException: Expected to find keystore file at [file:///opt/test1.p12] but was unable to. Make sure that it is available on the classpath, or if not, that you have specified a valid URI

上述报错是摘自各种es参数组合尝试十几次的内容,顺序不一定对,关键词就是Expected to find keystore file at [file:///opt/test1.p12] but was unable to. Make sure that it is available on the classpath, or if not, that you have specified a valid URI

classpath找不到证书文件,因之前有add jar都是把文件上传的hdfs的,我也把p12证书传到hdfs上然后add file,但是仍然报这个错误(很显然,一个是add jar一个是add file,名副其实,但错误依旧)。

可以看到报错内容是我尝试从本地文件系统(file://)加载证书文件,报错不变。这儿就是大坑,困扰了我两天。此处我分析了相关源码,没有什么特别之处,认证这里就是从给定的URI使用IO流读取。

注意:其中有个转机是我把证书文件放到了HADOOP_CLASSPATH里,昙花一现成功了一次,后续这样操作也还是错。

最终解决路径

  1. 把p12证书文件放到HiveServer2节点上,如/opt/hive/es/test1.p12

  2. 后台启动hive(beeline)之后,正常先add两个jar,再add上述file,也就是add file file:///opt/hive/es/test1.p12

  3. 创建ES外部表,注意增加了5个参数,分别是ssl开关、用户名、密码、trust证书位置和证书密码!缺一不可!此外注意es.nodes全部添加了"https",相当于6处改动,都要拾掇好了。

    CREATE EXTERNAL TABLE elastic_test_xpack(
    id bigint,
    name string,
    height int,
    birth_day string,
    create_time string
    )
    STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
    TBLPROPERTIES(
    'es.index.auto.create'='true',
    'es.index.read.missing.as.empty'='true',
    'es.mapping.id'='id',
    'es.resource'='es_test_xpack/_doc',
    'es.net.ssl'='true',
    'es.net.http.auth.pass'='elastic111',
    'es.net.http.auth.user'='elastic',
    'es.net.ssl.truststore.location'='test1.p12',
    'es.net.ssl.truststore.pass'='bigdata666',
    'es.nodes'='https://manager25.bigdata,https://master26.bigdata,https://worker27.bigdata',
    'es.port'='9200',
    'es.nodes.wan.only'='true',
    'es.nodes.discovery' = 'false',
    'es.read.metadata'='true'
    );
    
    
    insert into elastic_test_xpack values(11,"zhangsan",180,"20230108","20230206");
    
  4. 然后写入数据(insert …)

  5. 成功后到Kibana里查询hive数据已经同步到ES了。

分享以上。

补充说明

  1. 密码似乎不可以带特殊符号,我这里仅使用小写英文和数字。
  2. 理论上证书文件只要在特定的classpath里就行,如jdk或者hive的,前面也提到在hadoop的classpath中有过转机(hive启动时附加了该路径)。
  3. 使用hive本身查询时数据转换有异常,另论。
  4. 证书文件放在hdfs理应也是可以的,最早尝试时有报错。在本地文件成功之后我再次尝试hdfs://这种也是成功的,这样add jaradd file就统一起来了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024点线面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值