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 java.io.*; import java.sql.*; import java.text.SimpleDateFormat; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; //import org.apache.log4j.Logger; /** * Created by anshushukla on 05/04/15. */ public class SqlDb extends BaseRichBolt { OutputCollector ouc; BufferedWriter bw; static String DB_URL; static String JDBC_DRIVER; static final String USER = "root"; static final String PASS = "12345"; Connection conn = null; Statement stmt = null; ResultSet rs = null; static Set<String> positiveList; static Set<String> negativeList; String sentiment; String shortUrl; @Override public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { //for database JDBC_DRIVER = "com.mysql.jdbc.Driver"; DB_URL = "jdbc:mysql://localhost/first"; positiveList = new HashSet<String>(); negativeList = new HashSet<String>(); //add from file inserFromFile("~/positive-words.txt", positiveList); inserFromFile("negative-words.txt", negativeList); /* Data sources are provided by- http://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html#datasets */ // Database credentials try { //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to a selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Connected database successfully..."); //STEP 4: Execute a query System.out.println("Inserting records into the table..."); stmt = conn.createStatement(); // System.out.println("preparing inside function"); ouc = collector; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } @Override public void execute(Tuple tuple) { System.out.print("+++++++++++++++++++++++++++++++++++++++++++++++++"); String expUrl = tuple.getStringByField("expUrl"); if (true) { shortUrl = tuple.getStringByField("shortUrl"); try { long yourmilliseconds = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm"); Date resultdate = new Date(yourmilliseconds); System.out.println("modified format is "+sdf.format(resultdate)); String time=""+sdf.format(resultdate); sentiment=""+classifyText(expUrl); System.out.println("sentiment here is ------" + sentiment ); String sql="INSERT INTO tweetupdate1 " +"VALUES ("+"'"+shortUrl+"',"+"'"+expUrl+"'"+","+"'"+time+"'"+",'"+sentiment+"'"+")"; //String sql="INSERT INTO tweetupdate " +"VALUES ("+"'"+shortUrl+"',"+"'"+expUrl+"'"+","+"'"+time+"'"+")"; System.out.print("--------------"+sql); stmt.executeUpdate(sql); System.out.println("Inserted records into the table..."); } catch (SQLException e) { System.out.print("fail is being called"); ouc.fail(tuple); e.printStackTrace(); } } ouc.emit(new Values(shortUrl,sentiment)); System.out.println("==========================Done=========================="); ouc.ack(tuple); } //predictor public static String classifyText(String msg) { //Delimeters need to be further extended. StringTokenizer st = new StringTokenizer(msg,"[,. #]+"); int positive =0 ,negative =0 ,neutral =0; while(st.hasMoreTokens()) { String next = st.nextToken().toLowerCase(); //System.out.println(next); positive += (positiveList.contains(next) ? 1: 0); negative += (negativeList.contains(next) ? 1: 0); //See whether neutral adds any value, TODO - if(!positiveList.contains(next) && !negativeList.contains(next)) neutral += (positiveList.contains(next) ? 1: 0); } return (positive == negative) ? "positive" : ((positive > negative) ? "Positive" :"Negative"); } //helper function. public void inserFromFile(String filename, Set<String> list) { try { InputReader in = new InputReader(filename); while(in.hasMoreTokens()) { list.add(in.nextToken()); } } catch(Exception e) { e.printStackTrace(); } } //IO class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader() { reader = new BufferedReader(new InputStreamReader(System.in)); } InputReader(String fileName) throws FileNotFoundException { reader = new BufferedReader(new FileReader(new File(fileName))); } String readLine() throws IOException { return reader.readLine(); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(readLine(), "[-]"); return tokenizer.nextToken(); } boolean hasMoreTokens() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String s = readLine(); if (s == null) return false; tokenizer = new StringTokenizer(s); } return true; } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } } @Override public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) { outputFieldsDeclarer.declare(new Fields("hashtag","sentiment")); } }