Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum values in an iterator in a PySpark groupByKey()

I'm doing my first steps on Spark (Python) and I'm struggling with an iterator inside a groupByKey(). I'm not able to sum the values: My code looks like this:

example = sc.parallelize([('x',1), ('x',1), ('y', 1), ('z', 1)])

example.groupByKey()
x [1,1]
y [1]
z [1]

How to have the sum on Iterator? I tried something like below but it does not work

example.groupByKey().map(lambda (x,iterator) : (x,sum(iterator))
example.groupByKey().map(lambda (x,iterator) : (x,list(sum(iterator)))
like image 815
Leonida Gianfagna Avatar asked Jul 12 '15 09:07

Leonida Gianfagna


2 Answers

You can simply mapValues with sum:

example.groupByKey().mapValues(sum)

although in this particular case reduceByKey is much more efficient:

example.reduceByKey(lambda x, y: x + y)

or

from operator import add

example.reduceByKey(add)
like image 86
zero323 Avatar answered Nov 14 '22 23:11

zero323


Also you can do it in this way:

wordCountsGrouped = wordsGrouped.groupByKey().map(lambda (x,y):(x,map(sum,y))).map(lambda (x,y):(x,y[0]))

It is a bit late but i just found this solution

like image 27
mgarciaibanez Avatar answered Nov 15 '22 01:11

mgarciaibanez