-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspark_meetup.py
52 lines (29 loc) · 1.23 KB
/
spark_meetup.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from __future__ import print_function
import json
import sys
from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils
f_count = 0;
def country_count_update(newValue, existingCount):
if existingCount is None:
existingCount = 0
return sum(newValue)+existingCount
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: spark_meetup.py <zk> <topic>", file=sys.stderr)
exit(-1)
sc = SparkContext("local[2]",appName="PythonStreamingMeetup")
sc.setLogLevel("WARN")
ssc = StreamingContext(sc, 1)
ssc.checkpoint("checkpoint")
zkQuorum, topic = sys.argv[1:]
kvs = KafkaUtils.createStream(ssc, zkQuorum, "1", {topic: 1})
rsvps = kvs.map(lambda x: x[1])
rsvps_json = rsvps.map(lambda x: json.loads(x.encode('ascii','ignore')))
rsvps_us = rsvps_json.filter(lambda x: x['group']['group_country']=='us')
rsvps_city_pair = rsvps_us.map(lambda x: (x['group']['group_city'],1))
rsvps_city_statefulCount = rsvps_city_pair.updateStateByKey(country_count_update)
rsvps_city_statefulCount.pprint()
ssc.start()
ssc.awaitTermination()