尝试使用configfactory.parsestring时,找不到键“conf”的配置设置

btqmn9zl  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(475)

我正在尝试读取存储在s3 bucket中的application.conf。我使用bufferedsource从s3中读取,但是当我尝试使用 ConfigFactory.parseString(source.mkString).getConfig("conf") 它没有找到“conf”,它就在那里。下面是我的源代码:

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.services.s3.model.S3Object
import com.amazonaws.services.s3.{AmazonS3Client, AmazonS3ClientBuilder, AmazonS3URI}
import scala.collection.JavaConversions._

import scala.io.{BufferedSource, Source}

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

    import com.amazonaws.auth.BasicAWSCredentials
    val credentials = new BasicAWSCredentials("key", "secertkey")
  //  val credentialsProvider = new DefaultAWSCredentialsProviderChain()
    val s3Client = new AmazonS3Client(credentials)
    val uri: AmazonS3URI = new AmazonS3URI("s3://test-buck/conf/application.conf")
    val s3Object: S3Object = s3Client.getObject(uri.getBucket, uri.getKey)

    val source: BufferedSource = Source.fromInputStream(s3Object.getObjectContent)
    try {
      println(source.mkString)
      import com.typesafe.config.{Config, ConfigFactory}
      val rawConfig: Config = ConfigFactory.parseString(source.mkString)
      val rootConfig = rawConfig.getConfig("conf")

      println(rootConfig)
//      println(rotConfig)
    } finally {
      source.close()
    }

  }

}
我的应用程序配置如下所示

conf {

       source_data_list = ["OL", "SB","1CP"]
//some other value

 OL {
    filename = "receipts_delta_GBR_14_10_2017.csv"
    sftp_conf {
      hostname = "endpoint"
      port = "22"
      username = "ubuntu"
      pem = "pemfile"
      filetype = "csv"
      delimiter = "|"
      directory = "/home/ubuntu/data"
    }

  }
    }

我不知道我做错了什么。如果我放在资源上并尝试加载相同的应用程序配置 ConfigFactory.load("application.conf").getConfig("conf") 它按预期工作。任何线索都会有帮助。
我有个例外
线程“main”中的异常线程“main”com.typesafe.config.configexception$中的异常丢失:在com.typesafe.config.impl.simpleconfig.findkey(simpleconfig)中找不到键“conf”的配置设置。java:124)在com.typesafe.config.impl.simpleconfig.find(simpleconfig。java:145)在com.typesafe.config.impl.simpleconfig.find(simpleconfig。java:159)在com.typesafe.config.impl.simpleconfig.find(simpleconfig。java:164)在com.typesafe.config.impl.simpleconfig.getobject(simpleconfig。java:218)在com.typesafe.config.impl.simpleconfig.getconfig(simpleconfig。java:224)在com.typesafe.config.impl.simpleconfig.getconfig(simpleconfig。java:33)在com.dsm.utils.test$.main(test。scala:26)位于com.dsm.utils.test.main(test.scala)

eivgtgni

eivgtgni1#

实际上,您成功地读取了配置。
你的问题是因为 BufferedSource . 缓冲源可以读取一次。你读了它,为了调试,我猜,然后源代码就结束了。第二次阅读时,为了填充 rawConfig 你得到一个空字符串。我通过将配置字符串提取到一个变量中,然后使用它来解决这个问题。

val config = source.mkString
println(s"config is: $config")
val rawConfig: Config = ConfigFactory.parseString(config)
val rootConfig = rawConfig.getConfig("conf")
println(s"rootConfig is: $rootConfig")

输出为:

rootConfig is: Config(SimpleConfigObject({"OL":{"filename":"receipts_delta_GBR_14_10_2017.csv","sftp_conf":{"delimiter":"|","directory":"/home/ubuntu/data","filetype":"csv","hostname":"endpoint","pem":"pemfile","port":"22","username":"ubuntu"}},"source_data_list":["OL","SB","1CP"]}))

相关问题