11

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

我想改组列表然后shuffledList.take((0.97*rddList.count).toInt)

但是我怎样才能随机播放rdd?

还是有更好的方法来拆分列表?

4

2 回答 2

22

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

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

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

于 2014-07-21T13:02:00.360 回答
6

你应该使用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
}
于 2014-07-21T13:06:04.753 回答