Spark2.0什么时候用rdd?

When to use rdd in Spark2.0?

有了新的 SparkSQL API,我们似乎不再需要 RDD。由于 RDD 很昂贵,看来我们应该避免使用它。有人可以解释什么时候是在 Spark2 中使用 RDD 的好时机吗?

it seems that we don't need RDD anymore

RDD API 更通用,实际上 SQL API 是在 RDD API 之上构建的,具有一系列扩展。

Since RDD is expensive, it seems that we should avoid it.

RDD API 本身并不昂贵。它只是不提供与 SQL API 相同的优化。您仍然可以在 RDD 之上构建高性能应用程序(检查例如 org.apache.spark.ml)。

Can someone explain when is a good time to use RDD in Spark2?

它是基于意见的,但如果您需要端到端的类型安全性或经常使用没有内置编码器的类型,RDD API 是一个自然的选择。

当执行顺序很重要时(您可以使用 SQL 创建自己的计划规则,但需要付出更多的努力)或者您需要低级控制(例如用户定义的 [=11],您可能更喜欢 RDD =]).

TLDR:只有在需要对数据的物理分布进行细粒度控制时才应使用 RDD。

这可能与 Spark 2.0 无关,可能与 Spark 2.2 及更高版本相关。我在 Spark: The Definitive Guide 中找到了这个,我发现这本书的这一部分有助于决定是否使用 RDD:

There are basically no instances in modern Spark, for which you should be using RDDs instead of the structured APIs beyond manipulating some very raw unprocessed and unstructured data (p. 44).

如果你决定你绝对需要使用RDD,你可以参考p。本书 "When to use RDDs" 部分中的 212。摘录转载:

In general, you should not manually create RDDs unless you have a very, very specific reason for doing so. They are a much lower-level API that provides a lot of power but also lacks a lot of the optimizations that are available in the Structured APIs. For the vast majority of use cases, DataFrames will be more efficient, more stable, and more expressive than RDDs.

The most likely reason for why you'll want to use RDDs is because you need fine-grained control over the physical distribution of data (custom partitioning of data). (p. 212)