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

标签 apache-spark apache-spark-sql apache-spark-mllib

这个问题解释了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/

相关文章:

apache-spark - K-Means 聚类偏向于一个中心

scala - 获取 TrainValidationSplit scala 的最佳参数

apache-spark - 如何在HADOOP_CONF_DIR中指定集群位置?

scala - Spark DataFrame 中的条件连接

apache-spark-sql - 将带有美元符号的字符串转换为数字

scala - Spark DataFrame - 从列中删除空值

scala - spark数据帧爆炸功能错误

apache-spark - 从平均序列预测下一个事件

java - Spark Hbase : How to convert a dataframe to Hbase org. apache.hadoop.hbase.client.Result

apache-spark - Spark : regression model threshold and precision