-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-2871] [PySpark] add zipWithIndex() and zipWithUniqueId() #2092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1715,6 +1715,52 @@ def batch_as(rdd, batchSize): | |
| other._jrdd_deserializer) | ||
| return RDD(pairRDD, self.ctx, deserializer) | ||
|
|
||
| def zipWithIndex(self): | ||
| """ | ||
| 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)] | ||
| """ | ||
| starts = [0] | ||
| if self.getNumPartitions() > 1: | ||
| nums = self.mapPartitions(lambda it: [sum(1 for i in it)]).collect() | ||
| for i in range(len(nums) - 1): | ||
| starts.append(starts[-1] + nums[i]) | ||
|
|
||
| def func(k, it): | ||
| return enumerate(it, starts[k]) | ||
|
|
||
| return self.mapPartitionsWithIndex(func) | ||
|
|
||
| def zipWithUniqueId(self): | ||
| """ | ||
| 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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, it might be better to use three partitions (or some other value) so that there's a gap in the ids.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea. |
||
| [(0, 0), (2, 1), (1, 2), (3, 3)] | ||
| """ | ||
| n = self.getNumPartitions() | ||
|
|
||
| def func(k, it): | ||
| for i, v in enumerate(it): | ||
| yield i * n + k, v | ||
|
|
||
| return self.mapPartitionsWithIndex(func) | ||
|
|
||
| def name(self): | ||
| """ | ||
| Return the name of this RDD. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the best example because it's not clear which element is the item and which element is its index. In the Scala API, this is clear from the method's return type. Maybe we should update the documentation to explicitly state that the second element is the id (like the Scala API).
I think this implementation has things backwards w.r.t. the Scala one:
versus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will change it.