java连接数据库账户密码,如何保护Spark中的密码和用户名(例如用于JDBC连接/访问RDBMS数据库)?...

We have a use case where we need to export data from HDFS to a RDBMS. I saw this example . Here they have store the username and password in the code. Is there any way to hide the password while export the data like we have the option of password-alias in Sqoop.

解决方案

Setting the password

At the command line as a plaintext spark config:

spark-submit --conf spark.jdbc.password=test_pass ...

Using environment variable:

export jdbc_password=test_pass_export

spark-submit --conf spark.jdbc.password=$jdbc_password ...

Using spark config properties file:

echo "spark.jdbc.b64password=test_pass_prop" > credentials.properties

spark-submit --properties-file credentials.properties

With base64 encoding to "obfuscate":

echo "spark.jdbc.b64password=$(echo -n test_pass_prop | base64)" > credentials_b64.properties

spark-submit --properties-file credentials_b64.properties

Using the password in code

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)

Edit: spark command line interface help docs for --conf and --properties-file:

--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.

The properties-file name is arbitrary.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值