Github user mateiz commented on a diff in the pull request:
https://github.com/apache/spark/pull/97#discussion_r11193284
--- Diff: python/pyspark/rdd.py ---
@@ -713,6 +766,34 @@ def merge(a, b):
return sorted(self.mapPartitions(topIterator).reduce(merge))
+ def takeOrdered(self, num, key=None):
+ """
+ Get the N elements from a RDD ordered in ascending order or as
specified
+ by the optional key function.
+
+ >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7]).takeOrdered(6)
+ [1, 2, 3, 4, 5, 6]
+ >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7], 2).takeOrdered(6,
key=lambda x: -x)
+ [(-10, 10), (-9, 9), (-7, 7), (-6, 6), (-5, 5), (-4, 4)]
+ """
+
+ def topNKeyedElems(iterator, key_=None):
+ q = MaxHeapQ()
+ for k in iterator:
+ if not (key_ == None):
+ k = (key_(k), k)
+ if (len(q.q) -1) < num:
--- End diff --
Actually, as a further improvement, I would just take `MaxHeap` take a
`maxSize` parameter for how many elements to keep, and have it implement this
logic in `insert`. That way callers don't have to decide whether to call
`insert` or `replaceRoot`. Just make the API for it as simple as possible.
---
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.
---