Spark - convert Map to a single-row DataFrame

前端 未结 3 1472
广开言路
广开言路 2021-01-14 02:05

In my application I have a need to create a single-row DataFrame from a Map.

So that a Map like

(\"col1\" -> 5, \"col2\" -> 10, \"col3\" ->          


        
3条回答
  •  情话喂你
    2021-01-14 02:17

    here you go :

    val map: Map[String, Int] = Map("col1" -> 5, "col2" -> 6, "col3" -> 10)
    
    val df = map.tail
      .foldLeft(Seq(map.head._2).toDF(map.head._1))((acc,curr) => acc.withColumn(curr._1,lit(curr._2)))
    
    
    df.show()
    
    +----+----+----+
    |col1|col2|col3|
    +----+----+----+
    |   5|   6|  10|
    +----+----+----+
    

提交回复
热议问题