美文网首页
通过snakeyaml解析yaml文件

通过snakeyaml解析yaml文件

作者: daley_磊 | 来源:发表于2019-05-28 15:26 被阅读0次

    scala 读取yaml 配置文件

    1. 配置maven文件
      pom.xml
          <dependency>
             <groupId>org.yaml</groupId>
             <artifactId>snakeyaml</artifactId>
             <version>1.17</version>
         </dependency>
    
    1. 创建yaml 文件
      config.yaml
        name: CCC
        age: 26
        city: TRUE
        height: 0.015
        details:
           -  first_name: chuchu
              last_name: chen
              home: 泰兴
           -  first_name: chuchu1
              last_name: chen1
              home: 泰兴1
    
    1. 实现方式两种

      1. 通过实体类获取yaml配置文件数据
      • 实体类创建
        SpeedyConfig
        package com.test
        import scala.beans.BeanProperty
        import java.util
        class SpeedyConfig extends Serializable {
              @BeanProperty var name: String = _
              @BeanProperty var age: Int = _
              @BeanProperty var city: String = _
              @BeanProperty var height: Float = _
              @BeanProperty var details: util.ArrayList[util.HashMap[String,Object]] 
        = _
         }
      
      • 实现类
        yaml_entity_test
          package com.test
          import org.yaml.snakeyaml.Yaml
          import org.yaml.snakeyaml.constructor.Constructor
      
          object yaml_entity_test {
              def main(args: Array[String]): Unit = {
                  val stream = getClass.getResourceAsStream("config.yaml")
                  val yaml = new Yaml(new Constructor(classOf[SpeedyConfig]))
                  val config: SpeedyConfig = yaml.load(stream).asInstanceOf[SpeedyConfig]
                  println(s"${config.getName}, ${config.getAge}, ${config.getCity}")
                  println(config.getDetails)
              }
          }
      
      1. 通过map 获取yaml 配置文件数据
      • 实现类
        yaml_map_test
         package com.test
         import org.yaml.snakeyaml.Yaml
         import java.util
      
         object yaml_map_test {
             def main(args: Array[String]): Unit = {
                  val filePath = System.getProperty("user.dir")
                  val yamlPath = filePath+"/src/conf/config.yaml"
                  //读取指定路径yaml 文件
                  val stream = new FileInputStream(new File(yamlPath))
                 // 只能获取当前程序相同目录文件
                  val stream = getClass.getResourceAsStream("config.yaml")
                  val yaml = new Yaml()
                  val obj = yaml.load(stream).asInstanceOf[util.HashMap[String, Object]]
      
                  val stringValue   = obj.get("name")
                  val intValue      = obj.get("age").asInstanceOf[Int]
                  val booleanValue  = obj.get("city").asInstanceOf[Boolean]
                  val floatValue    = obj.get("height").asInstanceOf[Double].toFloat
                  val mapValue = obj.get("details").asInstanceOf[util.ArrayList[util.HashMap[String,Object]]]
                  println(s"name    -> ${stringValue}, " +
         s"age     -> ${intValue}, " +
         s"city    -> ${booleanValue}, " +
         s"height  -> ${floatValue}")
                  println(s"map -> ${mapValue}")
      
              }
           }
      
      

    相关文章

      网友评论

          本文标题:通过snakeyaml解析yaml文件

          本文链接:https://www.haomeiwen.com/subject/wbldtctx.html