I have to determine the schema from the values (not the keys) of a Map[String, Object].
Sample map:
val myMap = Map("k1" -> 1, "k2" -> "", "k3"-> new Timestamp(new Date().getTime), "k4" -> 2.0 )
Currently I have created a schema from the keys like below:
// I have created a schema using keys
val schema = StructType(myMap.keys.toSeq.map {
StructField(_, StringType) // StringType is wrong since Object in the Map can be of any datatype
}
// I have created a RDD like below
val rdd = sc.parallelize(Seq(Row.fromSeq(myMap.values.toSeq)))
val df = sc.createDataFrame(rdd,schema)
But now my problem is that the object can be a double or date or timestamp or anything. But I have created a schema using StringType as described above which is wrong.
Any ideas of creating a schema from Map values that are objects?