Map Spark UDAF (Java)

I run Spark code on Java. I had data with the following schema –

root
|-- userId: string (nullable = true</span>
|-- dt: string (nullable = true)</span>
|-- result: map (nullable = true)</span>
|    |-- key: string
|    |-- value: long (valueContainsNull = true)

And I wanted to get a single record for a user which has the following schema –

root
|-- userId: string (nullable = true)</span>
|-- result: map (nullable = true)
|    |-- key: string
|    |-- value: map (valueContainsNull = true)
|    |    |-- key: string
|    |    |-- value: long (valueContainsNull = true)

Attached the user defined aggregation function I wrote to achieve it. Before that –

MergeMapUDAF mergeMapUDAF = new MergeMapUDAF();
df.groupBy("userId").agg(mergeMapUDAF.apply(df.col("dt"), df.col("result")).as("result"));
Advertisements