Machine Learning & Big Data Blog

Working with Streaming Twitter Data Using Kafka

Walker Rowe
3 minute read
Walker Rowe

Here we show how to read messages streaming from Twitter and store them in Kafka.  In Part 2 we will show how to retrieve those messages from Kafka and read them into Spark Streaming.


People use Twitter data for all kinds of business purposes, like monitoring brand awareness.  Twitter, unlike Facebook, provides this data freely.  So you can use that and store it in a big data database so that you can run analytics over it.  You could, for example, make a graph of currently trending topics.

Since this data coming is as a stream, it makes sense to process it with a streaming product, like Apache Spark Streaming.  That keeps data in memory without writing it to storage, unless you want to.  But streaming data has value when it is live, i.e., streaming.  So there would not be much reason to store that data permanently to some place like Hadoop.

Kafka is the tool most people use to read streaming data like this.  It follows a publish-subscribe model where you write messages (publish) and read them (subscribe).  Messages are grouped into topics. As messages are consumed, they are removed from Kafka.

Now, here is our example.


  • Python
  • Kafka
  • Twitter API credentials


  1. Create an App on the Twitter API website. Basically that will give you keys that you need to use the Twitter API.
  2. Then install Kafka. It’s as simple as downloading and unzipping it.
  3. Install Install kafka-python and twitter-python:
    pip install kafka-python
    pip install python-twitter
    pip install tweepy
  4. Start Zooper and Kafka from the Kafka install directory:
    bin/ config/
    bin/ config/
  5. Create a topic. We will create the topic “trump” as obviously there are a lot of Tweets about the President.
     bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic trump
  6. Fill in the access keys you got from your Twitter API account and add them to this code below.
    from tweepy.streaming import StreamListener
    from tweepy import OAuthHandler
    from tweepy import Stream
    from kafka import SimpleProducer, KafkaClient
    access_token = "(get your own)"
    access_token_secret =  "(get your own)"
    consumer_key =  "(get your own)"
    consumer_secret =  "(get your own)"
    class StdOutListener(StreamListener):
        def on_data(self, data):
            producer.send_messages("trump", data.encode('utf-8'))
            print (data)
            return True
        def on_error(self, status):
            print (status)
    kafka = KafkaClient("localhost:9092")
    producer = SimpleProducer(kafka)
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)
  7. Now run it. Not only will it write messages to the screen, it will publish them to Kafka. The messages are in JSON format and will look something like:
    _name":null,"user":{"id":32871086,"id_str":"32871086","name":"Kyle Griffin","screen_name":"kylegriffin1","location":
    "Manhattan, NY","url":"http:\/\/\/the-last-word","description":"Producer. MSNBC's @TheLastWord. Honorary Aussie. Opinions mine. Please clap.","protected":false,"verified":true,"followers
    "created_at":"Sat Apr 18 12:45:48 +0000 2009","utc_offset":-14400,"time_zone":"Eastern Time (US & Canada)","geo_enabled":true,"lang":"en",
  8. You can test that topics are getting published in Kafka by using:
    bin/ --bootstrap-server localhost:9092 --topic trump --from-beginning
  9. It should echo the same output.

In a subsequent post we will show how to retrieve these messages from Kafka and read them into Apache Spark.

Wikibon: Automate your Big Data pipeline

Learn how data management experts throughout the industry are transforming their Big Data infrastructure for maximum business impact.
Download Now ›

These postings are my own and do not necessarily represent BMC's position, strategies, or opinion.

See an error or have a suggestion? Please let us know by emailing

About the author

Walker Rowe

Walker Rowe

Walker Rowe is an American freelance tech writer and programmer living in Tunisia. He specializes in big data, analytics, and programming languages. Find him on LinkedIn or Upwork.