Spark Scala Dataframe convert a column of Array of Struct to a column of Map

Viewed 5428

I am new to Scala. I have a Dataframe with fields

ID:string, Time:timestamp, Items:array(struct(name:string,ranking:long))

I want to convert each row of the Items field to a hashmap, with the name as the key. I am not very sure how to do this.

2 Answers

Since 2.4.0, one can use map_from_entries:

import spark.implicits._
import org.apache.spark.sql.functions._

val df = Seq(
  (Array(("n1", 4L), ("n2", 5L))),
  (Array(("n3", 6L), ("n4", 7L)))
).toDF("Items")

df.select(map_from_entries($"Items")).show

/*
+-----------------------+
|map_from_entries(Items)|
+-----------------------+
|     [n1 -> 4, n2 -> 5]|
|     [n3 -> 6, n4 -> 7]|
+-----------------------+
*/
Related