apache-spark - 如何保护 Spark 中的密码和用户名(例如 JDBC 连接/访问 RDBMS 数据库)?

标签 apache-spark apache-spark-sql

我们有一个用例,需要将数据从 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/

相关文章:

scala - 比较 RDD 的子集

scala - Spark Scala 中从字符串到日期的转换

maven - 将 Spark 源代码导入 intellij,构建错误 : not found: type SparkFlumeProtocol and EventBatch

apache-spark - Spark 数据帧同一列上的多个聚合操作

scala - 如何从特定列中具有最大值的数据框中获取行?

python - 根据spark中的移动和将批号添加到DataFrame

python - 如何使用 Python Dataframe API 在 Apache Spark 中找到中位数?

caching - 如何在 Spark SQL 中缓存和持久化临时表?

scala - Spark 2.0 中访问向量列时出现 MatchError

sql - 如何在 Spark SQL 中为posexplode 列指定别名?