【问题标题】:Spark - scala: shuffle RDD / split RDD into two random parts randomlySpark - scala:随机播放 RDD / 将 RDD 拆分为两个随机部分
【发布时间】:2014-09-11 22:37:11
【问题描述】:

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

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

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

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

【问题讨论】:

  • 所有项目是否都是唯一的(即没有重复项?)只是想知道您是否可以使用takeSample(),然后将样本从原始列表中过滤掉。
  • 可以重复,但为什么重要,如果它们是唯一的,你能做什么?
  • 好的,我认为 takeSample 方法不适用于重复项。
  • 这也是有问题的,因为我还想保存第二部分(即 3%)

标签: scala apache-spark rdd


【解决方案1】:

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

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

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

【讨论】:

  • 如何将f1 f2的类型改为DataFrame
【解决方案2】:

你应该使用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 中的 implementation

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
}

【讨论】:

  • @Boern 您可以希望看到另一个答案稍后被编辑。如果此答案已过时,请随时对其进行编辑。
猜你喜欢
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多