//package spouts; package mypackage.spouts; import backtype.storm.spout.SpoutOutputCollector; import backtype.storm.task.TopologyContext; import backtype.storm.topology.OutputFieldsDeclarer; import backtype.storm.topology.base.BaseRichSpout; import backtype.storm.tuple.Fields; import backtype.storm.tuple.Values; import backtype.storm.utils.Utils; import org.slf4j.Logger; import twitter4j.*; import twitter4j.conf.ConfigurationBuilder; import java.util.Map; import java.util.concurrent.LinkedBlockingQueue; //import org.apache.slf4j.Logger; //import twitter4j.internal.logging.LoggerFactory; /** * Twitter spout connected to real-time stream. It stores tweet statuses to a queue * and emits them to the topology. * * @author Michael Vogiatzis */ public class TwitterSpout extends BaseRichSpout { private static final Logger log = org.slf4j.LoggerFactory.getLogger(TwitterSpout.class); // .getLogger(TwitterSpout.class); SpoutOutputCollector _collector; LinkedBlockingQueue<Status> queue = null; long myRandomMsgId; @Override public void open(Map confMap, TopologyContext context, SpoutOutputCollector collector) { _collector = collector; queue = new LinkedBlockingQueue<Status>(1000); //implement a listener for twitter statuses StatusListener listener = new StatusListener() { public void onStatus(Status status) { queue.offer(status); } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long userId, long upToStatusId) { } @Override public void onStallWarning(StallWarning warning) { } }; //twitter stream authentication setup // Properties prop = new Properties(); // try { // prop.load(TwitterSpout.class.getClassLoader().getResourceAsStream("config.properties1")); // } catch (IOException e) { // log.error(e.toString()); // } ConfigurationBuilder twitterConf = new ConfigurationBuilder(); twitterConf.setIncludeEntitiesEnabled(true); // twitterConf.setOAuthAccessToken("104108757-xbOB6AmJQsqv0zfKRrGfVLnyJouxmbkMh5ZURI8m"); // twitterConf.setOAuthAccessTokenSecret("M7NbbbFZvKwJwT0xQo9LUvZS8H13jWcYGmqXooStt9ulC"); // twitterConf.setOAuthConsumerKey("7PCvsG0e8aR0LSmzYLO6MO9Rz"); // twitterConf.setOAuthConsumerSecret("hBfobguRPYbc8NZ1FaZ1JLpNH5aMK7HTF8y7QvAbkHyBMrfqiT"); twitterConf.setOAuthAccessToken("3070167834-DT8Z5AD0rA2cPtJQ3jwfypHUgYzmtjMUcBLIvki"); twitterConf.setOAuthAccessTokenSecret("PpOlDMGEc1x2l8dJ9ct8SIvJpEYrLftoZzQn4NHwbLSlp"); twitterConf.setOAuthConsumerKey("RIQrAnt60HKzKrd5hVJ1TDi2v"); twitterConf.setOAuthConsumerSecret("KL1J44TnzCbTYm1oHY7jPZcQvRuJ8e66GZp8Uoz11oY7n0sriR"); // twitterConf.setOAuthAccessToken(prop.getProperty("OATH_ACCESS_TOKEN")); // twitterConf.setOAuthAccessTokenSecret(prop.getProperty("OATH_ACCESS_TOKEN_SECRET")); // twitterConf.setOAuthConsumerKey(prop.getProperty("OATH_CONSUMER_KEY")); // twitterConf.setOAuthConsumerSecret(prop.getProperty("OATH_CONSUMER_SECRET")); TwitterStream twitterStream = new TwitterStreamFactory(twitterConf.build()).getInstance(); twitterStream.addListener(listener); // sample() method internally creates a thread which manipulates TwitterStream and calls //the listener methods continuously. twitterStream.filter(new FilterQuery().track(new String[]{"#Presstitutes", "#AshuCries", "#WorldBookDay", "#happybirthdaysachin", "#AAPRallyMurder", "#NetNeutrality", "#36Vayadhinile", "#RainaKiShaadi", "#BJPFoundationDay", "#36Vayadhinile", "#ThrillNightsOnAXN", "Prem Is Back", "Crystal Palace"})); //twitterStream.sample(); //Instead of sample try to use } @Override public void nextTuple() { Status ret = queue.poll(); Utils.sleep(200); if (ret == null) { //if queue is empty sleep the spout thread so it doesn't consume resources Utils.sleep(50); } else { myRandomMsgId++; if (myRandomMsgId > 100) myRandomMsgId = 1; // System.out.println("The value of in TwitterSpout is " + myRandomMsgId); // _collector.emit(new Values(ret)); _collector.emit(new Values(ret),myRandomMsgId); System.out.println(ret.getUser().getName() + " : " + ret.getText()); } } @Override public void ack(Object id) { System.out.print("The ACK value of in TwitterSpout is " + id); } @Override public void fail(Object id) { System.out.print("The fail value due to database exception of in RandomSentenceSpout is " + id); } @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declare(new Fields("tweet")); } }