Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding dropTake to RDD. Allows to drop first 'drop' elements and then take next 'take' elements #617

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions core/src/main/scala/spark/RDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,42 @@ abstract class RDD[T: ClassManifest](
return buf.toArray
}

/**
* Drop the first drop elements and then take next num elements of the RDD. This currently scans the partitions *one by one*, so
* it will be slow if a lot of partitions are required. In that case, use dropCollect(drop) to get the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a dropCollect method; maybe suggest collect().drop(drop) instead?

* whole RDD instead.
*/
def dropTake(drop: Int, num: Int): Array[T] = {
if (num == 0) {
return new Array[T](0)
}
val buf = new ArrayBuffer[T]
var p = 0
var dropped = sc.accumulator(0)
while (buf.size < num && p < partitions.size) {
val left = num - buf.size
val accDropped = dropped.value
//still in driver
val res = sc.runJob(this, (it: Iterator[T]) => {
var leftToDrop = drop - accDropped
while (leftToDrop > 0 && it.hasNext) {
it.next()
leftToDrop -= 1
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this will work the way you expect it to - "dropped" is updated in the slave and not 'brought back' : all slaves will see it as '0'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added another commit. Please take a look if that is okay. I added some comments which can aid in the discussion. Can remove them later if we want to merge.

//accumulate all that have been dropped here
dropped += drop - leftToDrop
//if still left to drop then don't take
val taken = if (leftToDrop > 0) it.take(0) else it.take(left)
taken.toArray
}, Array(p), true)
buf ++= res(0)
if (buf.size == num)
return buf.toArray
p += 1
}
return buf.toArray
}

/**
* Return the first element in this RDD.
*/
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/scala/spark/RDDSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class RDDSuite extends FunSuite with LocalSparkContext {
assert(nums.union(nums).collect().toList === List(1, 2, 3, 4, 1, 2, 3, 4))
assert(nums.glom().map(_.toList).collect().toList === List(List(1, 2), List(3, 4)))
assert(nums.collect({ case i if i >= 3 => i.toString }).collect().toList === List("3", "4"))
assert(nums.take(2).toList === List(1, 2))
assert(nums.dropTake(1,2).toList === List(2, 3))
assert(nums.keyBy(_.toString).collect().toList === List(("1", 1), ("2", 2), ("3", 3), ("4", 4)))
val partitionSums = nums.mapPartitions(iter => Iterator(iter.reduceLeft(_ + _)))
assert(partitionSums.collect().toList === List(3, 7))
Expand Down