import spark.implicits._
import org.apache.spark.sql.column
def reverseMap(colName:Column) = map_from_arrays(map_values(colName),map_keys(colName))
val testDF = Seq(("cat",Map("black"->3,"brown"->5,"white"->1)), ("dog",Map("cream"->6,"black"->5,"white"->2)))
.toDF("animal","ageMap")
testDF.show(false)
val testDF1 = testDF.withColumn("keySort",map_from_entries(array_sort(map_entries(col("ageMap")))))
This code runs fine in spark >3 . I want to run spark<3 .
From your comment I gather that your code was working in v3.2.2 and not in v2.4.5.
Your issue is that
map_entriesdoes not exist in Spark v2.4.5. You can get the same functionality by extracting the keys and values separately usingmap_keysandmap_values, and then usingarray_zipto combine them.The first bit is exactly the same:
And the difference is in how you define
testDF1This code ran successfully on a v2.4.5 spark-shell.