Convert Row to map in spark scala

前端 未结 4 962
傲寒
傲寒 2021-02-04 18:23

I have a row from a data frame and I want to convert it to a Map[String, Any] that maps column names to the values in the row for that column.

Is there an easy way to do

4条回答
  •  旧时难觅i
    2021-02-04 18:53

    You can convert your dataframe to rdd and use simple map function and use headernames in the MAP formation inside map function and finally use collect

    val fn = df.schema.fieldNames
    val maps = df.rdd.map(row => fn.map(field => field -> row.getAs(field)).toMap).collect()
    

提交回复
热议问题