Filter Twitter in Real Time Using v2 API
Updated: Aug 6, 2023
This example shows how to leverage the Twitter API v2 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 when building applications that monitor and analyze real-time Twitter data for various purposes, such as sentiment analysis, trend tracking, or social media monitoring.
Java Code Listing
package com.northconcepts.datapipeline.examples.twitter2; import com.northconcepts.datapipeline.core.DataWriter; import com.northconcepts.datapipeline.core.StreamWriter; import com.northconcepts.datapipeline.job.Job; import com.northconcepts.datapipeline.twitter2.TwitterFilterStreamReader; public class FilterTwitterInRealTimeUsingV2 { private static final String API_KEY = "YOUR_API_KEY"; private static final String API_SECRET = "YOUR_API_SECRET"; private static final String BEARER_TOKEN = "YOUR_BEARER_TOKEN"; public static void main(String[] args) { TwitterFilterStreamReader reader = new TwitterFilterStreamReader(API_KEY, API_SECRET, BEARER_TOKEN); reader.addRule("cat has:images", "cat images"); DataWriter writer = new StreamWriter(System.out); Job.run(reader, writer); } }
Code Walkthrough
- First, the following Twitter API access parameters are introduced as constant class variables: API_KEY, API_SECRET, BEARER_TOKEN.
- TwitterFilterStreamReader instance is created to read filtered Tweets in real time based on a set of filter rules.
- Rules for filtration are specified in the next step using
addRule()
method. In the given example, only cat images are selected. - Job.run(reader, writer) is used to transfer the data from the
reader
toStreamWriter(System.out)
. See how to compile and run data pipeline jobs.
Console Output
The Tweets with cat images are printed on the console.