1.使用 typesafe.config 读取外部配置文件
使用 com.typesafe.config 的方式
STEP1.导入 typesafe 依赖
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>RELEASE</version>
</dependency>
STEP2.创建配置文件 application.conf
# 已维护表
maintainedTbls = dh.maintained_tables
# 各层库名
dlDb = dl
dhDb = dh
dmDb = dm
STEP3.代码中加载 application.conf
在本地加载配置文件
# 默认在 resources 目录下
val config: Config = ConfigFactory.load()
# 加载任意位置的配置文件
val config: Config =
ConfigFactory.parseFile(new File("***.conf"))
val prop1 = config.getString("maintainedTbls")
val prop2 = config.getString("dlDb")
val prop3 = config.getString("dhDb")
如果要在 Spark-submit 时使用外部配置文件,则将 application.conf 上传到服务器,并在提交命令中指定,详见 👇
STEP4.Spark-submit
将 application.conf 放到服务器上,路径为 ./conf/application.conf(相对&绝对路径都可)
- yarn cluster 模式
经测试发现 --files 只适用于 yarn cluster 模式
而在 yarn client 和 local 模式中使用 --files 还是会读取 resources 下的配置文件,不会读取指定的外部配置文件
spark2-submit \
--master yarn \
--deploy-mode cluster \
--name ParseVdfTables \
--jars parse-vdf-tables-1.0-SNAPSHOT-jar-with-dependencies.jar \
--class io.bigtimes.TstSparkApp \
--files ./conf/application.conf \
parse-vdf-tables-1.0-SNAPSHOT.jar
- yarn client 和 local[*] 模式
对于这两种提交模式,需要在提交命令中做如下配置
spark2-submit … \
–conf spark.driver.extraJavaOptions=-Dconfig.file=./conf/application.conf \
–conf spark.executor.extraJavaOptions=-Dconfig.file=./conf/application.conf \
…
OR
更推荐这种方式
spark2-submit … \
–conf spark.driver.extraJavaOptions=-Dconfig.file=./conf/application.conf \
–conf spark.executor.extraJavaOptions=-Dconfig.file=./conf/application.conf \
…
完整的提交命令
yarn client:
spark2-submit \
--master yarn \
--deploy-mode client \
--name ParseVdfTables \
--jars parse-vdf-tables-1.0-SNAPSHOT-jar-with-dependencies.jar \
--class io.bigtimes.TstSparkApp \
--conf spark.driver.extraClassPath=./conf \
--conf spark.executor.extraClassPath=./conf \ # if you need to load config at executors
parse-vdf-tables-1.0-SNAPSHOT.jar
OR
# 更推荐这种方式
spark2-submit \
--master yarn \
--deploy-mode client \
--name ParseVdfTables \
--jars parse-vdf-tables-1.0-SNAPSHOT-jar-with-dependencies.jar \
--class io.bigtimes.TstSparkApp \
--conf spark.driver.extraJavaOptions=-Dconfig.file=./conf/application.conf \
--conf spark.executor.extraJavaOptions=-Dconfig.file=./conf/application.conf \
parse-vdf-tables-1.0-SNAPSHOT.jar
local[*]:
spark2-submit \
--master local[*] \
--name ParseVdfTables \
--jars parse-vdf-tables-1.0-SNAPSHOT-jar-with-dependencies.jar \
--class io.bigtimes.TstSparkApp \
--conf spark.driver.extraClassPath=./conf \
--conf spark.executor.extraClassPath=./conf \ # if you need to load config at executors
parse-vdf-tables-1.0-SNAPSHOT.jar
OR
# 更推荐这种方式
spark2-submit \
--master local[*] \
--name ParseVdfTables \
--jars parse-vdf-tables-1.0-SNAPSHOT-jar-with-dependencies.jar \
--class io.bigtimes.TstSparkApp \
--conf spark.driver.extraJavaOptions=-Dconfig.file=./conf/application.conf \
--conf spark.executor.extraJavaOptions=-Dconfig.file=./conf/application.conf \
parse-vdf-tables-1.0-SNAPSHOT.jar
2.网上比较多的方法
在CSDN上看了很多篇博客,大多数介绍的方法是:
Spark程序:
val filePath = "application.properties"
val props = newProperties()
props.load(newFileInputStream(filePath))
提交命令:
--files /../conf/application.properties \
但我尝试时报错找不到配置文件,不知道是不是因为 --files 只适用于 yarn cluster 模式(我尝试时也没注意用的什么提交模式),了解的朋友可以告知一下