forked from databricks/learning-spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicKeyValueMapFilter.py
More file actions
29 lines (24 loc) · 850 Bytes
/
Copy pathBasicKeyValueMapFilter.py
File metadata and controls
29 lines (24 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
>>> from pyspark.context import SparkContext
>>> sc = SparkContext('local', 'test')
>>> input = ["coffee", "i really like coffee", "coffee > magic"]
>>> b = sc.parallelize(input)
>>> sorted(basicKeyValueMapFilter(b).collect())
[4, 9]
"""
import sys
from pyspark import SparkContext
def basicKeyValueMapFilter(input):
"""Construct a key/value RDD and then filter on the value"""
return input.map(lambda x: (x.split(" ")[0], x)).filter(
lambda x: len(x[1]) < 20)
if __name__ == "__main__":
master = "local"
if len(sys.argv) == 2:
master = sys.argv[1]
sc = SparkContext(master, "BasicFilterMap")
input = sc.parallelize(
["coffee", "i really like coffee", "coffee > magic", "panda < coffee"])
output = sorted(basicKeyValueMapFilter(input).collect())
for elem in output:
print elem