-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtwitterStream.py
More file actions
129 lines (99 loc) · 4.12 KB
/
twitterStream.py
File metadata and controls
129 lines (99 loc) · 4.12 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from pyspark import SparkConf, SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils
import operator
import numpy as np
import matplotlib.pyplot as plt
# Declaring conf and sc as global variable to use in other functions
conf = SparkConf().setMaster("local[2]").setAppName("Streamer")
sc = SparkContext(conf=conf)
def main():
# conf = SparkConf().setMaster("local[2]").setAppName("Streamer")
#sc = SparkContext(conf=conf)
ssc = StreamingContext(sc, 10) # Create a streaming context with batch interval of 10 sec
ssc.checkpoint("checkpoint")
pwords = load_wordlist("positive.txt")
nwords = load_wordlist("negative.txt")
counts = stream(ssc, pwords, nwords, 100)
make_plot(counts)
def make_plot(counts):
"""
Plot the counts for the positive and negative words for each timestep.
Use plt.show() so that the plot will popup.
"""
for count in counts:
if len(count) ==1:
if count[0][0] == "positive":
count.append(("negative",0))
else:
count.insert(0,("positive",0))
elif len(count) ==0:
count.insert(0,("positive",0))
count.append(("negative",0))
#print counts
positive = []
negative = []
for count in counts:
positive.append(count[0][1])
negative.append(count[1][1])
maximum = max(max(positive),max(negative)) + 75
xlen = len(positive)+1
plt.plot(positive, color = "b", marker="o", markerfacecolor="b")
plt.plot(negative, color = "g", marker="o", markerfacecolor="g")
plt.xticks(np.arange(-1, len(positive)+1, 1))
xticks = plt.gca().xaxis.get_major_ticks()
xticks[0].set_visible(False)
plt.yticks(np.arange(0, maximum, 25))
plt.xlabel('Time step')
plt.ylabel('Word count')
plt.legend(['positive', 'negative'], loc='upper left')
plt.savefig("plot.png")
plt.show()
# YOUR CODE HERE
def load_wordlist(filename):
"""
This function should return a list or set of words from the given filename.
"""
#conf = SparkConf().setMaster("local[2]").setAppName("Streamer")
#sc = SparkContext(conf=conf)
rdd = sc.textFile(filename)
return set(rdd.collect())
# YOUR CODE HERE
def fx(word,pwords,nwords):
if word in pwords:
return "positive"
elif word in nwords:
return "negative"
else:
return None
def updateFunction(newValues, runningCount):
if runningCount is None:
runningCount = 0
return sum(newValues, runningCount)
def stream(ssc, pwords, nwords, duration):
kstream = KafkaUtils.createDirectStream(
ssc, topics = ['twitterstream'], kafkaParams = {"metadata.broker.list": 'localhost:9092'})
tweets = kstream.map(lambda x: x[1].encode("ascii","ignore"))
words = tweets.flatMap(lambda line: line.split(" "))
pairs = words.map(lambda word: (fx(word,pwords,nwords), 1)).filter(lambda x: x[0]=="positive" or x[0] == "negative")
wordCounts = pairs.reduceByKey(lambda x, y: x + y)
#wordCounts = wordCounts.filter(lambda x: x[0]=="positive" or x[0] == "negative")
running_counts = pairs.updateStateByKey(updateFunction)
running_counts.pprint()
# Print the first ten elements of each RDD generated in this DStream to the console
# Each element of tweets will be the text of a tweet.
# You need to find the count of all the positive and negative words in these tweets.
# Keep track of a running total counts and print this at every time step (use the pprint function).
# YOUR CODE HERE
# Let the counts variable hold the word counts for all time steps
# You will need to use the foreachRDD function.
# For our implementation, counts looked like:
# [[("positive", 100), ("negative", 50)], [("positive", 80), ("negative", 60)], ...]
counts = []
wordCounts.foreachRDD(lambda t,rdd: counts.append(rdd.collect()))
ssc.start() # Start the computation
ssc.awaitTerminationOrTimeout(duration)
ssc.stop(stopGraceFully=True)
return counts
if __name__=="__main__":
main()