0

I want to convert List[Map] to a spark dataframe,the keys of Map are sname,the keys of Map are DataFrame's columns

2 Answers 2

2

If you already have res which is a List[Map[String,String]] :

res: List[Map[String,String]] = List(Map(A -> a1, B -> b1, C -> c1), Map(A -> a2, B -> b2, C -> c2))

You can do this to create you dataframe:

//create your rows
val rows = res.map(m => Row(m.values.toSeq:_*))

//create the schema from the header
val header = res.head.keys.toList
val schema = StructType(header.map(fieldName => StructField(fieldName, StringType, true)))

//create your rdd
val rdd = sc.parallelize(rows)

//create your dataframe using 
val df = spark.createDataFrame(rdd, schema)

You can output the result with df.show() :

+---+---+---+
|  A|  B|  C|
+---+---+---+
| a1| b1| c1|
| a2| b2| c2|
+---+---+---+

Note that you can also create your schema in this way:

val schema = StructType(
   List(
     StructField("A", StringType, true),
     StructField("B", StringType, true),
     StructField("C", StringType, true)
   )
 )
Sign up to request clarification or add additional context in comments.

Comments

0

Here you can do this way

val map1 = {"a"->1}
val map2 = {"b"->2}
val lst = List(map1,map2)
val lstDF = lst.toDF
lstDF.take(2).foreach(println)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.