【问题标题】:SCALA: Read YAML file using Scala with class constructorSCALA:使用带有类构造函数的 Scala 读取 YAML 文件
【发布时间】:2017-02-10 04:15:46
【问题描述】:

我试图使用 scala 和如下构造函数调用来读取 YAML 文件

import org.yaml.snakeyaml.constructor.Constructor
import java.io.{File, FileInputStream}
import scala.collection.mutable.ArrayBuffer
import org.yaml.snakeyaml.Yaml

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

    //val conf = new SparkConf().setAppName("yaml test").setMaster("local[*]")
    //val   sc = new SparkContext(conf)
    val yamlfile = "C:\\Users\\***\\Desktop\\mongoDB\\sparkTest\\project\\properties.yaml"
    val input1 = new FileInputStream(new File(yamlfile))
    val yaml = new Yaml(new Constructor(classOf[ReadProperties]))
    val e = yaml.load(input1).asInstanceOf[ReadProperties]

    println(e.file1)
  }
}

我有一个单独的类,这样我就可以将 YAML 项目作为 bean,如下所示,

class ReadProperties(@BeanProperty  var file1:String,@BeanProperty var file2:String) {

  //constructor
}

我的yaml文件(properties.yaml)的内容如下,

file1: C:\\data\\names.txt
file2: C:\\data\\names2.txt

但错误在于

Can't construct a java object for tag:yaml.org,2002:ReadProperties; exception=java.lang.NoSuchMethodException: ReadProperties.<init>()
 in 'reader', line 1, column 1:
    file1: C:\\data\\names.txt
    ^

	at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:350)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)

但是如果我使用下面的代码它可以工作(没有构造函数类),

  val yaml = new Yaml
  val obj = yaml.load(input)
  val e = obj.asInstanceOf[java.util.HashMap[String,String]]
  println(e)

result :

{file1=C:\\data\\names.txt, file2=C:\\data\\names2.txt}
16/10/02 01:24:28 INFO SparkContext: Invoking stop() from shutdown hook

我希望我的构造函数工作,并希望直接引用 yaml 属性文件中的参数值。 (比如yaml文件“file1”和“file2”有两个参数所以想直接引用)

任何帮助将不胜感激。提前致谢!

【问题讨论】:

    标签: scala constructor yaml snakeyaml


    【解决方案1】:

    This post建议使用

    val yaml = new Yaml
    val obj = yaml.load(input1)
    val e = obj.asInstanceOf[ReadProperties]
    

    因为如果直接反序列化为自定义类对象,SnakeYAML 需要一个非参数构造函数。

    也可以为ReadProperties 类提供自定义构造函数,但由于我并不真正了解 Scala,因此我无法展示其代码。 official documentation 可能会有所帮助。

    【讨论】:

      【解决方案2】:

      它有效,实际问题似乎是环境错误。无论如何,这就是我所做的,是的,我知道在这里发表评论为时已晚,

      def main(args:Array[String]) : Unit = {
      val conf = new SparkConf().setAppName("yaml test").setMaster("local[*]")
      val sc = new SparkContext(conf)
      val yamlfile = "C:\\Users\\Chaitu-Padi\\Desktop\\mongoDB\\sparkTest\\project\\properties.yaml"
      val input1 = new FileInputStream(new File(yamlfile))
      val yaml = new Yaml(new Constructor(classOf[yamlProperties]))
      val e = yaml.load(input1)
      val d = e.asInstanceOf[yamlProperties]
      println(d)
      

      }

      class yamlProperties {
      @BeanProperty var file1: String = null
      @BeanProperty var file2: String = null
      @BeanProperty var file3: String = null
      
      override def toString: String = {
      //return "%s,%s".format(file1, file2)
      return ( "%s,%s,%s").format (file1, file2, file3)
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-16
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        相关资源
        最近更新 更多