我们有一个用例,需要将数据从 HDFS 导出到 RDBMS。我看到了这个example 。他们在这里将用户名和密码存储在代码中。有没有办法在导出数据时隐藏密码,就像我们在 Sqoop 中可以选择密码别名一样。
最佳答案
设置密码
在命令行中作为纯文本 Spark 配置:
spark-submit --conf spark.jdbc.password=test_pass ...
使用环境变量:
export jdbc_password=test_pass_export
spark-submit --conf spark.jdbc.password=$jdbc_password ...
使用 Spark 配置属性文件:
echo "spark.jdbc.b64password=test_pass_prop" > credentials.properties
spark-submit --properties-file credentials.properties
使用base64编码来“混淆”:
echo "spark.jdbc.b64password=$(echo -n test_pass_prop | base64)" > credentials_b64.properties
spark-submit --properties-file credentials_b64.properties
在代码中使用密码
import java.util.Base64 // for base64
import java.nio.charset.StandardCharsets // for base64
val properties = new java.util.Properties()
properties.put("driver", "com.mysql.jdbc.Driver")
properties.put("url", "jdbc:mysql://mysql-host:3306")
properties.put("user", "test_user")
val password = new String(Base64.getDecoder().decode(spark.conf.get("spark.jdbc.b64password")), StandardCharsets.UTF_8)
properties.put("password", password)
val models = spark.read.jdbc(properties.get("url").toString, "ml_models", properties)
编辑:--conf 和 --properties-file 的 Spark 命令行界面帮助文档:
--conf PROP=VALUE Arbitrary Spark configuration property.
--properties-file FILE Path to a file from which to load extra properties. If not
specified, this will look for conf/spark-defaults.conf.
属性文件名是任意的。
关于apache-spark - 如何保护 Spark 中的密码和用户名(例如 JDBC 连接/访问 RDBMS 数据库)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43336383/