I have a spark dataframe with id and array of json as columns. I want to do group by based on ID and sum the values for same key in the json column.
Note : I don't want to use explode as i have huge dataset and every json has millions of key and value. If i do explode, my datasize will increase exponentially. I want some solution without explode
My dataframe and schema looks like below :
+-------+----------------------------+
|id |json |
+-------+----------------------------+
|a1 |[{key1, 10.0}, {key2, 20.0}]|
|a1 |[{key3, 30.0}, {key1, 40.0}]|
|a2 |[{key1, 10.0}, {key5, 50.0}]|
+-------+----------------------------+
root
|-- id: string (nullable = true)
|-- json: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- key: string (nullable = true)
| | |-- value: double (nullable = true)
Expected Output :
+-------+------------------------------------------+
|id |json |
+-------+------------------------------------------+
|a1 |[{key1, 50.0}, {key2, 20.0}, {key3, 30.0}]|
|a2 |[{key1, 10.0}, {key5, 50.0}] |
+-------+------------------------------------------+