Spark 读取外部配置文件(各种提交模式、使用 typesafe.config)

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 模式(我尝试时也没注意用的什么提交模式),了解的朋友可以告知一下


3.参考

https://www.it1352.com/1848294.html

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值