Forum Discussion

ahouston's avatar
ahouston
New Contributor II
2 years ago

Help With Reduce Function in Mapper

I'm having trouble using the reduce function in a mapper which will subtotal two values across a two field compound key.  For example, take the following input:

"myarray": [{
"key1": 1,
"key2": "A",
"value1": 5,
"value2": 10
}, {
"key1": 1,
"key2": "A",
"value1": 10,
"value2": 20
}, {
"key1": 2,
"key2": "A",
"value1": 15,
"value2": 30
}, {
"key1": 2,
"key2": "B",
"value1": 20,
"value2": 40
}
]

 

My desired result is as follows:

"myarray": [{
"key1": 1,
"key2": "A",
"value1": 15,
"value2": 30
}, {
"key1": 2,
"key2": "A",
"value1": 3,
"value2": 30
}, {
"key1": 2,
"key2": "B",
"value1": 4,
"value2": 40
}
]

Unfortunately due to use of an ultra pipeline, I cannot use a Group By snap and must resolve within a mapper.  Appreciate any guidance from the community on how to best solve this.

Thanks!