gpt4 book ai didi

python - 执行顺序和缓存的需要

转载 作者:可可西里 更新时间:2023-11-01 14:48:02 24 4
gpt4 key购买 nike

让我们考虑使用 spark 的 python 伪代码片段。

    rdd1 = sc.textFile("...")
rdd2 = rdd1.map().groupBy().filter()
importantValue = rdd2.count()
rdd3 = rdd1.map(lambda x : x / importantValue)

在spark任务的DAG中,有两个分支,在创建rdd1之后。两个分支都使用 rdd1,但第二个分支(计算 rdd3)也使用 rdd2 的聚合值(importantValue) .我假设 DAG 看起来像这样: enter image description here我对吗?如果是,我们是否可以假设用于计算 rdd3rdd1 仍在内存中处理?或者我们必须缓存 rdd1 以防止重复加载它?

更一般地说,如果 DAG 看起来像这样: enter image description here我们可以假设两个分支是并行计算的并使用 rdd1 的相同副本吗?或者 Spark 驱动程序会一个接一个地计算这些分支,因为这是两个不同的阶段?我知道在执行之前,spark 驱动程序将 DAG 分成阶段和更详细的逻辑部分 - tasks。一个阶段内的任务可以并行计算,因为其中没有混洗阶段,但是图像中的两个并行分支呢?我知道所有支持 rdd 抽象的直觉(惰性评估等),但这并没有让我更容易理解。请给我任何建议。

最佳答案

I assume that DAG looks something like this: Am I right?

是的。

If yes, can we assume that rdd1 used in computing rdd3 is still handled in memory?

没有。 Spark 利用惰性求值来处理数据。这意味着在需要之前不会计算任何内容。除非有明确的声明,否则不会存储任何内容。

Or we have to cache rdd1 in order to prevent repeated loading of that?

确切地说,您需要缓存 rdd1 以防止文本文件被读取两次。

More generally, if DAG looks like this: can we assume that both branches are computed pararelly and use the same copy of rdd1? Or Spark driver will compute these branches one after another, because these are two different stages?

这两个分支不会并行处理,因为它们有不同的血统。通常,在采取行动之前不会处理任何数据。每当需要一个结果时(读取,调用一个 Action ),所有正在进行的转换和给定谱系中的当前 Action 的数据处理都会发生。之后,除非调用 cache ,否则内存中不会存在任何数据。

查看此 deck转换与 Action 的解释

关于python - 执行顺序和缓存的需要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50219311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com