Use UDF only if you do not have an inbuilt function for your use case due to performance reasons.
Spark version 2.4 and above
import org.apache.spark.sql.functions.{map_concat, col}
sampleDF.withColumn("map_concat", map_concat(col("mapCol1"), col("mapCol2"))).show(false)
Outputs
+----+-----------------+-----------------+-------------------------------+
|name|mapCol1 |mapCol2 |map_concat |
+----+-----------------+-----------------+-------------------------------+
|Jeff|Map(key1 -> val1)|Map(key2 -> val2)|Map(key1 -> val1, key2 -> val2)|
+----+-----------------+-----------------+-------------------------------+
Spark version 2.4 below
Create a UDF as per @RameshMaharjan answer in this question, but I added with a null check to avoid NPE at runtime which will fail the job eventually if not added.
import org.apache.spark.sql.functions.{udf, col}
val map_concat = udf((map1: Map[String, String],
map2: Map[String, String]) =>
if (map1 == null) {
map2
} else if (map2 == null) {
map1
} else {
map1 ++ map2
})
sampleDF.withColumn("map_concat", map_concat(col("mapCol1"), col("mapCol2")))
.show(false)