Pyspark 与 Elasticsearch

Pyspark with Elasticsearch

我将 Pyspark 与 Elasticsearch 结合使用。我注意到当您创建一个 RDD 时,它不会在任何收集、计数或任何其他 'final' 操作之前执行。

是否可以执行和缓存转换后的 RDD,因为我也将转换后的 RDD 结果用于其他用途。

就像我在评论区说的,

All transformations in Spark are lazy, in that they do not compute their results right away. Instead, they just remember the transformations applied to some base dataset (e.g. a file). The transformations are only computed when an action requires a result to be returned to the driver program. This design enables Spark to run more efficiently – for example, we can realize that a dataset created through map will be used in a reduce and return only the result of the reduce to the driver, rather than the larger mapped dataset.

别无选择。

为什么偷懒

函数式编程的惰性求值优势:

  • 通过避免不必要的计算和评估复合表达式时的错误条件来提高性能
  • 构建潜在无限数据结构的能力
  • 将控制结构定义为抽象而不是原语的能力

注意: 大多数新的函数式编程语言都是惰性的(例如 Haskell、Scala)。即使您使用的是 Python,Spark 也是用 Scala 编写的。

然而,如果您想在每个 RDD 定义后计算您的 RDD,您可以根据需要在缓存后执行 count 操作,但我看不出这样做有什么意义。您最终会在需要时获得 RDD。