- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamingTweetCount.py
More file actions
Latest commit
65 lines (52 loc) · 2.25 KB
/
Copy pathStreamingTweetCount.py
File metadata and controls
65 lines (52 loc) · 2.25 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
__author__='Akarsh'
fromtweepyimportStream
fromtweepyimportOAuthHandler
fromtweepy.streamingimportStreamListener
importjson
importio
frompysparkimportSparkContext
frompyspark.streamingimportStreamingContext
importthreading
importtime
#consumer key, consumer secret, access token, access secret.
ckey="0jFyRU3DZsmcnuNSfXwxX5S7O"
csecret="HQ3P8llrooT5vqJhWYxXxtRGs5FmQ2tfidsaUDrWgBfbolq5D5"
atoken="38135704-1mHF4pypfXQazuyVRqCc09Do8nfLBct1EI6ZpTy3c"
asecret="5OZ7ndGqPOGPQzJM2voGcPh03hgUFXWxtP8yu27p2zIJX"
classlistener(StreamListener):
defon_data(self, data):
withio.open("input.txt", "a+") asfile:
file.write(data)
return(True)
defon_error(self, status):
print (status)
defdoTwitterStreaming():
auth=OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream=Stream(auth, listener())
twitterStream.filter(track=["breaking"])
start_time=time.time() #grabs the system time
keyword_list= ['twitter'] #track list
t=threading.Thread(target=doTwitterStreaming)
try:
t.start()
sc=SparkContext(appName="Comp6360Project")
ssc=StreamingContext(sc, 10)
lines=ssc.textFileStream("input.txt")
words=lines.filter(lambdaline : line!='').flatMap(lambdaline: json.loads(line)["text"].split(" "))\
.map(lambdax: (x, 1))\
.reduceByKey(lambdaa, b: a+b).map(lambdax: (x[1], x[0]))\
.transform(lambdardd: rdd.sortByKey(False)).map(lambdax: (x[1], x[0]))
langs=lines.filter(lambdaline: line!='').map(lambdaline: (json.loads(line)["lang"],1))\
.reduceByKey(lambdaa, b: a+b).map(lambdax: (x[1], x[0]))\
.transform(lambdardd: rdd.sortByKey(False)).map(lambdax: (x[1], x[0]))
locs=lines.filter(lambdaline: line!='').map(lambdaline: (json.loads(line)["user"]["location"],1))\
.reduceByKey(lambdaa, b: a+b).map(lambdax: (x[1], x[0]))\
.transform(lambdardd: rdd.sortByKey(False)).map(lambdax: (x[1], x[0]))
words.saveAsTextFiles("output/word","")
langs.saveAsTextFiles("output/langs","")
locs.saveAsTextFiles("output/locs","")
ssc.start()
ssc.awaitTermination()
finally:
print("end")