【发布时间】:2019-10-21 20:44:05
【问题描述】:
我正在上 Spark 的课程,我有点困惑。
所以有下面的代码。我知道第 1 行正在创建元组(单词,1)。然后第 2 行按单词分组并对计数求和。
我不明白的是,第 2 行中的 X 和 Y 是什么。我们只有一个数字输入到 lamda 函数,即 wordcounts 中的计数列(全为 1),那么为什么是 y?
wordCounts = words.map(lambda x: (x, 1)) #outputs [('self', 1), ('employment', 1), ('building', 1)...
wordCounts2 = wordCounts.reduceByKey(lambda x, y: x + y) # outputs [('self', 111), ('an', 178), ('internet', 26)
然后,我们有这段代码,它紧随其后。我知道它对 RDD 进行排序。为了确认我的理解是 X[1] 这个词和 X[2] 的总数?我猜是这样,但我不是 100%
抱歉问了这么愚蠢的问题,但我找不到明确的解释!
wordCountsSorted = wordCounts2.map(lambda x: (x[1], x[0])).sortByKey()
【问题讨论】:
标签: python apache-spark lambda pyspark