1

I have a dataframe origMap with a column that is of type map. I want to add more entries to that map

I'm doing following that's working:

val origMap = df("mapping")

val tempMap = tempDFFields.flatMap(tempField => Array(lit(tempField), tempDF(tempField))): _*)

origMap.withColumn("mapping", tempMap.union(origMap))

tempDFFields is list of column names in tempDF.

I'm creating a map of all colname->colvalue from tempDF and want to add it to original DF. It complains that I'm passing array of Column instead of single instance of Column. how can I pass single instance of column here .. I just want to update the map and store it back.

Example:

Input

origDF

+--------+-----------------------------
|id  | amount       | mapping         | 
|1   | 10           | {a=b, c=d}      |
|3   | 90           | {e=f, g=h}           |

tempDF

+-----
|Id |
|1  |

output: origDF

+--------+-----------------------------
|id  | amount       | mapping          | 
|1   | 10           | {a=b, c=d, id=1} |
|3   | 90           | {e=f, g=h, id=1} |
7
  • Can you explain with some input and output data aswell? Commented May 18, 2018 at 0:35
  • @ShankarKoirala added Commented May 18, 2018 at 0:44
  • Is the mapping field of type Map, and what is tempDF? Commented May 18, 2018 at 0:49
  • yes, mapping os of type map. tempDF is another DF. Commented May 18, 2018 at 0:57
  • always with one column? what if tempDF Id contains value 1, 2 and 3. Commented May 18, 2018 at 1:02

1 Answer 1

2

You can create an udf to merge the map as below

val origDF = Seq(
  (1, 10, Map("a" -> "b", "c" -> "d")),
  (3, 90, Map("e" -> "f", "g" -> "h"))
).toDF("id", "amount", "mapping")

If you have a single row DF you can create a map directly
val tmpDF = Map("id" -> "1")

//UDf to merge the two map 
val addToMap = udf((mapping: Map[String, String]) => {mapping ++ tmpDF})

//Use the udf 
origDF.withColumn("mapping", addToMap($"mapping"))

.show(false)

Output:

+---+------+----------------------------+
|id |amount|mapping                     |
+---+------+----------------------------+
|1  |10    |Map(a -> b, c -> d, id -> 1)|
|3  |90    |Map(e -> f, g -> h, id -> 1)|
+---+------+----------------------------+

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.