-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka_producer.py
39 lines (29 loc) · 993 Bytes
/
kafka_producer.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
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler, Stream
from kafka import SimpleProducer, KafkaClient
import json
from twitter_auth import ACCESS_TOKEN, TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET
# Twitter Authentication
authentication = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
authentication.set_access_token(ACCESS_TOKEN, TOKEN_SECRET)
# Twitter Stream Listener
HASHTAG = "bigdata" # track hashtag
LOCALHOST = "localhost:9092" # connect to
TOPIC = "twitter" # filter by topic
# create Kafka Listener
class KafkaListener(StreamListener):
def on_data(self, data):
producer.send_messages(TOPIC, data.encode('utf-8'))
print(data)
return True
def on_error(self, status):
print(status)
# define client
client = KafkaClient(LOCALHOST)
# define producer
producer = SimpleProducer(client)
# define listener
listener = KafkaListener()
# set stream & track HASHTAG
stream = Stream(authentication, listener)
stream.filter(track=HASHTAG)