简体   繁体   English

Scala Typesafe Config 打印文件路径、键和值

[英]Scala Typesafe Config Printing file path, keys and values

Trying to find a very simple and clean way to just print the filepath and keys along with values that are present inside my application.conf file when using typesafe config library in scala.在 scala 中使用 typesafe 配置库时,试图找到一种非常简单和干净的方法来打印文件路径和键以及我的application.conf文件中存在的值。 I have found many examples that almost do what I need but can't figure out how to properly filter down to the keys only in file.我发现了许多几乎可以满足我需要的示例,但无法弄清楚如何正确过滤到文件中的键。 For example, I tried below variations but it prints too many values along with what I need such as akka etc which I'm not sure where it even gets from.例如,我尝试了以下变体,但它打印了太多值以及我需要的值,例如akka等,我什至不确定它来自哪里。

  val keys = propReader.getConfig().entrySet().map(_.getKey)
  val values = propReader.getConfig().entrySet().map(_.getValue)
  for (key <- keys) if (key.contains("mykey"))
    for (value <- values) println(key, value)

Also tried but also prints too much也尝试过但也打印太多

  private def listingPathsAndKeys(config: Config) {
    import scala.collection.JavaConversions._
    def display(codeAsString: String)(body: => Any) {
      println(body)
    }
    display("config.root().keySet()") {
      config.root().render(ConfigRenderOptions.concise())

    }
  }
  listingPathsAndKeys(propReader.getConfig())

this gives me the root level keys that ideally I would like to filter to a specific root in my file and just print the keys/values:这为我提供了根级键,理想情况下我希望过滤到我的文件中的特定根并只打印键/值:

config.root().keySet()

This answer is based on Typesafe Config 1.3.1这个答案基于 Typesafe Config 1.3.1

To get the path of your config file first get a subtree existing in your application.conf (check @fcat's answer) returning the Config object and then use the origin() method on it.要获取配置文件的路径,首先获取application.conf存在的子树(检查@fcat 的答案),返回Config对象,然后在其上使用origin()方法。 You will get the ConfigOrigin object with the url field that points to your application.conf :您将获得带有指向application.confurl字段的ConfigOrigin对象:

config.getConfig("sample-configuration").origin()

In other words - this will return the origin of the configuration subtree, which in your case is application.conf file.换句话说 - 这将返回配置子树的来源,在您的情况下是application.conf文件。 However, in some cases ConfigOrigin.url can be null.但是,在某些情况下ConfigOrigin.url可以为 null。

The code you presented does not contain any information on how you actually load the configuration.您提供的代码不包含有关您如何实际加载配置的任何信息。 From what you described I assume it is ConfigFactory.load() in which case the library uses a bunch of defaults that load different configs present in the classpath.根据你的描述,我假设它是ConfigFactory.load()在这种情况下,库使用一堆默认值来加载类路径中存在的不同配置。 That is why you get some keys not existing in your application.conf .这就是为什么您的application.conf不存在一些密钥的原因。 And since ConfigFactory.load() actually returns a merged Config from what it can find in the classpath, it is impossible to get its "merged" origin.并且由于ConfigFactory.load()实际上从它可以在类路径中找到的内容返回一个合并的Config ,因此不可能获得它的“合并”来源。

I don't know how to print the path of application.conf in use.我不知道如何打印正在使用的application.conf的路径。 However, I can recommend a good approach for printing key-value pairs:但是,我可以推荐一种打印key-value对的好方法:

1. In your application.conf enclose your parameters in a specified namespace. 1. 在您的application.conf将您的参数包含在指定的命名空间中。

application.conf:应用程序.conf:

 sample-configuration = {
    key1 = "value1"
    key2 = "value2"
 }

2. In your application load the configuration and print only the parameters in the specified namespace: 2. 在您的应用程序中加载配置并仅打印指定命名空间中的参数:

val config: Config = ConfigFactory.load()
val sampleConfig: Config = config.getConfig("sample-configuration")
println(sampleConfig)

Was trying to do the same thing to figure out which value is from which config file.试图做同样的事情来找出哪个值来自哪个配置文件。 An alternative way is to write a script that appends the current file path to each of the value, then dump out the merged config file and you can tell which file the value is from.另一种方法是编写一个脚本,将当前文件路径附加到每个值,然后转储合并的配置文件,您就可以知道该值来自哪个文件。

You can use this tool: Hocon Config Printer你可以使用这个工具: Hocon Config Printer

It will show you the 'final' configuration file, and also the file from where each setting is read from.它将向您显示“最终”配置文件,以及从中读取每个设置的文件。

Example:示例:

hocon-config-printer conf/application.conf  -verbose

Output:输出:

    {
    # conf/akka.conf: 1-5
    "akka" : {
        # conf/akka.conf: 9
        "actor" : {
            # conf/akka.conf: 10
            "default-dispatcher" : {
                # conf/akka.conf: 11
                "fork-join-executor" : {
                    # conf/akka.conf: 12
                    "parallelism-factor" : 3,
                    # conf/akka.conf: 14
                    "parallelism-max" : 200,
                    # conf/akka.conf: 13
                    "parallelism-min" : 10
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM