首页 > 解决方案 > spark submit-read 命令行参数,它是使用 Scop 和 ConfigFactory 的配置文件 [HOCON]

问题描述

我有一个通过 spark-submit 运行的 jar,之前我使用 Argot 解析通过 ConfigFactory 解析的 HOCON 配置文件,然后我从那里读取。

 * spark/bin/spark-submit --class ConsumerApp \
 *                        --master local[2] \
 *                        some-consumer-jar-0.1.0.jar \
 *                        --config config.hocon

不幸的是,Argot 现在是一个死项目,要升级到当前版本的 Scala,我必须开始使用 Scopt ,但我无法理解如何使用 Scopt 解析相同的配置文件并加载到 ConfigFactory 中,而无需进行太多更改。

尝试阅读文档,但找不到太多。

当前实施

def main(args: Array[String]) {

    // General bumf for our app
    val parser = new ArgotParser(
      programName = "generated",
      compactUsage = true,
      preUsage = Some("%s: Version %s, %s.".format(
        generated.Settings.name,
        generated.Settings.version,
        generated.Settings.organization)
      )
    )

    // Optional config argument
    val config = parser.option[Config](List("config"),
      "filename",
      "Configuration file.") {
      (c, opt) =>

        val file = new File(c)
        if (file.exists) {
          ConfigFactory.parseFile(file)
        } else {
          parser.usage("Configuration file \"%s\" does not exist".format(c))
          ConfigFactory.empty()
        }
    }
    parser.parse(args)

    // read the config file if --config parameter is provided else fail
    val conf = config.value.getOrElse(throw new RuntimeException("--config argument must be provided"))

然后像这样读

arg1 = conf.getConfig("somelevel").getString("arg1"),

标签: scalaapache-sparkhoconscopt

解决方案


推荐阅读