Github user andrewor14 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/93#discussion_r10362394
  
    --- Diff: python/pyspark/rdd.py ---
    @@ -628,6 +669,26 @@ def mergeMaps(m1, m2):
                     m1[k] += v
                 return m1
             return self.mapPartitions(countPartition).reduce(mergeMaps)
    +    
    +    def top(self, num):
    +        """
    +        Get the top N elements from a RDD.
    +
    +        Note: It returns the list sorted in ascending order.
    +        """
    +        def f(iterator):
    +            q = BoundedPriorityQueue(num)
    +            for k in iterator:
    +                q.put(k)
    +            return q
    +
    +        def f2(a, b):
    +            a.put(b)
    +            return a
    +        q = BoundedPriorityQueue(num)
    +        # I can not come up with a way to avoid this step. 
    +        t = self.mapPartitions(f).collect()
    +        return [k for k in iter(reduce(f2, t, q))]
    --- End diff --
    
    This could just be ```list(reduce(f2, t, q))```


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to