Apache火花和Pythonλ

2024-09-30 10:30:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码

file = spark.textFile("hdfs://...")
counts = file.flatMap(lambda line: line.split(" ")) \
             .map(lambda word: (word, 1)) \
             .reduceByKey(lambda a, b: a + b)
counts.saveAsTextFile("hdfs://...")

http://spark.apache.org/examples.html我已经从这里复制了示例

我无法理解这段代码,尤其是关键字

  1. 平面图
  2. 地图和
  3. 还原比

有人能用通俗易懂的英语解释一下发生了什么事吗。


Tags: lambda代码httpmaplinehdfssparkword
1条回答
网友
1楼 · 发布于 2024-09-30 10:30:15

请参阅内联注释:

file = spark.textFile("hdfs://...") # opens a file
counts = file.flatMap(lambda line: line.split(" ")) \  # iterate over the lines, split each line by space (into words)
             .map(lambda word: (word, 1)) \ # for each word, create the tuple (word, 1)
             .reduceByKey(lambda a, b: a + b) # go over the tuples "by key" (first element) and sum the second elements
counts.saveAsTextFile("hdfs://...")

关于reduceByKey的更详细的解释可以找到here

相关问题 更多 >

    热门问题