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.Tuple; import backtype.storm.tuple.Values; import com.lambdaworks.redis.RedisClient; import com.lambdaworks.redis.RedisConnection; import java.util.HashMap; import java.util.Map; /** * Created by anshushukla on 06/04/15. */ public class report extends BaseRichBolt { private OutputCollector ouc; private Map<String, Integer> countMap; // place holder to keep the connection to redis transient RedisConnection<String,String> redis; // public static String superscript(String str) { // str = str.replaceAll("-", "-"); // str = str.replaceAll("+", "+"); // // return str; // } @Override public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) { // instantiate a redis connection RedisClient client = new RedisClient("localhost",6379); // initiate the actual connection redis = client.connect(); countMap = new HashMap<String, Integer>(); ouc = outputCollector; } @Override public void execute(Tuple tuple) { // access the first column 'word' String word = tuple.getStringByField("hashtag"); //System.out.print("hashtag inside report is "+ word); String senti = tuple.getStringByField("sentiment"); System.out.print("senti inside report is "+ senti); if(senti.equals("Positive")) word=word+"+"; else if(senti.equals("Negative")) word=word+"-"; // String word = tuple.getString(0); if(!countMap.containsKey(word)) countMap.put(word, 1); else{ int x = countMap.get(word); countMap.put(word, x+1); } // access the second column 'count' // Integer count = tuple.getIntegerByField("sentiment"); System.out.print("the count is " + countMap.get(word)); Double d= Math.pow(2, countMap.get(word)); Integer count=50+20*countMap.get(word); ouc.emit(new Values(word, count)); //d.intValue(); // publish the word count to redis using word as the key //countMap.get(word) redis.publish("TopologyWithSQL", word + "|" + count); System.out.println("inserted to redis with key\t" + word + "value is \t" + count); ouc.ack(tuple); } @Override public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) { } }