I've got a spark method where I'm running a flatMap
function which is returning me a list of tuples. The key value in the tuple is a Timestamp
, and the value is a dict
.
[(Timestamp('2000-01-01 00:00:00'),
{'id': '1', 'val': '200M', 'date':Timestamp('2000-01-01 00:00:00')}),
(Timestamp('2000-01-01 00:00:00'),
{'id': '2', 'val': '10M', 'date':Timestamp('2000-01-01 00:00:00')}),
(Timestamp('2000-01-01 00:00:00'),
{'id': '3', 'val': '30M', 'date':Timestamp('2000-01-01 00:00:00')}),
(Timestamp('2000-01-02 00:00:00'),
{'id': '15', 'val': '120M', 'date':Timestamp('2000-01-02 00:00:00')}),
(Timestamp('2000-01-02 00:00:00'),
{'id': '3', 'val': '35M', 'date':Timestamp('2000-01-02 00:00:00')}),
(Timestamp('2000-01-02 00:00:00'),
{'id': '4', 'val': '56M', 'date':Timestamp('2000-01-02 00:00:00')}),
(Timestamp('2000-01-03 00:00:00'),
{'id': '6', 'val': '5M', 'date':Timestamp('2000-01-03 00:00:00')}),
(Timestamp('2000-01-03 00:00:00'),
{'id': '1', 'val': '25M', 'date':Timestamp('2000-01-03 00:00:00')}),
(Timestamp('2000-01-03 00:00:00'),
{'id': '2', 'val': '7M', 'date':Timestamp('2000-01-03 00:00:00')}),
I'm trying to run a reduceByKey
function next which gives me:
[ (Timestamp('2000-01-01 00:00:00'),
[{'id': '1', 'val': '200M', 'date':Timestamp('2000-01-01 00:00:00')},
{'id': '2', 'val': '10M', 'date':Timestamp('2000-01-01 00:00:00')},
{'id': '3', 'val': '30M', 'date':Timestamp('2000-01-01 00:00:00')}]),
(Timestamp('2000-01-02 00:00:00'),
[{'id': '15', 'val': '120M', 'date':Timestamp('2000-01-02 00:00:00')},
{'id': '3', 'val': '35M', 'date':Timestamp('2000-01-02 00:00:00')},
{'id': '4', 'val': '56M', 'date':Timestamp('2000-01-02 00:00:00')}]),
(Timestamp('2000-01-03 00:00:00'),
[{'id': '6', 'val': '5M', 'date':Timestamp('2000-01-03 00:00:00')},
{'id': '1', 'val': '25M', 'date':Timestamp('2000-01-03 00:00:00')},
{'id': '2', 'val': '7M', 'date':Timestamp('2000-01-03 00:00:00')}]) ]
So far I've tried this:
output = rdd.flatMap(split_func).reduceByKey(lambda x, y: x+y).collect()
but I get this error:
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
Thanks in advance!