在运行时从自定义位置加载 HOCON 格式的类型安全配置

Load typesafe configs in HOCON format from custom locations at runtime

我有一个应用程序可以通过文件获取工作配置信息(可能有几十个文件,每个文件都描述了应用程序要完成的特定工作,最好能灵活地确定它们所在的位置),为此我们选择 HOCON 格式和类型安全库。

它在从资源文件夹加载开发时运行良好,但目的是在运行时通过参数传递这些配置文件的路径,同时调用 spark-submit (spark-submit ... pathToFile ...) .

但是读取文件路径失败并出现此错误:

val jConfig = ConfigFactory.load(path)

com.typesafe.config.ConfigException$Missing: system properties: No configuration setting found for key 'configuration'

如何从 /resources 以外的其他位置读取配置文件?

解决方案

根据@hagarwal 提示,以下方法有效

val input = Source.fromFile(path,"UTF8").mkString
val jConfig = ConfigFactory.parseString(input)

将配置存储在HDFS/S3/ADFS/Local文件系统中,将配置文件路径作为参数传递给程序,从配置文件路径中读取配置文件作为输入流(或文件)。

val confPath = args(0) //Configuration file path
val stream = Client.getObject(confPath) //Client -> HDFS/S3/ADFS/Local
val configString = Source.fromInputStream(stream.asInstanceOf[InputStream]).mkString
val config = ConfigFactory.parseString(configString)