繁体   English   中英

Spark - scala:随机将RDD /拆分RDD分成两个随机部分

[英]Spark - scala: shuffle RDD / split RDD into two random parts randomly

我如何获取rdd数组的spark,并将其随机分成两个rdds,这样每个rdd将包含一些数据(比如说97%和3%)。

我想要洗牌清单然后shuffledList.take((0.97*rddList.count).toInt)

但是我该如何改变rdd呢?

或者是否有更好的方法来拆分列表?

我找到了一种简单快捷的方法来拆分数组:

val Array(f1,f2) = data.randomSplit(Array(0.97, 0.03))

它将使用提供的权重拆分数据。

你应该使用randomSplit方法:

def randomSplit(weights: Array[Double], seed: Long = Utils.random.nextLong): Array[RDD[T]]

// Randomly splits this RDD with the provided weights.
// weights for splits, will be normalized if they don't sum to 1
// returns split RDDs in an array

这是它在spark 1.0中的实现

def randomSplit(weights: Array[Double], seed: Long = Utils.random.nextLong): Array[RDD[T]] = {
    val sum = weights.sum
    val normalizedCumWeights = weights.map(_ / sum).scanLeft(0.0d)(_ + _)
    normalizedCumWeights.sliding(2).map { x =>
       new PartitionwiseSampledRDD[T, T](this, new BernoulliSampler[T](x(0), x(1)),seed)
    }.toArray
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM