Convert Map keys to columns in dataframe

PHOTO EMBED

Mon Apr 05 2021 14:20:34 GMT+0000 (UTC)

Saved by @anoopsugur #scala

val keysDF = mapTypeDF.select(explode(map_keys($"property"))).distinct()
val keys = keysDF.collect().map(f=>f.get(0))
val keyCols = keys.map(f=> col("property").getItem(f).as(f.toString))
val expandedDf = mapTypeDF.select(col("name") +: keyCols:_*).show(false)
content_copyCOPY

https://sparkbyexamples.com/spark/spark-convert-map-to-multiple-columns/