How to Search Tweets using Twitter4j Java API

In a recent SEO related project, I used Twitter4J Java API to download 3 million plus tweets. Below is the proof of concept code. There are only three major steps:

1. Download the latest Twitter4J JAR file from and add into your project library or class path.

2. Create Twitter App by visting Click Create New App button and fill the form by providing app name, URL and app brief description and submit clicking on "Create New Twitter Applicaton" button.

Once the application is created, click the application name on dashboard and go to Key and Access Toekn tab. There would see Consumer Key (API Key), Consumer Secret (API Secret). Click on Generate Access Token and Token secret button, it would generate Access Token and Access Token Secret. These 4 things are what you need when making the API call.

3. Just use below code, as-is, it shall fetch the tweets. Modify the code as per your requirements. Its self explanatory, if you have any questions, feel free to ask under commends. Here is the code sample:


import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

import java.util.ArrayList;
import java.util.List;

public class TweetsFetcher {
    public static final int TWEETS_COUNT = 20;
    public static final String TWITTER_QUERY_FILTERS = " -filter:retweets AND -filter:replies AND -filter:images AND -filter:videos";
    public static final String NAME = ""; // TWITTER account
    public static final String CONSUMER_KEY = "write consumer key here ";
    public static final String CONSUMER_SECRET = "write consume secret here";
    public static final String ACCESS_TOKEN = "write access token here";
    public static final String ACCESS_TOKEN_SECRET = "write access token secret here";
    public static final Twitter TWITTER;

    static {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        TwitterFactory tf = new TwitterFactory(;
        TWITTER = tf.getInstance();

    public static void main(String[] args) {
        TweetsFetcher tweetsFetcher = new TweetsFetcher();
        List statusList = tweetsFetcher.downloadTweets(" care ");
        for(Status status : statusList) {
            System.out.println(status.getId() + " | " + status.getUser().getScreenName() + " | " + status.getText());

    public List downloadTweets(String keyword) {
        List statuses = new ArrayList<>();
        try {
            String queryString = keyword + TWITTER_QUERY_FILTERS;
            Query query = new Query(queryString);
            QueryResult result =;
            statuses = result.getTweets();
            return statuses;
        } catch (TwitterException e) {
        return statuses;


When I run the code, below is the list of tweets returned by API.

894122656773152770 | thelandee | we don't care
894122646656385024 | benjochannon | I Don't Know How To Explain To You That You Should Care About Other People
894122644609650689 | nabilahazmie | Be who you want to be and not care about what others think 💙
894122640264151041 | nydafarah | The less i know the less I'll care.
894122638812913664 | smilesmileposi | You don't have to know exactly how to make people feel better. At least showing them you care will help!
894122630164299777 | peythepotatoooo | only those who care a'right
894122629900140546 | AtiliusRegulus | Actually zero people care about your "bi-erasure," Aaron Carter trend. Sickos are sickos.
894122627354296320 | BullMelford | The Bull can take care of your complete old English #wedding day, from the ceremony to the reception. Tweet us today! #LongMelford #Suffolk
894122597524398080 | MsBoss48536280 | LOL trust me fetty do not care about them talking about masika kalysha because that is not his ex that's his sister-in-law
894122589664272384 | HaileyOliver5 | Holy fuck I don't care about ANY of these we fest, ice fest or harbour fest snaps my god u r all annoying
894122583695605760 | liberty201755 | But i would look because anyone calling showing what federalism and lack of care gets you, anti social, is trying to hide something
894122582429114368 | rvmemberme | sans surprise he doesn't care at all
894122573633552384 | glcnigeria | 🎵Hands in the air now, wave it like you don't care...🎵

894122572467621888 | andrew4mk | Further signs of abuses in Venezuela. Don't care if you are right or left. This is wrong. All should speak out
894122559238807552 | CllrBSilvester | Retweeted Annie Greek UK lover (@annie_ioanna):

I don't care if you are white, brown, yellow, blue or purple..if...
894122541203300352 | chyato | Anyone care for Yoko's original Taiwanese pork leg stew (豬滷腳)? [偷笑]
894122525596291072 | MrWoodo | They do it just to make you loudly tell everyone in the room how much "you don't care but..." you gammon-faced old…
894122523813720064 | GonzalezMaryssa | If I can't tell you how I really feel about you or about something you said or did... I just don't care about you.
894122518369456128 | ixvs3 | I care too much. That's how I get hurt.

There is lot of other information about tweet that you can get using other getter methods of Status class e.g. user id, date, etc.


  1. i am such a dummy when it comes to dealing with these kind of programs or systems. this post has helped me a great deal and i am so thankful for it. keep posting

  2. Woah, how hard this code is. I really got astonished to see that this code is quite difficult and hard to understand. Great effort you have put on.


Post a Comment