1
votes

Can you please let me know on how to create data frame from the following code ?

val x =List(Map("col1"->"foo","col2"->"bar"))
val RDD =sc.parallelize(x)

The input is as shown above ie's RDD[Map[String, String]] Want to covert into dataframe with col1 and col2 as column names and foo and bar as one single row.

1

1 Answers

2
votes

You can create a case class, convert Maps in the rdd to case class and then toDF should work:

case class r(col1: Option[String], col2: Option[String])

RDD.map(m => r(m.get("col1"), m.get("col2"))).toDF.show
+----+----+
|col1|col2|
+----+----+
| foo| bar|
+----+----+