Search Twitter for Tweets Using v2 API

Updated: Jun 4, 2023

This example shows you how you can use Data Pipeline's TwitterSearchReader to search Twitter for Tweets using Twitter v2 API.

In this demo code you are going to use OAuth 1.0a as an authentication. This standard allows you to make an API call on behalf of a Twitter user by specifying Access Token and Secret.

package com.northconcepts.datapipeline.examples.twitter2;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.twitter2.TwitterSearchReader;

public class SearchTwitterForTweetsUsingV2 {

    private static final String API_KEY = "YOUR_API_KEY";
    private static final String API_SECRET = "YOUR_API_SECRET";
    private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
    private static final String ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET";
    private static final String SEARCH_KEYWORD = "Corona";

    public static void main(String[] args) {
        DataReader reader = new TwitterSearchReader(API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET, SEARCH_KEYWORD);
        DataWriter writer = new StreamWriter(System.out);, writer);

Code Walkthrough

  1. First, parameters which are going to be used to connect to Twitter v2 API are specified.
  2. TwitterSearchReader is created to search Twitter for tweets with a search keyword Corona.
  3. StreamWriter is created to stream records to the writer specified in the parameter(i.e in this example).
  4. Data are transferred from TwitterSearchReader to the console via method. See how to compile and run data pipeline jobs.


Uses the Twitter v2 API to obtain records of Tweets posted over the last 7 days that match a search criteria. It extends AbstractTwitterReader class and it's constructor takes API_Key, API_Secrete, Access_Token, Access_Secrete and a searchQuery.

Console output

Obtained records will printed on the console.

Mobile Analytics