How to convert a simple DataFrame to a DataSet Spark Scala with case class?

后端 未结 2 1557
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 03:48

I am trying to convert a simple DataFrame to a DataSet from the example in Spark: https://spark.apache.org/docs/latest/sql-programming-guide.html

case class         


        
2条回答
  •  孤独总比滥情好
    2021-01-17 04:03

    This is how you create dataset from case class

    case class Person(name: String, age: Long) 
    

    Keep the case class outside of the class that has below code

    val primitiveDS = Seq(1,2,3).toDS()
    val augmentedDS = primitiveDS.map(i => Person("var_" + i.toString, (i + 1).toLong))
    augmentedDS.show()
    
    augmentedDS.as[Person].show()
    

    Hope this helped

提交回复
热议问题