RDD Transformation —— sample

原理

sample将RDD这个集合内的元素进行采样,获取所有元素的子集。用户可以设定是否有放回的抽样、百分比、随机种子,进而决定采样方式。
参数说明:

withReplacement=true, 表示有放回的抽样;
withReplacement=false, 表示无放回的抽样。

这里写图片描述

每个方框是一个RDD分区。通过sample函数,采样50%的数据。V1、V2、U1、U2、U3、U4采样出数据V1和U1、U2,形成新的RDD。

源码

/**
 * Return a sampled subset of this RDD.
 */
def sample(withReplacement: Boolean,
    fraction: Double,
    seed: Long = Utils.random.nextLong): RDD[T] = {
  require(fraction >= 0.0, "Negative fraction value: " + fraction)
  if (withReplacement) {
    new PartitionwiseSampledRDD[T, T](this, new PoissonSampler[T](fraction), true, seed)
  } else {
    new PartitionwiseSampledRDD[T, T](this, new BernoulliSampler[T](fraction), true, seed)
  }
}

上手使用

scala> val rdd = sc.makeRDD(1 to 100,2)
rdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[2] at makeRDD at <console>:27

scala> rdd.sample(true,0.1,10).collect
res5: Array[Int] = Array(2, 4, 20, 20, 37, 70, 77)

scala> rdd.sample(true,0.1,10).collect
res6: Array[Int] = Array(2, 4, 20, 20, 37, 70, 77)

scala> rdd.sample(true,0.1,11).collect
res7: Array[Int] = Array(17, 19, 28, 35, 80, 94, 97)

scala> rdd.sample(true,0.1,131).collect
res8: Array[Int] = Array(7, 9, 10, 21, 29, 38, 41, 55, 57, 61, 72, 74, 74, 87, 91)

scala> rdd.sample(true,0.1,9).collect
res9: Array[Int] = Array(1, 19, 25, 28, 39, 60, 62, 77, 84, 88, 93,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值