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
457bcd33
Commit
457bcd33
authored
11 years ago
by
Andre Schumacher
Browse files
Options
Downloads
Patches
Plain Diff
PySpark: implementing subtractByKey(), subtract() and keyBy()
parent
baa84e7e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
python/pyspark/rdd.py
+37
-0
37 additions, 0 deletions
python/pyspark/rdd.py
with
37 additions
and
0 deletions
python/pyspark/rdd.py
+
37
−
0
View file @
457bcd33
...
...
@@ -754,6 +754,43 @@ class RDD(object):
"""
return
python_cogroup
(
self
,
other
,
numPartitions
)
def
subtractByKey
(
self
,
other
,
numPartitions
=
None
):
"""
Return each (key, value) pair in C{self} that has no pair with matching key
in C{other}.
>>>
x
=
sc
.
parallelize
([(
"
a
"
,
1
),
(
"
b
"
,
4
),
(
"
b
"
,
5
),
(
"
a
"
,
2
)])
>>>
y
=
sc
.
parallelize
([(
"
a
"
,
3
),
(
"
c
"
,
None
)])
>>>
sorted
(
x
.
subtractByKey
(
y
).
collect
())
[(
'
b
'
,
4
),
(
'
b
'
,
5
)]
"""
filter_func
=
lambda
tpl
:
len
(
tpl
[
1
][
0
])
>
0
and
len
(
tpl
[
1
][
1
])
==
0
map_func
=
lambda
tpl
:
[(
tpl
[
0
],
val
)
for
val
in
tpl
[
1
][
0
]]
return
self
.
cogroup
(
other
,
numPartitions
).
filter
(
filter_func
).
flatMap
(
map_func
)
def
subtract
(
self
,
other
,
numPartitions
=
None
):
"""
Return each value in C{self} that is not contained in C{other}.
>>>
x
=
sc
.
parallelize
([(
"
a
"
,
1
),
(
"
b
"
,
4
),
(
"
b
"
,
5
),
(
"
a
"
,
3
)])
>>>
y
=
sc
.
parallelize
([(
"
a
"
,
3
),
(
"
c
"
,
None
)])
>>>
sorted
(
x
.
subtract
(
y
).
collect
())
[(
'
a
'
,
1
),
(
'
b
'
,
4
),
(
'
b
'
,
5
)]
"""
rdd
=
other
.
map
(
lambda
x
:
(
x
,
True
))
# note: here 'True' is just a placeholder
return
self
.
map
(
lambda
x
:
(
x
,
True
)).
subtractByKey
(
rdd
).
map
(
lambda
tpl
:
tpl
[
0
])
# note: here 'True' is just a placeholder
def
keyBy
(
self
,
f
):
"""
Creates tuples of the elements in this RDD by applying C{f}.
>>>
x
=
sc
.
parallelize
(
range
(
0
,
3
)).
keyBy
(
lambda
x
:
x
*
x
)
>>>
y
=
sc
.
parallelize
(
zip
(
range
(
0
,
5
),
range
(
0
,
5
)))
>>>
sorted
(
x
.
cogroup
(
y
).
collect
())
[(
0
,
([
0
],
[
0
])),
(
1
,
([
1
],
[
1
])),
(
2
,
([],
[
2
])),
(
3
,
([],
[
3
])),
(
4
,
([
2
],
[
4
]))]
"""
return
self
.
map
(
lambda
x
:
(
f
(
x
),
x
))
# TODO: `lookup` is disabled because we can't make direct comparisons based
# on the key; we need to compare the hash of the key to the hash of the
# keys in the pairs. This could be an expensive operation, since those
...
...
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