Filter Twitter in Real Time

This example leverages the Twitter API to retrieve and process tweets in real time, allowing you to apply custom filters and rules to obtain specific, relevant tweets.

This can be used in social media analytics platforms, sentiment analysis tools, and news aggregators that require real-time access to Twitter data. It enables you to extract and process tweets in real time, providing valuable insights and timely information for your applications and services.

 

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.twitter.TwitterFilterStreamReader;
import com.northconcepts.datapipeline.twitter.TwitterStreamReader;

public class FilterTwitterInRealTime {

    public static void main(String[] args) {
        
        // Your Twitter API keys and secret
        final String CONSUMER_KEY = "**********";
        final String CONSUMER_SECRET = "**********";
        final String ACCESS_KEY = "**********";
        final String ACCESS_TOKEN_SECRET = "**********";

        TwitterStreamReader reader = new TwitterFilterStreamReader(
                CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_TOKEN_SECRET)
                .setTrack("#ManUtd", "#ManchesterUnited");

        Job.run(reader,new StreamWriter(System.out));
    }
}

 

Code Walkthrough

  1. First, the following Twitter API access parameters are introduced as constant variables: CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_TOKEN_SECRET.
  2. TwitterFilterStreamReader instance is created to read filtered Tweets in real time based on a set of filter rules.
  3. Tracks for filtration are specified in the next step using setTrack() method. In the given example, tweets with #ManUtd, #ManchesterUnited hashtags are selected.
  4. Job.run(reader, writer) is used to transfer the data from the reader to StreamWriter(System.out). See how to compile and run data pipeline jobs. 

 

Console Output

The Tweets with the required hashtags are printed on the console.

Mobile Analytics