Wednesday, September 23, 2015

How to do data analysis from twitter using JAVA Code.

Data mining and analysis is most happening concept right now in the IT market. Company want to first gather the information from social site like Facebook, Twitter etc to do data analysis and get the market trend before developing any software.

In case of Twitter their are ready made API available that help us to do the Data analysis or study the market trand.

We can down load the Twitter api from site

We can us following code to

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

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

public class TweetManager {

    public static ArrayList getTweets(String topic) {

        Twitter twitter = new TwitterFactory().getInstance();
        ArrayList tweetList = new ArrayList();
        try {
            Query query = new Query(topic);
            QueryResult result;
            do {
                result =;
                List tweets = result.getTweets();
                for (Status tweet : tweets) {
            } while ((query = result.nextQuery()) != null);
        } catch (TwitterException te) {
            System.out.println("Failed to search tweets: " + te.getMessage());
        return tweetList;

import java.util.ArrayList;
import java.util.Iterator;

public class SiddhuMainClass {

public static void main(String args[])
TweetManager obTweetManager = new TweetManager();
ArrayList objArrayResult = obTweetManager.getTweets("dhumale");
Iterator iterator = objArrayResult.iterator();
while (iterator.hasNext()) {


Out put :

>>>>>>>>>>>>Shooting #shotgun #beretta #thuglife #madeindorset #rah @ Axnoller - Dorset
>>>>>>>>>>>>@beetlejuiceltd #looemusicfestival @ Looe Music Festival
>>>>>>>>>>>>View from the queue #looemusicfestival #beetlejuice #cocktails @beetlejuiceltd @ Looe Music Festival
>>>>>>>>>>>>Crazy weekend at Looe Music Festival! #cocktails #dreamteam #beetlejuice #looemusicfestival…
>>>>>>>>>>>>Fal Week 2014 #throwback #falmouth #summer #beetlejuice #barmen #dreamteam #cocktails
>>>>>>>>>>>>@narendramodi , happy b'day sir....  May god fullfill your all wishesh and ours too..
>>>>>>>>>>>>2nd family #cornwall #friends #squad #rock #jack #goodbyesummer
>>>>>>>>>>>>email reply from staff admin to me..

To Remo Dhumale Sep 15 at 12:41 PM
Dear Sir,

Note: To execute the programe make sure to add all *.jar files that we had downloaded from the above link.

Site also contain some of the inbuild example for
- post a Tweet
- Sending / Receiving Direct Messages
- Pagination control

No comments: