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

http://twitter4j.org/en/index.html#download



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 = twitter.search(query);
                List tweets = result.getTweets();
                for (Status tweet : tweets) {
                    tweetList.add(tweet.getText());
                }
            } while ((query = result.nextQuery()) != null);
        } catch (TwitterException te) {
            te.printStackTrace();
            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()) {
System.out.println(">>>>>>>>>>>>"+iterator.next());
}
}


}


Out put :

>>>>>>>>>>>>Shooting #shotgun #beretta #thuglife #madeindorset #rah @ Axnoller - Dorset https://t.co/tT6l7HlXez
>>>>>>>>>>>>@beetlejuiceltd #looemusicfestival @ Looe Music Festival https://t.co/FpWbkCAd66
>>>>>>>>>>>>View from the queue #looemusicfestival #beetlejuice #cocktails @beetlejuiceltd @ Looe Music Festival https://t.co/IG8jgSHwEq
>>>>>>>>>>>>Crazy weekend at Looe Music Festival! #cocktails #dreamteam #beetlejuice #looemusicfestival… https://t.co/u08MsXnuh5
>>>>>>>>>>>>Fal Week 2014 #throwback #falmouth #summer #beetlejuice #barmen #dreamteam #cocktails https://t.co/IICVhUpOCP
>>>>>>>>>>>>@narendramodi , happy b'day sir....  May god fullfill your all wishesh and ours too..
>>>>>>>>>>>>2nd family #cornwall #friends #squad #rock #jack #goodbyesummer https://t.co/zppUUoIa0e
>>>>>>>>>>>>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: