1.参数说明
withReplacement:是否是有放回的抽样,就是普通的抽样,我们都是抽过的数据就不能在抽样了,有放回抽样就是可以继续抽以前抽取过的。
num:数据抽样的个数
2.执行源码流程
执行流程主要是调用了RDD的takeSample方法,下面先贴上这个方法的代码:
def takeSample(
withReplacement: Boolean,
num: Int,
seed: Long = Utils.random.nextLong): Array[T] = withScope {
val numStDev = 10.0
require(num >= 0, "Negative number of elements requested")
require(num <= (Int.MaxValue - (numStDev * math.sqrt(Int.MaxValue)).toInt),
"Cannot support a sample size > Int.MaxValue - " +
s"$numStDev * math.sqrt(Int.MaxValue)")
if (num == 0) {
new Array[T](0)
} else {
val initialCount = this.count() //统计数据的总量
if (initialCount == 0) {
new Array[T](0)
} else {
val rand = new Random(seed)
if (!withReplacement && n
Spark源码解析:takeSample方法详解

本文深入探讨Spark的takeSample方法,分析其参数、执行流程和源码细节。讲解了withReplacement参数的含义,指出当结果数组较小且数据加载到driver内存时适用。文章还介绍了count方法获取RDD元素数量的过程,以及在不同抽样条件下如何计算采样率。通过PoissonBounds和BinomialBounds计算概率,然后进行抽样。总结中强调takeSample和sample方法底层都是基于概率进行抽样。
最低0.47元/天 解锁文章
6210

被折叠的 条评论
为什么被折叠?



