No configuration setting found for key ‘conf’ while trying to use ConfigFactory.parseString.

huangapple go评论54阅读模式
英文:

No configuration setting found for key 'conf' while trying to use ConfigFactory.parseString

问题

I am trying to read my application.conf which is stored in my s3 bucket.I used Bufferedsource to read from s3 but when I try to use ConfigFactory.parseString(source.mkString).getConfig("conf") it did not find the 'conf' which is there. Below is my source code:

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", "secretkey")
    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)
    } finally {
      source.close()
    }
  }
}

My application config looks like below:

conf {
  source_data_list = ["OL", "SB", "1CP"]

  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"
    }
  }
}

Not sure what I am doing wrong here. The same application config, if I put it on a resource and try loading it by ConfigFactory.load("application.conf").getConfig("conf"), it works as expected.

Exception I got:

Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'conf'
at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:145)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)
at com.typesafe.config.impl.SimpleConfig.getObject(SimpleConfig.java:218)
at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:224)
at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:33)
at com.dsm.utils.Test$.main(Test.scala:26)
at com.dsm.utils.Test.main(Test.scala)

(Note: This translation is only for the provided code and not for additional questions or requests.)

英文:

I am trying to read my application.conf which is stored in in my s3 bucket.I used Bufferedsource to read from s3 but when I try to use ConfigFactory.parseString(source.mkString).getConfig("conf") it did not find the 'conf' which is there.Below is my source code :

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()
    }

  }

}

My application config looks like below

  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"
    }

  }
    }

Not sure what i am doing wrong here .Same application config if i put on resource and try loading by ConfigFactory.load("application.conf").getConfig("conf") it works as expected .
Any clue on this would help .

Exception I got

Exception in thread "main" Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'conf'
at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:145)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)
at com.typesafe.config.impl.SimpleConfig.getObject(SimpleConfig.java:218)
at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:224)
at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:33)
at com.dsm.utils.Test$.main(Test.scala:26)
at com.dsm.utils.Test.main(Test.scala)

答案1

得分: 2

你成功读取了配置。

你遇到的问题是由于 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"]}))
英文:

Actually you succeeded to read the configuration.

The issue you're having is because of BufferedSource. The Buffered source can be read once. You read it, in order to debug, I guess, and then the source gets to the end. The second time you read it, in order to populate rawConfig you get an empty string. I solved it by extracting the configuration string into a variable, and then using it.

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")

The output is:

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"]}))

huangapple
  • 本文由 发表于 2020年8月2日 14:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63213087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定