我理解在 Scala 中有两种类型的操作
像 map()、filter() 这样的转换是惰性求值的。因此,该优化可以在 Action 执行上完成。例如,如果我先执行 action(),那么 Spark 将优化为仅读取第一行。
但是为什么要懒惰地评估persist() 操作。因为无论我采用哪种方式,无论是急切还是懒惰,它都会根据存储级别保留整个 RDD。
你能详细说明为什么persist()是转换而不是行动。
最佳答案
首先,急切的坚持会污染整个管道。 cache
或 persist
只表示意图。这并不意味着我们将永远达到 RDD 物化并可以实际缓存的地步。此外,还有一些上下文会自动缓存数据。
Because either ways I go, eagerly or lazily, it is going to persist entire RDD as per Storage level.
这不完全正确。事情是,
persist
不持久。正如它明确说明的那样in the documentation为 MEMORY_ONLY
持久度:If the RDD does not fit in memory, some partitions will not be cached and will be recomputed on the fly each time they're needed.
与
MEMORY_AND_DISK
剩余的数据存储到磁盘,但如果没有足够的内存用于后续缓存,仍然可以被逐出。更重要的是:Spark automatically monitors cache usage on each node and drops out old data partitions in a least-recently-used (LRU) fashion.
你也可以说
cache
/persist
在语义上与针对特定 IO 副作用执行的 Spark 操作不同。 cache
更像是对 Spark 引擎的一个提示,我们可能希望稍后重用这段代码。
关于scala - 为什么persist()在Spark中被惰性求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34438670/