如何强制 Spark 评估内联的 DataFrame 操作
How to force Spark to evaluate DataFrame operations inline
根据 Spark RDD docs:
All transformations in Spark are lazy, in that they do not compute their results right away...This design enables Spark to run more efficiently.
有时我需要对我的数据帧执行某些操作当时和现在。但是因为数据帧操作是“延迟评估”(根据上述),当我在代码中编写这些操作时,几乎无法保证 Spark 实际上 与其余代码一起执行这些操作。例如:
val someDataFrame : DataFrame = getSomehow()
val someOtherDataFrame : DataFrame = getSomehowAlso()
// Do some stuff with 'someDataFrame' and 'someOtherDataFrame'
// Now we need to do a union RIGHT HERE AND NOW, because
// the next few lines of code require the union to have
// already taken place!
val unionDataFrame : DataFrame = someDataFrame.unionAll(someOtherDataFrame)
// Now do some stuff with 'unionDataFrame'...
所以我的解决方法(到目前为止)是 运行 .show()
or .count()
紧跟在我的时间敏感数据帧操作之后,像这样:
val someDataFrame : DataFrame = getSomehow()
val someOtherDataFrame : DataFrame = getSomehowAlso()
// Do some stuff with 'someDataFrame' and 'someOtherDataFrame'
val unionDataFrame : DataFrame = someDataFrame.unionAll(someOtherDataFrame)
unionDataFrame.count() // Forces the union to execute/compute
// Now do some stuff with 'unionDataFrame'...
...which forces Spark 执行 dataframe 操作然后在那里,内联。
我觉得 hacky/kludgy 很糟糕。所以我问:是否有更普遍接受的 and/or 有效方法来 强制 数据帧操作按需发生(而不是被延迟评估)?
没有.
您必须调用 action 来强制 Spark 进行实际工作。 Transformations 不会触发那种效果,这就是喜欢 spark.
的原因之一
顺便说一句,我很确定 spark 非常清楚什么时候必须做某事 "right here and now",所以您可能正在关注点错了。
Can you just confirm that count()
and show()
are considered "actions"
您可以在documentation中看到Spark的一些动作函数,其中列出了count()
。 show()
不是,我也没有用过,但是感觉像是一个动作——不做实际工作怎么能显示结果呢? :)
Are you insinuating that Spark would automatically pick up on that, and do the union (just in time)?
是! :)
spark 记住你调用的 transformations,当 action 出现时,它会执行它们,就在-正确的时机!
需要记住的事情:由于这项政策,只有在出现 操作 时才进行实际工作,您将不会在 中看到逻辑错误转换(s),直到动作发生!
我同意你的看法,在某些时候你想在需要的时候做这个动作。
例如,如果您正在使用 Spark 流式传输数据,并且您想要评估在每个 RDD 上完成的转换,而不是为每个 RDD 累积转换,并且突然 运行 对这一大数据集采取行动。
现在,假设您有一个 DataFrame,并且已经对它进行了所有转换,那么您可以使用 sparkContext.sql("CACHE table <table-name>")
。
此缓存是急切缓存,这将触发此 DataFrame 上的操作,并评估此 DataFrame 上的所有转换。
根据 Spark RDD docs:
All transformations in Spark are lazy, in that they do not compute their results right away...This design enables Spark to run more efficiently.
有时我需要对我的数据帧执行某些操作当时和现在。但是因为数据帧操作是“延迟评估”(根据上述),当我在代码中编写这些操作时,几乎无法保证 Spark 实际上 与其余代码一起执行这些操作。例如:
val someDataFrame : DataFrame = getSomehow()
val someOtherDataFrame : DataFrame = getSomehowAlso()
// Do some stuff with 'someDataFrame' and 'someOtherDataFrame'
// Now we need to do a union RIGHT HERE AND NOW, because
// the next few lines of code require the union to have
// already taken place!
val unionDataFrame : DataFrame = someDataFrame.unionAll(someOtherDataFrame)
// Now do some stuff with 'unionDataFrame'...
所以我的解决方法(到目前为止)是 运行 .show()
or .count()
紧跟在我的时间敏感数据帧操作之后,像这样:
val someDataFrame : DataFrame = getSomehow()
val someOtherDataFrame : DataFrame = getSomehowAlso()
// Do some stuff with 'someDataFrame' and 'someOtherDataFrame'
val unionDataFrame : DataFrame = someDataFrame.unionAll(someOtherDataFrame)
unionDataFrame.count() // Forces the union to execute/compute
// Now do some stuff with 'unionDataFrame'...
...which forces Spark 执行 dataframe 操作然后在那里,内联。
我觉得 hacky/kludgy 很糟糕。所以我问:是否有更普遍接受的 and/or 有效方法来 强制 数据帧操作按需发生(而不是被延迟评估)?
没有.
您必须调用 action 来强制 Spark 进行实际工作。 Transformations 不会触发那种效果,这就是喜欢 spark.
的原因之一顺便说一句,我很确定 spark 非常清楚什么时候必须做某事 "right here and now",所以您可能正在关注点错了。
Can you just confirm that
count()
andshow()
are considered "actions"
您可以在documentation中看到Spark的一些动作函数,其中列出了count()
。 show()
不是,我也没有用过,但是感觉像是一个动作——不做实际工作怎么能显示结果呢? :)
Are you insinuating that Spark would automatically pick up on that, and do the union (just in time)?
是! :)
spark 记住你调用的 transformations,当 action 出现时,它会执行它们,就在-正确的时机!
需要记住的事情:由于这项政策,只有在出现 操作 时才进行实际工作,您将不会在 中看到逻辑错误转换(s),直到动作发生!
我同意你的看法,在某些时候你想在需要的时候做这个动作。 例如,如果您正在使用 Spark 流式传输数据,并且您想要评估在每个 RDD 上完成的转换,而不是为每个 RDD 累积转换,并且突然 运行 对这一大数据集采取行动。
现在,假设您有一个 DataFrame,并且已经对它进行了所有转换,那么您可以使用 sparkContext.sql("CACHE table <table-name>")
。
此缓存是急切缓存,这将触发此 DataFrame 上的操作,并评估此 DataFrame 上的所有转换。