Is it possible to find median in spark in a distributed way? I am currently finding: Sum
, Average
, Variance
, Count
using the following code:
dataSumsRdd = numRDD.filter(lambda x: filterNum(x[1])).map(lambda line: (line[0], float(line[1])))\
.aggregateByKey((0.0, 0.0, 0.0),
lambda (sum, sum2, count), value: (sum + value, sum2 + value**2, count+1.0),
lambda (suma, sum2a, counta), (sumb, sum2b, countb): (suma + sumb, sum2a + sum2b, counta + countb))
#Generate RDD of Count, Sum, Average, Variance
dataStatsRdd = dataSumsRdd.mapValues(lambda (sum, sum2, count) : (count, sum, sum/count, round(sum2/count - (sum/count)**2, 7)))
I am not quite sure how to find Median though. To find standard deviation I just do the result locally with square rooting variance. Once I gather median I can than easily do Skewness locally as well.
I have my data in Key/Value pairs (key = column)