【问题标题】:How to pass external resouce yml /property file while running spark job on cluster?如何在集群上运行 Spark 作业时传递外部资源 yaml /property 文件?
【发布时间】:2019-10-27 22:22:06
【问题描述】:

我使用的是 spark-sql 2.4.1 版本、jackson jars 和 Java 8。

在我的 spark 程序/作业中,我正在从位于我的 Java 项目的“resource”文件夹中的外部“conditions.yml”文件中读取一些配置/属性,如下所示

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 传递此属性文件 ...该工作将从资源或外部获取路径?

如果我想作为外部文件传递,是否需要更改上面的代码?

更新问题:

在我的 spark 驱动程序中,我将属性文件作为程序参数读取 正在加载如下

 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-spark apache-spark-sql spark-streaming databricks spark-submit


    【解决方案1】:

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

    希望这就是你要找的东西

    【讨论】:

    • 谢谢,现在我在代码中硬编码,从资源文件夹中读取,如果我使用 --file 给出其他路径,驱动程序如何理解它应该从 --文件而不是代码资源文件路径中提到的内容?
    • 当您为其提供文件选项时.. spark 会将其复制到类路径.. 在您的代码中,您应该将其读取为 /yourfilename.yml
    【解决方案2】:

    您可以添加: 规格: 参数:

    • --deploy-mode
    • cluster

    【讨论】:

    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    猜你喜欢
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2015-04-25
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多