SCALA: Read YAML file using Scala with class constructor
<p>I was trying to read YAML file using scala with a constructor call as below </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>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)
  }
}</code></pre>
</div>
</div>
</p>

<p>And I have a separate class so that I can have the YAML items as beans as below,</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>class ReadProperties(@BeanProperty  var file1:String,@BeanProperty var file2:String) {

  //constructor
}</code></pre>
</div>
</div>
</p>

<p>And the content of my yaml file(properties.yaml) is as below, </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>file1: C:\\data\\names.txt
file2: C:\\data\\names2.txt</code></pre>
</div>
</div>
</p>

<p>but the error is that </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>Can't construct a java object for tag:yaml.org,2002:ReadProperties; exception=java.lang.NoSuchMethodException: ReadProperties.&lt;init&gt;()
 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)</code></pre>
</div>
</div>
</p>

<p>But if I use the below code it works(without constructor class),</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>  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</code></pre>
</div>
</div>
</p>

<p>I want my constructor to work and wanted to directly refer the values of parameters in yaml properties file. (for example, there are two parameters in yaml file "file1" and "file2" so I wanted to refer them directly)</p>

<p>Any help would be appreciated. thanks in advance!</p>