gpt4 book ai didi

scala - Spark DataFrames/Datasets 在缓存时共享数据吗?

转载 作者:行者123 更新时间:2023-12-01 09:35:29 26 4
gpt4 key购买 nike

假设我做这样的事情:

def readDataset: Dataset[Row] = ???

val ds1 = readDataset.cache();

val ds2 = ds1.withColumn("new", lit(1)).cache();

威尔 ds2ds1共享列中除"new"之外的所有数据添加到 ds2 ?如果我缓存两个数据集,它会将整个数据集存储在内存中 dsds2还是共享数据只存储一次?

如果数据是共享的,那么当这种共享被破坏时(因此相同的数据存储在两个内存位置)?

我知道数据集和 rdds 是不可变的,但是如果共享数据,我找不到明确的答案。

最佳答案

简而言之:缓存的数据不会被共享 .

使用 Spark UI 中的代码片段和相应的内存使用情况来说服您的实验证明:

val df = spark.range(10000000).cache()
val df2 = df.withColumn("other", col("id")*3)
df2.count()

使用大约 10MB 的内存:

enter image description here

尽管

val df = spark.range(10000000).cache()
val df2 = df.withColumn("other", col("id")*3).cache()
df2.count()

使用大约 30MB:
  • df : 10MB
  • df2 : 10MB 用于复制的列,另外 10MB 用于新列:

  • enter image description here

    关于scala - Spark DataFrames/Datasets 在缓存时共享数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58860546/

    26 4 0
    文章推荐: actionscript-3 - 使用 Vector. 代替标准数组有什么优势吗?