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
143ef4f9
Commit
143ef4f9
authored
12 years ago
by
Matei Zaharia
Browse files
Options
Downloads
Patches
Plain Diff
Added a CoalescedRDD class for reducing the number of partitions in an RDD.
parent
c45758dd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/src/main/scala/spark/CoalescedRDD.scala
+44
-0
44 additions, 0 deletions
core/src/main/scala/spark/CoalescedRDD.scala
core/src/test/scala/spark/RDDSuite.scala
+31
-0
31 additions, 0 deletions
core/src/test/scala/spark/RDDSuite.scala
with
75 additions
and
0 deletions
core/src/main/scala/spark/CoalescedRDD.scala
0 → 100644
+
44
−
0
View file @
143ef4f9
package
spark
private
class
CoalescedRDDSplit
(
val
index
:
Int
,
val
parents
:
Array
[
Split
])
extends
Split
/**
* Coalesce the partitions of a parent RDD (`prev`) into fewer partitions, so that each partition of
* this RDD computes zero or more of the parent ones. Will produce exactly `maxPartitions` if the
* parent had more than this many partitions, or fewer if the parent had fewer.
*
* This transformation is useful when an RDD with many partitions gets filtered into a smaller one,
* or to avoid having a large number of small tasks when processing a directory with many files.
*/
class
CoalescedRDD
[
T:
ClassManifest
](
prev
:
RDD
[
T
],
maxPartitions
:
Int
)
extends
RDD
[
T
](
prev
.
context
)
with
Logging
{
@transient
val
splits_
:
Array
[
Split
]
=
{
val
prevSplits
=
prev
.
splits
if
(
prevSplits
.
length
<
maxPartitions
)
{
prevSplits
.
zipWithIndex
.
map
{
case
(
s
,
idx
)
=>
new
CoalescedRDDSplit
(
idx
,
Array
(
s
))
}
}
else
{
(
0
until
maxPartitions
).
map
{
i
=>
val
rangeStart
=
(
i
*
prevSplits
.
length
)
/
maxPartitions
val
rangeEnd
=
((
i
+
1
)
*
prevSplits
.
length
)
/
maxPartitions
new
CoalescedRDDSplit
(
i
,
prevSplits
.
slice
(
rangeStart
,
rangeEnd
))
}.
toArray
}
}
override
def
splits
=
splits_
override
def
compute
(
split
:
Split
)
:
Iterator
[
T
]
=
{
split
.
asInstanceOf
[
CoalescedRDDSplit
].
parents
.
iterator
.
flatMap
{
parentSplit
=>
prev
.
iterator
(
parentSplit
)
}
}
val
dependencies
=
List
(
new
NarrowDependency
(
prev
)
{
def
getParents
(
id
:
Int
)
:
Seq
[
Int
]
=
splits
(
id
).
asInstanceOf
[
CoalescedRDDSplit
].
parents
.
map
(
_
.
index
)
}
)
}
This diff is collapsed.
Click to expand it.
core/src/test/scala/spark/RDDSuite.scala
+
31
−
0
View file @
143ef4f9
...
...
@@ -71,4 +71,35 @@ class RDDSuite extends FunSuite with BeforeAndAfter {
val
rdd
=
sc
.
makeRDD
(
Array
(
1
,
2
,
3
,
4
),
2
).
flatMap
(
x
=>
1
to
x
).
checkpoint
()
assert
(
rdd
.
collect
().
toList
===
List
(
1
,
1
,
2
,
1
,
2
,
3
,
1
,
2
,
3
,
4
))
}
test
(
"coalesced RDDs"
)
{
sc
=
new
SparkContext
(
"local"
,
"test"
)
val
data
=
sc
.
parallelize
(
1
to
10
,
10
)
val
coalesced1
=
new
CoalescedRDD
(
data
,
2
)
assert
(
coalesced1
.
collect
().
toList
===
(
1
to
10
).
toList
)
assert
(
coalesced1
.
glom
().
collect
().
map
(
_
.
toList
).
toList
===
List
(
List
(
1
,
2
,
3
,
4
,
5
),
List
(
6
,
7
,
8
,
9
,
10
)))
// Check that the narrow dependency is also specified correctly
assert
(
coalesced1
.
dependencies
.
head
.
getParents
(
0
).
toList
===
List
(
0
,
1
,
2
,
3
,
4
))
assert
(
coalesced1
.
dependencies
.
head
.
getParents
(
1
).
toList
===
List
(
5
,
6
,
7
,
8
,
9
))
val
coalesced2
=
new
CoalescedRDD
(
data
,
3
)
assert
(
coalesced2
.
collect
().
toList
===
(
1
to
10
).
toList
)
assert
(
coalesced2
.
glom
().
collect
().
map
(
_
.
toList
).
toList
===
List
(
List
(
1
,
2
,
3
),
List
(
4
,
5
,
6
),
List
(
7
,
8
,
9
,
10
)))
val
coalesced3
=
new
CoalescedRDD
(
data
,
10
)
assert
(
coalesced3
.
collect
().
toList
===
(
1
to
10
).
toList
)
assert
(
coalesced3
.
glom
().
collect
().
map
(
_
.
toList
).
toList
===
(
1
to
10
).
map
(
x
=>
List
(
x
)).
toList
)
// If we try to coalesce into more partitions than the original RDD, it should just
// keep the original number of partitions.
val
coalesced4
=
new
CoalescedRDD
(
data
,
20
)
assert
(
coalesced4
.
collect
().
toList
===
(
1
to
10
).
toList
)
assert
(
coalesced4
.
glom
().
collect
().
map
(
_
.
toList
).
toList
===
(
1
to
10
).
map
(
x
=>
List
(
x
)).
toList
)
}
}
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