gpt4 book ai didi

apache-spark - Spark 如何跟踪 randomSplit 中的分割?

转载 作者:行者123 更新时间:2023-12-01 16:25:21 25 4
gpt4 key购买 nike

这个问题解释了Spark的随机分割是如何工作的,How does Sparks RDD.randomSplit actually split the RDD ,但我不明白 Spark 如何跟踪哪些值进入一个拆分,以便这些相同的值不会进入第二个拆分。

如果我们看一下 randomSplit 的实现:

def randomSplit(weights: Array[Double], seed: Long): Array[DataFrame] = {
// It is possible that the underlying dataframe doesn't guarantee the ordering of rows in its
// constituent partitions each time a split is materialized which could result in
// overlapping splits. To prevent this, we explicitly sort each input partition to make the
// ordering deterministic.

val sorted = Sort(logicalPlan.output.map(SortOrder(_, Ascending)), global = false, logicalPlan)
val sum = weights.sum
val normalizedCumWeights = weights.map(_ / sum).scanLeft(0.0d)(_ + _)
normalizedCumWeights.sliding(2).map { x =>
new DataFrame(sqlContext, Sample(x(0), x(1), withReplacement = false, seed, sorted))
}.toArray
}

我们可以看到它创建了两个共享相同 sqlContext 并具有两个不同 Sample(rs) 的 DataFrame。

这两个 DataFrame 如何相互通信,以便第一个 DataFrame 中的值不会包含在第二个 DataFrame 中?

数据是否被提取两次? (假设sqlContext正在从数据库中选择,该选择是否被执行了两次?)。

最佳答案

这与对 RDD 进行采样完全相同。

假设你有权重数组(0.6, 0.2, 0.2),Spark将为每个范围(0.0, 0.6), (0.6, 0.8), (0.8, 1.0)

当需要读取结果 DataFrame 时,Spark 将仅遍历父 DataFrame。对于每个项目,生成一个随机数,如果该数字落在指定范围内,则发出该项目。所有子DataFrame共享相同的随机数生成器(从技术上讲,不同的生成器具有相同的种子),因此随机数的序列是确定性的。

对于你的最后一个问题,如果你没有缓存父DataFrame,那么每次计算输出DataFrame时都会重新获取输入DataFrame的数据。

关于apache-spark - Spark 如何跟踪 randomSplit 中的分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62159261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com