forked from databricks/learning-spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicMapPartitions.py
More file actions
35 lines (28 loc) · 941 Bytes
/
Copy pathBasicMapPartitions.py
File metadata and controls
35 lines (28 loc) · 941 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
30
31
32
33
34
35
"""
>>> from pyspark.context import SparkContext
>>> sc = SparkContext('local', 'test')
>>> b = sc.parallelize(["KK6JKQ", "Ve3UoW", "kk6jlk", "W6BB"])
>>> fetchCallSigns(b).size()
4
"""
import sys
import urllib3
from pyspark import SparkContext
def processCallSigns(signs):
"""Process call signs"""
http = urllib3.PoolManager()
requests = map(
lambda x: http.request('GET', "http://qrzcq.com/call/" + x), signs)
return map(lambda x: x.data, requests)
def fetchCallSigns(input):
"""Fetch call signs"""
return input.mapPartitions(lambda callSigns: processCallSigns(callSigns))
if __name__ == "__main__":
master = "local"
if len(sys.argv) == 2:
master = sys.argv[1]
sc = SparkContext(master, "BasicMapPartitions")
input = sc.parallelize(["KK6JKQ", "Ve3UoW", "kk6jlk", "W6BB"])
output = sorted(fetchCallSigns(input).collect())
for str in output:
print "%s " % (str)