首页 > 解决方案 > 如何在集群上运行 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 程序的正确方法是什么?

标签: apache-sparkapache-spark-sqlspark-streamingdatabricksspark-submit

解决方案


您必须在 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
}

希望这就是你要找的


推荐阅读