Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add key to top()
  • Loading branch information
davies committed Aug 23, 2014
commit ccbaf25ce6d601bcbc7cb6081128c2b4236925ad
16 changes: 6 additions & 10 deletions python/pyspark/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ def mergeMaps(m1, m2):
return m1
return self.mapPartitions(countPartition).reduce(mergeMaps)

def top(self, num):
def top(self, num, key=None):
"""
Get the top N elements from a RDD.

Expand All @@ -947,20 +947,16 @@ def top(self, num):
[12]
>>> sc.parallelize([2, 3, 4, 5, 6], 2).top(2)
[6, 5]
>>> sc.parallelize([10, 4, 2, 12, 3]).top(3, key=str)
[4, 3, 2]
"""
def topIterator(iterator):
q = []
for k in iterator:
if len(q) < num:
heapq.heappush(q, k)
else:
heapq.heappushpop(q, k)
yield q
yield heapq.nlargest(num, iterator, key=key)

def merge(a, b):
return next(topIterator(a + b))
return heapq.nlargest(num, a + b, key=key)

return sorted(self.mapPartitions(topIterator).reduce(merge), reverse=True)
return self.mapPartitions(topIterator).reduce(merge)

def takeOrdered(self, num, key=None):
"""
Expand Down