我对Solr很新,并尝试实现一个每30秒查询一次solr的java程序。
情景:
程序启动时,它会查询solr以检索前一个月的DDI网络活动。 以下是查询。 query.set(“q”,“pname:'深度威胁发现设备'和eventName:MALWARE_DETECTION和logTime:[NOW-1MONTH TO NOW] AND app:HTTP”); query.setRows(10000);
初始化后,程序每隔30秒查询一次DDA。 以下是查询。 query.set(“q”,“pname:'深度威胁发现督察'和eventName:MALWARE_DETECTION和logTime:[NOW-30SECOND TO NOW] AND app:HTTP”); query.setRows(10000);
以下是我的java代码。
public void run(){
Connection conn;
PreparedStatement ps;
int dda_count = glob_var.dda_cnt;
long threadId = Thread.currentThread().getId();
System.out.println("Solr handling thread started... Thread ID: " + threadId);
System.out.println("Solr monitoring started... Thread ID: " + threadId);
while(true){
try{
for(int j = 0; j < glob_var.dda_cnt; j++){
Query_DDA_Solr(dda_solr[j], my_url);
Thread.sleep(100);
}
Thread.sleep(30000);
} catch(Exception e){
log.write_log("DDIC_SOLR_CON/run: " + e);
}
}
}
public static void Query_DDA_Solr(String dda_s, String my_u){
try{
Connection conn;
PreparedStatement ps;
SolrServer server = new HttpSolrServer(dda_s);
SolrQuery query = new SolrQuery();
//query.set("q","pname:'Deep Discovery Inspector' AND eventName:MALWARE_DETECTION AND logTime:[NOW-5MINUTE TO NOW] AND app:HTTP");
query.set("q","pname:'Deep Discovery Inspector' AND eventName:MALWARE_DETECTION AND app:HTTP");
query.setRows(10000);
QueryResponse rsp = server.query(query);
SolrDocumentList results = rsp.getResults();
conn = DriverManager.getConnection(my_u, glob_var.my_userid, glob_var.my_userpw);
System.out.println("size of result = " + results.size());
for (int i = 0; i < results.size(); ++i) {
//boolean insert = false;
String virusname = "", request = "";
Timestamp etime = null;
String element = results.get(i).toString();
StringTokenizer ts = new StringTokenizer(element, "{");
ts.nextElement();
String ts2 = ts.nextElement().toString();
ts = new StringTokenizer(ts2, ",");
while(ts.hasMoreTokens()){
String pair = ts.nextToken();
StringTokenizer ts_2 = new StringTokenizer(pair, "=");
String temp_attr = ts_2.nextElement().toString();
if(temp_attr.equals(" logTime")){
String eventtime = ts_2.nextElement().toString();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date d = (Date) sdf.parse(eventtime);
Timestamp temp_ts = new Timestamp(d.getTime());
etime = temp_ts;
break;
}
}
String app = "";
// other works here
//if(insert){
if(etime.after(glob_var.solr_timestamp)){
ResultSet trs = null;
PreparedStatement my_ps;
// database insert here
}
}
conn.close();
results.clear();
query.clear();
server.shutdown();
Thread.sleep(5);
} catch(Exception e){
log.write_log("SOLR_CON/Query_DDA_Solr: " + e);
}
}
有没有人知道这段代码有什么问题?
提前致谢
答案 0 :(得分:0)
将query.setRows(xxxx)增加到一个大数字并检查,它帮助了我