У меня есть искровой RDD (полный код ниже), и я немного запутался.
Учитывая входные данные:
385 | 1
291 | 2
Если у меня есть ниже лямбда-функция, почему вlowerByKey у нас есть x [0] + y [0] = 385 + 291? Конечно, X и Y относятся к разным столбцам СДР? Или я принимаю это как означающее, что они ссылаются на
totalsByAge = rdd2.mapValues(lambda x: (x, 1)).reduceByKey(lambda x, y:(x[0] + y[0], x[1] + y[1]))
Полный код:
import findspark
findspark.init()
import pyspark
#UserID | Name | Age | Num_Friends
#r before the filepath converts it to a raw string
lines = sc.textFile(r"c:\Users\kiera\Downloads\fakefriends.csv")
#For each line in the file, split it at the comma
#split 2 is the age
#Split 3 is the number of friends
def splitlines(line):
fields = line.split(',')
age = int(fields[2])
numFriends = int(fields[3])
return (age, numFriends)
rdd2 = lines.map(splitlines)
totalsByAge = rdd2.mapValues(lambda x: (x, 1)).reduceByKey(lambda x, y:(x[0] + y[0], x[1] + y[1]))
rdd2 выглядит примерно так
[(33, 385),
(26, 2),
(55, 221),
(40, 465),
(68, 21),
(59, 318),
(37, 220),
(54, 307)....