Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
spark
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cs525-sp18-g07
spark
Commits
1ad1331a
Commit
1ad1331a
authored
12 years ago
by
Reynold Xin
Browse files
Options
Downloads
Patches
Plain Diff
Added MapPartitionsWithSplitRDD.
parent
58eb44ac
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/src/main/scala/spark/RDD.scala
+18
-0
18 additions, 0 deletions
core/src/main/scala/spark/RDD.scala
core/src/test/scala/spark/RDDSuite.scala
+5
-0
5 additions, 0 deletions
core/src/test/scala/spark/RDDSuite.scala
with
23 additions
and
0 deletions
core/src/main/scala/spark/RDD.scala
+
18
−
0
View file @
1ad1331a
...
...
@@ -196,6 +196,9 @@ abstract class RDD[T: ClassManifest](@transient sc: SparkContext) extends Serial
def
mapPartitions
[
U:
ClassManifest
](
f
:
Iterator
[
T
]
=>
Iterator
[
U
])
:
RDD
[
U
]
=
new
MapPartitionsRDD
(
this
,
sc
.
clean
(
f
))
def
mapPartitionsWithSplit
[
U:
ClassManifest
](
f
:
(
Int
,
Iterator
[
T
])
=>
Iterator
[
U
])
:
RDD
[
U
]
=
new
MapPartitionsWithSplitRDD
(
this
,
sc
.
clean
(
f
))
// Actions (launch a job to return a value to the user program)
def
foreach
(
f
:
T
=>
Unit
)
{
...
...
@@ -417,3 +420,18 @@ class MapPartitionsRDD[U: ClassManifest, T: ClassManifest](
override
val
dependencies
=
List
(
new
OneToOneDependency
(
prev
))
override
def
compute
(
split
:
Split
)
=
f
(
prev
.
iterator
(
split
))
}
/**
* A variant of the MapPartitionsRDD that passes the split index into the
* closure. This can be used to generate or collect partition specific
* information such as the number of tuples in a partition.
*/
class
MapPartitionsWithSplitRDD
[
U:
ClassManifest
,
T:
ClassManifest
](
prev
:
RDD
[
T
],
f
:
(
Int
,
Iterator
[
T
])
=>
Iterator
[
U
])
extends
RDD
[
U
](
prev
.
context
)
{
override
def
splits
=
prev
.
splits
override
val
dependencies
=
List
(
new
OneToOneDependency
(
prev
))
override
def
compute
(
split
:
Split
)
=
f
(
split
.
index
,
prev
.
iterator
(
split
))
}
This diff is collapsed.
Click to expand it.
core/src/test/scala/spark/RDDSuite.scala
+
5
−
0
View file @
1ad1331a
...
...
@@ -29,6 +29,11 @@ class RDDSuite extends FunSuite with BeforeAndAfter {
assert
(
nums
.
glom
().
map
(
_
.
toList
).
collect
().
toList
===
List
(
List
(
1
,
2
),
List
(
3
,
4
)))
val
partitionSums
=
nums
.
mapPartitions
(
iter
=>
Iterator
(
iter
.
reduceLeft
(
_
+
_
)))
assert
(
partitionSums
.
collect
().
toList
===
List
(
3
,
7
))
val
partitionSumsWithSplit
=
nums
.
mapPartitionsWithSplit
{
case
(
split
,
iter
)
=>
Iterator
((
split
,
iter
.
reduceLeft
(
_
+
_
)))
}
assert
(
partitionSumsWithSplit
.
collect
().
toList
===
List
((
0
,
3
),
(
1
,
7
)))
}
test
(
"SparkContext.union"
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment