python/pyspark/rdd.py
def take(self, num):
        """
        Take the first 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 L{collect} to get
        the whole RDD instead.

        >>> sc.parallelize([2, 3, 4, 5, 6]).cache().take(2)
        [2, 3]
        >>> sc.parallelize([2, 3, 4, 5, 6]).take(10)
        [2, 3, 4, 5, 6]
        """
        def takeUpToNum(iterator):
            taken = 0
            while taken < num:
                yield next(iterator)
                taken += 1
        # Take only up to num elements from each partition we try
        mapped = self.mapPartitions(takeUpToNum)
        items = []
        # TODO(shivaram): Similar to the scala implementation, update the take 
        # method to scan multiple splits based on an estimate of how many elements 
        # we have per-split.
        with _JavaStackTrace(self.context) as st:
            for partition in range(mapped._jrdd.splits().size()):
                partitionsToTake = self.ctx._gateway.new_array(self.ctx._jvm.int, 1)
                partitionsToTake[0] = partition
                iterator = mapped._jrdd.collectPartitions(partitionsToTake)[0].iterator()
                items.extend(mapped._collect_iterator_through_file(iterator))
                if len(items) >= num:
                    break
        return items[:num]

        Take the first 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 L{collect} to get
        the whole RDD instead.

        >>> sc.parallelize([2, 3, 4, 5, 6]).cache().take(2)
        [2, 3]
        >>> sc.parallelize([2, 3, 4, 5, 6]).take(10)
        [2, 3, 4, 5, 6]
        
Take the first 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 L{collect} to get
the whole RDD instead.


Take the first 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 L { collect } to get the whole RDD instead .
def take(self, num):
        def takeUpToNum(iterator):
            taken = 0
            while taken < num:
                yield next(iterator)
                taken += 1
        mapped = self.mapPartitions(takeUpToNum)
        items = []
        with _JavaStackTrace(self.context) as st:
            for partition in range(mapped._jrdd.splits().size()):
                partitionsToTake = self.ctx._gateway.new_array(self.ctx._jvm.int, 1)
                partitionsToTake[0] = partition
                iterator = mapped._jrdd.collectPartitions(partitionsToTake)[0].iterator()
                items.extend(mapped._collect_iterator_through_file(iterator))
                if len(items) >= num:
                    break
        return items[:num]
def take ( self , num ) : def takeUpToNum ( iterator ) : taken = 0 while taken < num : yield next ( iterator ) taken += 1 mapped = self . mapPartitions ( takeUpToNum ) items = [ ] with _JavaStackTrace ( self . context ) as st : for partition in range ( mapped . _jrdd . splits ( ) . size ( ) ) : partitionsToTake = self . ctx . _gateway . new_array ( self . ctx . _jvm . int , 1 ) partitionsToTake [ 0 ] = partition iterator = mapped . _jrdd . collectPartitions ( partitionsToTake ) [ 0 ] . iterator ( ) items . extend ( mapped . _collect_iterator_through_file ( iterator ) ) if len ( items ) >= num : break return items [ : num ]
Take the first num elements of the RDD.
Take the first num elements of the RDD .
"""
        Take the first 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 L{collect} to get
        the whole RDD instead.

        >>> sc.parallelize([2, 3, 4, 5, 6]).cache().take(2)
        [2, 3]
        >>> sc.parallelize([2, 3, 4, 5, 6]).take(10)
        [2, 3, 4, 5, 6]
        """ # Take only up to num elements from each partition we try # TODO(shivaram): Similar to the scala implementation, update the take  # method to scan multiple splits based on an estimate of how many elements  # we have per-split.
self
num