Skip to content

Commit fb0db77

Browse files
daviesJoshRosen
authored andcommitted
[SPARK-2871] [PySpark] add zipWithIndex() and zipWithUniqueId()
RDD.zipWithIndex() Zips this RDD with its element indices. The ordering is first based on the partition index and then the ordering of items within each partition. So the first item in the first partition gets index 0, and the last item in the last partition receives the largest index. This method needs to trigger a spark job when this RDD contains more than one partitions. >>> sc.parallelize(range(4), 2).zipWithIndex().collect() [(0, 0), (1, 1), (2, 2), (3, 3)] RDD.zipWithUniqueId() Zips this RDD with generated unique Long ids. Items in the kth partition will get ids k, n+k, 2*n+k, ..., where n is the number of partitions. So there may exist gaps, but this method won't trigger a spark job, which is different from L{zipWithIndex} >>> sc.parallelize(range(4), 2).zipWithUniqueId().collect() [(0, 0), (2, 1), (1, 2), (3, 3)] Author: Davies Liu <[email protected]> Closes apache#2092 from davies/zipWith and squashes the following commits: cebe5bf [Davies Liu] improve test cases, reverse the order of index 0d2a128 [Davies Liu] add zipWithIndex() and zipWithUniqueId()
1 parent b1b2030 commit fb0db77

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

python/pyspark/rdd.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,53 @@ def batch_as(rdd, batchSize):
17411741
other._jrdd_deserializer)
17421742
return RDD(pairRDD, self.ctx, deserializer)
17431743

1744+
def zipWithIndex(self):
1745+
"""
1746+
Zips this RDD with its element indices.
1747+
1748+
The ordering is first based on the partition index and then the
1749+
ordering of items within each partition. So the first item in
1750+
the first partition gets index 0, and the last item in the last
1751+
partition receives the largest index.
1752+
1753+
This method needs to trigger a spark job when this RDD contains
1754+
more than one partitions.
1755+
1756+
>>> sc.parallelize(["a", "b", "c", "d"], 3).zipWithIndex().collect()
1757+
[('a', 0), ('b', 1), ('c', 2), ('d', 3)]
1758+
"""
1759+
starts = [0]
1760+
if self.getNumPartitions() > 1:
1761+
nums = self.mapPartitions(lambda it: [sum(1 for i in it)]).collect()
1762+
for i in range(len(nums) - 1):
1763+
starts.append(starts[-1] + nums[i])
1764+
1765+
def func(k, it):
1766+
for i, v in enumerate(it, starts[k]):
1767+
yield v, i
1768+
1769+
return self.mapPartitionsWithIndex(func)
1770+
1771+
def zipWithUniqueId(self):
1772+
"""
1773+
Zips this RDD with generated unique Long ids.
1774+
1775+
Items in the kth partition will get ids k, n+k, 2*n+k, ..., where
1776+
n is the number of partitions. So there may exist gaps, but this
1777+
method won't trigger a spark job, which is different from
1778+
L{zipWithIndex}
1779+
1780+
>>> sc.parallelize(["a", "b", "c", "d", "e"], 3).zipWithUniqueId().collect()
1781+
[('a', 0), ('b', 1), ('c', 4), ('d', 2), ('e', 5)]
1782+
"""
1783+
n = self.getNumPartitions()
1784+
1785+
def func(k, it):
1786+
for i, v in enumerate(it):
1787+
yield v, i * n + k
1788+
1789+
return self.mapPartitionsWithIndex(func)
1790+
17441791
def name(self):
17451792
"""
17461793
Return the name of this RDD.

0 commit comments

Comments
 (0)