代码之家  ›  专栏  ›  技术社区  ›  Rajesh Kumar Dash

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

  •  0
  • Rajesh Kumar Dash  · 技术社区  · 4 年前

    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") 它按预期工作。 任何线索都会有帮助。

    我有个例外

    在com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig。java:124) 在com.typesafe.config.impl.SimpleConfig.find(SimpleConfig。java:159) 在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)

    0 回复  |  直到 4 年前
        1
  •  2
  •   Tomer Shetah Anshul Jain    4 年前

    实际上,您成功地读取了配置。

    你的问题是因为 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"]}))
    
    推荐文章