【spark】spark使用sql读取elasticsearch es索引,使用keystore配置用户密码

发布于:2024-04-23 ⋅ 阅读:(16) ⋅ 点赞:(0)

参考文章

spark配置elasticsearch属性汇总(基于es7)
es-offical-doc
Spark多方案读取Es性能比较
Spark读写ES数据时遇到的问题总结
es 查询多个索引的文档

spark table中使用明文密码

set es.index.auto.create=true
drop table if exists default.test_es01;
create table default.test_es01(
id STRING,
name STRING,
es_metadata map<STRING,STRING>
) USING org.elasticsearch.spark.sql
options (
es.resource='es索引名称-可以*作为通配符',
es.nodes='demo.test.com',
es.port='9200',
es.index.auto.create='true',
es.net.http.auth.user='用户名',
es.net.http.auth.pass='密码',
es.read.metadata='true',
es.read.metadata.field='es_metadata'
);

select * from default.test_es01 limit 10;

使用keystore存储用户密码

keystore生成

es-hadoop官方文档
Only the following configurations can be read from the secure settings: * es.net.http.auth.pass * es.net.ssl.keystore.pass * es.net.ssl.truststore.pass * es.net.proxy.http.pass * es.net.proxy.https.pass * es.net.proxy.socks.pass
所以es.net.http.auth.user添加至keystore也不会被识别。

export ES_SPARK_JAR=/usr/xxx/spark2/jars/elasticsearch-spark-20_2.11-8.13.2.jar
export ES_KEYTOOL_CLASSPATH=org.elasticsearch.hadoop.cli.Keytool
# 查看帮助文档
java -classpath $ES_SPARK_JAR $ES_KEYTOOL_CLASSPATH -h
# 生成空的esh.keystore文件
java -classpath $ES_SPARK_JAR $ES_KEYTOOL_CLASSPATH create

# 向esh.keystore文件添加kv对,无需指定文件名称使用默认文件名esh.keystore
java -classpath $ES_SPARK_JAR $ES_KEYTOOL_CLASSPATH add es.net.http.auth.pass
# 输入:密码

# 确认命令,执行后打印存储的键值对:
java -classpath $ES_SPARK_JAR $ES_KEYTOOL_CLASSPATH list
# 打印如下内容:
# es.net.http.auth.pass

# 如下命令可以移除密码
java -classpath $ES_SPARK_JAR $ES_KEYTOOL_CLASSPATH remove es.net.http.auth.user

esh.keystore可以修改名称,分发至集群各个节点。
注意:Once your settings are all specified, you must make sure that the keystore is available on every node. 每个spark节点都需要方keystore文件,即使测试也必须分发到各个节点,因为其只有运行才会检查此文件此文件,而spark实际运行节点是不定的。

use hudi_db;
drop table if exists default.test_es02;
create table default.test_es02(
id STRING,
name STRING,
es_metadata map<STRING,STRING>
) USING org.elasticsearch.spark.sql
options (
es.resource='es索引名称-可以*作为通配符',
es.nodes='demo.test.com',
es.port='9200',
es.index.auto.create='true',
es.net.http.auth.user='用户名',
es.keystore.location='file:///home/***/修改后的名称.keystore',
es.read.metadata='true',
es.read.metadata.field='es_metadata'
);

select count(1) sl from default.test_es02;