nodes: 0, module; 1, function_definition; 2, function_name:top; 3, parameters; 4, block; 5, identifier:self; 6, identifier:num; 7, default_parameter; 8, expression_statement; 9, function_definition; 10, function_definition; 11, return_statement; 12, identifier:key; 13, None; 14, comment:"""
          Get the top N elements from an RDD.
  
          .. note:: This method should only be used if the resulting array is expected
              to be small, as all the data is loaded into the driver's memory.
  
          .. note:: It returns the list sorted in descending order.
  
          >>> sc.parallelize([10, 4, 2, 12, 3]).top(1)
          [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]
          """; 15, function_name:topIterator; 16, parameters; 17, block; 18, function_name:merge; 19, parameters; 20, block; 21, call; 22, identifier:iterator; 23, expression_statement; 24, identifier:a; 25, identifier:b; 26, return_statement; 27, attribute; 28, argument_list; 29, yield; 30, call; 31, call; 32, identifier:reduce; 33, identifier:merge; 34, call; 35, attribute; 36, argument_list; 37, attribute; 38, argument_list; 39, attribute; 40, argument_list; 41, identifier:heapq; 42, identifier:nlargest; 43, identifier:num; 44, binary_operator:a + b; 45, keyword_argument; 46, identifier:self; 47, identifier:mapPartitions; 48, identifier:topIterator; 49, identifier:heapq; 50, identifier:nlargest; 51, identifier:num; 52, identifier:iterator; 53, keyword_argument; 54, identifier:a; 55, identifier:b; 56, identifier:key; 57, identifier:key; 58, identifier:key; 59, identifier:key
code: def top(self, num, key=None):
          """
          Get the top N elements from an RDD.
  
          .. note:: This method should only be used if the resulting array is expected
              to be small, as all the data is loaded into the driver's memory.
  
          .. note:: It returns the list sorted in descending order.
  
          >>> sc.parallelize([10, 4, 2, 12, 3]).top(1)
          [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):
              yield heapq.nlargest(num, iterator, key=key)
  
          def merge(a, b):
              return heapq.nlargest(num, a + b, key=key)
  
          return self.mapPartitions(topIterator).reduce(merge)