Github user falaki commented on a diff in the pull request:
https://github.com/apache/spark/pull/1025#discussion_r13659499
--- Diff: core/src/main/scala/org/apache/spark/rdd/PairRDDFunctions.scala
---
@@ -156,6 +161,182 @@ class PairRDDFunctions[K, V](self: RDD[(K, V)])
}
/**
+ * Return a subset of this RDD sampled by key (via stratified sampling).
+ * We guarantee a sample size = math.ceil(fraction * S_i), where S_i is
the size of the ith
+ * stratum.
+ *
+ * @param withReplacement whether to sample with or without replacement
+ * @param fraction sampling rate
+ * @param seed seed for the random number generator
+ * @return RDD containing the sampled subset
+ */
+ def sampleByKey(withReplacement: Boolean,
+ fraction: Double,
+ seed: Long = Utils.random.nextLong): RDD[(K, V)]= {
+
+ class Stratum(var numItems: Long = 0L, var numAccepted: Long = 0L)
extends Serializable {
+ var waitList: ArrayBuffer[Double] = new ArrayBuffer[Double]
+ var q1: Option[Double] = None
+ var q2: Option[Double] = None
+
+ def incrNumItems(by: Long = 1L) = numItems += by
+
+ def incrNumAccepted(by: Long = 1L) = numAccepted += by
+
+ def addToWaitList(elem: Double) = waitList += elem
+
+ def addToWaitList(elems: ArrayBuffer[Double]) = waitList ++= elems
+
+ override def toString() = {
+ "numItems: " + numItems + " numAccepted: " + numAccepted + " q1: "
+ q1 + " q2: " + q2 +
+ " waitListSize:" + waitList.size
+ }
+ }
+
+ class Result(var resultMap: Map[K, Stratum], var cachedPartitionId:
Option[Int] = None)
+ extends Serializable {
+ var rand: RandomDataGenerator = new RandomDataGenerator
+
+ def getEntry(key: K, numItems: Long = 0L): Stratum = {
+ if (resultMap.get(key).isEmpty) {
+ resultMap += (key -> new Stratum(numItems))
+ }
+ resultMap.get(key).get
--- End diff --
```resultMap(key)```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---