apache-spark - 如何在集群上运行 spark 作业时传递外部资源 yml /property 文件?
问题描述
我正在使用 spark-sql 2.4.1 版本、jackson jars 和 Java 8。
在我的 spark 程序/作业中,我正在从外部“conditions.yml”文件中读取一些配置/属性,该文件位于我的 Java 项目的“resource”文件夹中,如下所示
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
driverConfig = mapper.readValue(
Configuration.class.getClassLoader().getResourceAsStream("conditions.yml"),Configuration.class);
}
如果我想在提交 spark-job 时从外部传递“conditions.yml”文件,如何传递这个文件?它应该放在哪里?
在我的程序中,我正在从“资源”目录读取,即 .getResourceAsStream("conditions.yml") ...如果我从 spark-submit 传递此属性文件 ...该作业将从资源或外部路径中获取吗?
如果我想作为外部文件传递,我需要更改上面的代码吗?
更新的问题:
在我的火花驱动程序中,我正在将属性文件作为程序参数读取,其加载如下
Config props = ConfigFactory.parseFile(new File(args[0]));
在 shell 脚本中运行我的 spark 作业时,我给出如下
$SPARK_HOME/bin/spark-submit \
--master yarn \
--deploy-mode cluster \
--name MyDriver \
--jars "/local/jars/*.jar" \
--files hdfs://files/application-cloud-dev.properties,hdfs://files/condition.yml \
--class com.sp.MyDriver \
--executor-cores 3 \
--executor-memory 9g \
--num-executors 5 \
--driver-cores 2 \
--driver-memory 4g \
--driver-java-options -Dconfig.file=./application-cloud-dev.properties \
--conf spark.executor.extraJavaOptions=-Dconfig.file=./application-cloud-dev.properties \
--conf spark.driver.extraClassPath=. \
--driver-class-path . \
ca-datamigration-0.0.1.jar application-cloud-dev.properties condition.yml
错误 :
没有加载属性...这里有什么问题?将 Program Args 传递给 Spark-Job Java 程序的正确方法是什么?
解决方案
您必须在 spark-submit 命令中使用 --file 文件路径才能传递任何文件。请注意这是
语法是
"--file /home/user/config/my-file.yml"
如果它在 hdfs 上,则提供 hdfs 路径
这应该将文件复制到类路径,并且您的代码应该能够从驱动程序中找到它。
读取文件的实现应该是这样的
def readProperties(propertiesPath: String) = {
val url = getClass.getResource("/" + propertiesPath)
assert(url != null, s"Could not create URL to read $propertiesPath properties file")
val source = Source.fromURL(url)
val properties = new Properties
properties.load(source.bufferedReader)
properties
}
希望这就是你要找的
推荐阅读
- javascript - React:ComponentDidMount 中的 SetInterval 导致错误“警告:无法对未安装的组件执行 React 状态更新。”
- sql - 计算两个(或更多)不同登录周期之间的事件数
- php - 无法写入 json 文件
- python - 使用 pytest 发送电子邮件
- c - C编程 - 打印带有退格字符的字符串的问题
- python - Python:验证输入到列表中
- java - Android App Recycler 视图显示用户拇指和用户名的问题
- sql - 如何在 VIEW 中使用 WHERE?
- go - 使用来自 BSON 的 oneof 反序列化 proto 失败
- python - 用正则表达式替换重复出现的子字符串?