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 Person(name: String, age: Int)
import spark.implicits._
val path = "examples/src/main/resources/people.json"
val peopleDS = spark.read.json(path).as[Person]
peopleDS.show()
But the following problem arises:
Exception in thread "main" org.apache.spark.sql.AnalysisException: Cannot up cast `age` from bigint to int as it may truncate
The type path of the target object is:
- field (class: "scala.Int", name: "age")
- root class: ....
Can anyone help me out?
Edit I noticed that with Long instead of Int works! Why is that?
Also:
val primitiveDS = Seq(1,2,3).toDS()
val augmentedDS = primitiveDS.map(i => ("var_" + i.toString, (i + 1).toLong))
augmentedDS.show()
augmentedDS.as[Person].show()
Prints:
+-----+---+
| _1| _2|
+-----+---+
|var_1| 2|
|var_2| 3|
|var_3| 4|
+-----+---+
Exception in thread "main"
org.apache.spark.sql.AnalysisException: cannot resolve '`name`' given input columns: [_1, _2];
Can Anyone Help me out understand here?
This is how you create dataset from case class
Keep the case class outside of the class that has below code
Hope this helped
If you change Int to Long (or BigInt) it works fine:
Output:
EDIT:
Spark.read.json
by default parses numbers asLong
types - it's safer to do so. You can change the col type after using casting or udfs.EDIT2:
To answer your 2nd question, you need to name the columns correctly before the conversion to Person will work:
Outputs: