package mypackage.bolts;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;
import twitter4j.HashtagEntity;
import twitter4j.Status;
import java.util.Map;
/**
* Gets a Tweet status and emits shortened urls along with their expansion if any.
*
* @author Michael Vogiatzis
*
*/
public class HashtagAndTweetBolt extends BaseRichBolt{
private OutputCollector ouc;
@Override
public void prepare(Map stormConf, TopologyContext context,
OutputCollector collector) {
ouc = collector;
}
@Override
public void execute(Tuple tuple) {
Status s = (Status) tuple.getValueByField("tweet");
//Status s1 = (Status) tuple.getValueByField("tweet");
//URLEntity[] urls = s.getURLEntities();
System.out.println("the text is here===================="+s.getText());
// long startTimeMs = System.currentTimeMillis( );
// String time=""+startTimeMs;
String tweet_text=s.getText();
HashtagEntity[] urls=s.getHashtagEntities();
for (HashtagEntity htag : urls){
String shortURL = htag.getText();
System.out.println("the output of hashtag is "+shortURL);
//String tweet_text = htag.getText();
System.out.println("the output of tweet is == "+tweet_text);
if (tweet_text!=null)
{ouc.emit(new Values(shortURL, tweet_text));
}
}
ouc.ack(tuple);
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("shortUrl", "expUrl"));
}
}