1 package compbio.cassandra;
4 import java.util.HashMap;
6 import java.util.ArrayList;
9 import org.apache.log4j.Logger;
11 import com.datastax.driver.core.Row;
12 import com.datastax.driver.core.Session;
13 import com.datastax.driver.core.ResultSet;
14 import compbio.engine.JobStatus;
16 public class CassandraReader {
17 private Session session;
18 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
20 public CassandraReader() {
21 Session inis = CassandraNativeConnector.getSession();
25 public void setSession(Session s) {
31 * getting data from the db
33 public List<Pair<String, String>> ReadProteinDataTable() {
34 final long startTime = System.currentTimeMillis();
35 String com = "SELECT DataBegin,DataEnd FROM ProteinLog;";
36 System.out.println("Command: " + com);
37 ResultSet results = session.execute(com);
38 final long queryTime = System.currentTimeMillis();
39 List<Row> rows = results.all();
40 System.out.println("Query time is " + (queryTime - startTime) + " msec");
42 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
45 Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
49 final long endTime = System.currentTimeMillis();
50 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
55 * getting data from the db
57 public DateBean ReadProteinData(long day, String date) {
58 String com = "SELECT JobID, Protein FROM ProteinData WHERE jobtime = " + day + ";";
59 System.out.println("Command: " + com);
60 ResultSet results = session.execute(com);
61 if (results.isExhausted())
63 List<Row> rows = results.all();
64 DateBean res = new DateBean(date);
66 res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
72 * getting data from the db
74 public DateBean ReadFailedJobs(long day, String date, JobStatus status) {
75 // FailLog (jobtime, JobID, ExecTime, ip, FinalStatus)
76 String com = "SELECT JobID FROM FailLog WHERE jobtime = " + day + " and FinalStatus = '" + status.name() + "';";
77 ResultSet results = session.execute(com);
78 if (results.isExhausted())
80 List<Row> rows = results.all();
81 DateBean res = new DateBean(date);
83 String jobid = r.getString("JobID");
84 String com1 = "SELECT Protein FROM ProteinLog WHERE JobID = '" + jobid + "';";
85 System.out.println("Command: " + com1);
86 ResultSet results2 = session.execute(com1);
87 List<Row> jrows = results2.all();
88 if (1 == jrows.size()) {
89 String protein = jrows.get(0).getString("Protein");
90 res.setJobidAndSeq(jobid, protein);
97 * getting data from the db JobDateInfo
99 public Total ReadDateTable(long queryDate) {
100 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
101 if (results.isExhausted())
103 Row therow = results.one();
104 Total res = new Total(therow.getLong("Total"), therow.getLong("TotalOK"), therow.getLong("TotalStopped"),
105 therow.getLong("TotalError"), therow.getLong("TotalTimeOut"));
106 if (!results.isExhausted()) {
107 Date date = new Date(queryDate);
108 log.warn("CassandraReader.ReadDateTable: date row for " + date.toString() + " (" + queryDate + ") duplicated ");
114 * getting whole protein sequence from the db ProteinRow
116 public List<ProteinBean> ReadWholeSequence(String queryProtein) {
117 final long startTime = System.currentTimeMillis();
118 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
119 System.out.println("Command: " + com);
120 ResultSet results = session.execute(com);
121 if (results.isExhausted())
123 final long queryTime = System.currentTimeMillis();
124 List<Row> rows = results.all();
125 System.out.println("first size : " + rows.size());
126 System.out.println("Query time is " + (queryTime - startTime) + " msec");
127 System.out.println(" rows analysed, " + rows.size());
128 List<ProteinBean> res = new ArrayList<ProteinBean>();
129 ProteinBean structure = new ProteinBean(queryProtein, rows.get(0).getMap("Predictions", String.class, String.class));
130 System.out.println("second size : " + rows.size());
133 structure.setJobid(r.getString("JobID"));
137 final long endTime = System.currentTimeMillis();
138 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
145 public Map<String, String[]> ReadIpWithJobs(String ip) {
146 final long startTime = System.currentTimeMillis();
147 String com = "SELECT JobID, Protein, FinalStatus, DataBegin FROM ProteinLog WHERE ip = '" + ip + "';";
148 System.out.println("Command: " + com);
149 ResultSet results = session.execute(com);
150 if (results.isExhausted())
152 final long queryTime = System.currentTimeMillis();
153 List<Row> rows = results.all();
154 Map<String, String[]> res = new HashMap<String, String[]>();
155 System.out.println("Query time is " + (queryTime - startTime) + " msec");
156 System.out.println(" rows analysed, " + rows.size());
159 if (r.getString("FinalStatus").equals("OK")) {
160 String date = r.getString("DataBegin");
161 res.put(r.getString("JobID"), new String[] { date.substring(0, date.indexOf(":")), r.getString("Protein") });
165 final long endTime = System.currentTimeMillis();
166 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
171 * getting part of protein sequence from the db ProteinRow
173 public List<ProteinBean> ReadPartOfSequence(String queryProtein) {
174 final long startTime = System.currentTimeMillis();
175 String com = "SELECT * FROM ProteinRow;";
176 System.out.println("Command: " + com);
177 ResultSet results = session.execute(com);
178 if (results.isExhausted())
180 final long queryTime = System.currentTimeMillis();
181 List<Row> rows = results.all();
182 System.out.println("Query time is " + (queryTime - startTime) + " msec");
183 System.out.println(" rows analysed, " + rows.size());
184 List<ProteinBean> res = new ArrayList<ProteinBean>();
187 String prot = r.getString("Protein");
188 if (prot.matches("(.*)" + queryProtein + "(.*)")) {
189 ProteinBean structure = new ProteinBean(prot, r.getMap("Predictions", String.class, String.class));
190 structure.setJobid(r.getString("JobID"));
195 final long endTime = System.currentTimeMillis();
196 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
201 * getting protein sequences by counter
203 public Map<String, Integer> ReadProteinSequenceByCounter() {
204 final long startTime = System.currentTimeMillis();
205 String com = "SELECT Protein, JobID FROM ProteinRow;";
206 System.out.println("Command: " + com);
207 ResultSet results = session.execute(com);
208 if (results.isExhausted())
210 final long queryTime = System.currentTimeMillis();
211 List<Row> rows = results.all();
212 System.out.println("Query time is " + (queryTime - startTime) + " msec");
213 System.out.println(" rows analysed, " + rows.size());
214 Map<String, Integer> res = new HashMap<String, Integer>();
217 String protein = r.getString("Protein");
218 String id = r.getString("JobID");
219 if (res.containsKey(protein))
220 res.put(protein, res.get(protein) + 1);
224 final long endTime = System.currentTimeMillis();
225 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
230 * getting ip by counter
232 public Map<String, Integer> ReadIpByCounter() {
233 final long startTime = System.currentTimeMillis();
234 String com = "SELECT JobID, ip FROM ProteinLog;";
235 System.out.println("Command: " + com);
236 ResultSet results = session.execute(com);
237 if (results.isExhausted())
239 final long queryTime = System.currentTimeMillis();
240 List<Row> rows = results.all();
241 System.out.println("Query time is " + (queryTime - startTime) + " msec");
242 System.out.println(" rows analysed, " + rows.size());
243 Map<String, Integer> res = new HashMap<String, Integer>();
246 String ip = r.getString("ip");
247 String id = r.getString("JobID");
248 if (res.containsKey(ip))
249 res.put(ip, res.get(ip) + 1);
253 final long endTime = System.currentTimeMillis();
254 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
259 * getting log info for jobid
261 public JobBean ReadJobLog(String jobid) {
262 final long startTime = System.currentTimeMillis();
263 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
264 System.out.println("Command: " + com);
265 ResultSet results = session.execute(com);
266 if (results.isExhausted())
268 final long queryTime = System.currentTimeMillis();
269 Row row = results.one();
270 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
271 System.out.println("Command: " + com1);
272 ResultSet results1 = session.execute(com1);
273 if (results1.isExhausted())
275 Row row1 = results1.one();
276 JobBean res = new JobBean(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"), row.getString("DataEnd"),
277 row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
278 System.out.println("Query time is " + (queryTime - startTime) + " msec");
279 final long endTime = System.currentTimeMillis();
280 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");