【问题标题】:Not able to read Configuration file Using scala typesafe API无法使用 scala typesafe API 读取配置文件
【发布时间】:2017-04-03 02:51:01
【问题描述】:

我有一个名为 Omega 的 spark/scala 项目

我在 Omega/conf/omega.config 中有一个 conf 文件

我使用 typesafe 的 API 从 conf/omega.config 加载配置文件。 它工作正常,我能够读取每个键的相应值

今天,我第一次在我的 omega.config 文件中添加了更多键值对,并尝试从我的 scala 代码中检索它们。它抛出

 Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'job_name'

在我的 omega.config 文件中为键 job_name 添加新值后,此问题开始发生。 另外我无法读取新添加的 -key-values,我仍然可以使用 config.xml 读取所有旧值。获取字符串方法

我正在使用 maven 构建我的 spark/scala 应用程序。

Omega.config

input_path="/user/cloudera/data
user_name="surender"
job_name="SAMPLE"

我无法单独访问最近添加的密钥“job_name” 包 com.pack1

import com.pack2.ApplicationUtil
object OmegaMain {

  val config_loc = "conf/omega.config"

  def main(args: Array[String]): Unit = {

   val config               = ApplicationUtil.loadConfig(config_loc)
   val jobName              = ApplicationUtil.getFromConfig(config,"job_name")
}
}


package com.pack2
import com.typesafe.config.{Config, ConfigFactory}


object ApplicationUtil {

 def loadConfig(filePath:String):Config={
  val config = ConfigFactory.parseFile(new File(filePath))

  config
 }

  def getFromConfig(config:Config,jobName:String):String={
    config.getString(jobName)
  }

}

有人可以帮我解决问题吗?

【问题讨论】:

  • 您是否将配置直接解析为一个类?向我们展示您用于处理类型安全配置的代码。 minimal reproducible example 如果你愿意的话。
  • @Yuval:也添加了代码
  • 您使用的路径是相对的...也许您没有从以前的相同位置执行您的应用程序?进行测试并尝试指定绝对路径,或者如果文件在您的类路径中,请尝试将其指定为资源而不是构建新的文件(路径)。

标签: scala apache-spark


【解决方案1】:

你可以试试这样的:

def loadConfig(filename: String, syntax: ConfigSyntax): Config = {
val in: InputStream = getClass.getResourceAsStream(filename)
if (in == null) return null
val file: File = File.createTempFile(String.valueOf(in.hashCode()), ".conf")
file.deleteOnExit()

val out: FileOutputStream = new FileOutputStream(file)
val buffer: Array[Byte] = new Array(1024)
var bytesRead: Int = in.read(buffer)
while (bytesRead != -1) { out.write(buffer, 0, bytesRead); bytesRead = in.read(buffer) }
out.close()

val conf: Config = ConfigFactory.parseFile(file, ConfigParseOptions.defaults().setSyntax(syntax).setAllowMissing(false).setOriginDescription("Merged with " + filename))
conf

}

filename 是一些文件路径在类路径中。如果您想更新此方法以考虑一些外部文件,请将更新第 4 次更改为 val file: File = new File("absolute Path of he file")

【讨论】:

  • 这个项目在 IntelliJ Workspace 中。我正在使用 mavne 进行构建。构建没有采用最新更新的 conf 文件。它需要以前版本的conf文件。
  • 我无法帮助您使用 IntelliJ,我是 eclipse 用户...使用 eclipse,当您处理使用 eclipse 构建的项目时可能会发生这种情况(这包括 Run as Maven 从 Eclipse IDE 构建)。我想 IntelliJ 可能会发生类似的事情。尝试从 ${user}/.m2/repository/your-groupId/* 中删除您的构建并重新运行您的测试。它应该将您的工作区项目考虑在内,而不是将任何已安装的版本纳入 mvn 本地 repo。但是好吧,这是一个eclipse技巧,我不知道它是否适用于intelliJ......
【解决方案2】:

我猜在您使用 Maven 构建后,该文件不在类路径中。

由于您使用 Maven 构建 jar,因此您需要将 omega.config 放在类路径中。这意味着您要么必须默认将其放入src/main/resources,要么明确告诉Maven 将conf 添加到默认资源类路径中。

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2013-10-04
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多