2020-06-28 18:29:39 +08:00
|
|
|
|
package cn.ac.iie.dao;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.ac.iie.config.ApplicationConfig;
|
|
|
|
|
|
import cn.ac.iie.utils.ClickhouseConnect;
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidPooledConnection;
|
|
|
|
|
|
import com.arangodb.entity.BaseDocument;
|
|
|
|
|
|
import com.arangodb.entity.BaseEdgeDocument;
|
2020-07-15 19:33:59 +08:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-06-28 18:29:39 +08:00
|
|
|
|
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
|
import java.sql.Statement;
|
2020-07-07 18:17:14 +08:00
|
|
|
|
import java.util.ArrayList;
|
2020-06-28 18:29:39 +08:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
import static cn.ac.iie.service.read.ReadClickhouseData.*;
|
2020-06-28 18:29:39 +08:00
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 读取clickhouse数据,封装到map
|
|
|
|
|
|
* @author wlh
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class BaseClickhouseData {
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(BaseClickhouseData.class);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
private static ClickhouseConnect manger = ClickhouseConnect.getInstance();
|
2020-07-20 19:37:00 +08:00
|
|
|
|
static HashMap<Integer, HashMap<String, ArrayList<BaseDocument>>> newVertexFqdnMap = new HashMap<>();
|
|
|
|
|
|
static HashMap<Integer, HashMap<String, ArrayList<BaseDocument>>> newVertexIpMap = new HashMap<>();
|
|
|
|
|
|
static HashMap<Integer, HashMap<String,ArrayList<BaseDocument>>> newVertexSubscriberMap = new HashMap<>();
|
|
|
|
|
|
static HashMap<Integer, HashMap<String, ArrayList<BaseEdgeDocument>>> newRelationFqdnAddressIpMap = new HashMap<>();
|
|
|
|
|
|
static HashMap<Integer, HashMap<String, ArrayList<BaseEdgeDocument>>> newRelationIpVisitFqdnMap = new HashMap<>();
|
|
|
|
|
|
static HashMap<Integer, HashMap<String, ArrayList<BaseEdgeDocument>>> newRelationSubsciberLocateIpMap = new HashMap<>();
|
2020-07-21 19:41:17 +08:00
|
|
|
|
static HashMap<Integer, HashMap<String, ArrayList<BaseEdgeDocument>>> newRelationFqdnSameFqdnMap = new HashMap<>();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
private DruidPooledConnection connection;
|
|
|
|
|
|
private Statement statement;
|
2020-06-28 18:29:39 +08:00
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
void baseVertexFqdn() {
|
2020-07-20 19:37:00 +08:00
|
|
|
|
initializeMap(newVertexFqdnMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info("FQDN resultMap初始化完成");
|
|
|
|
|
|
String sql = getVertexFqdnSql();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
try {
|
2020-07-15 19:33:59 +08:00
|
|
|
|
connection = manger.getConnection();
|
|
|
|
|
|
statement = connection.createStatement();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
BaseDocument newDoc = getVertexFqdnDocument(resultSet);
|
|
|
|
|
|
if (newDoc != null) {
|
2020-07-20 19:37:00 +08:00
|
|
|
|
putMapByHashcode(newDoc, newVertexFqdnMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
}
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info(sql + "\n读取clickhouse v_FQDN时间:" + (last - start));
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
LOG.error(e.toString());
|
|
|
|
|
|
}finally {
|
|
|
|
|
|
manger.clear(statement,connection);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
void baseVertexIp() {
|
2020-07-20 19:37:00 +08:00
|
|
|
|
initializeMap(newVertexIpMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info("IP resultMap初始化完成");
|
|
|
|
|
|
String sql = getVertexIpSql();
|
2020-07-07 18:17:14 +08:00
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
try {
|
2020-07-15 19:33:59 +08:00
|
|
|
|
connection = manger.getConnection();
|
|
|
|
|
|
statement = connection.createStatement();
|
2020-07-07 18:17:14 +08:00
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
BaseDocument newDoc = getVertexIpDocument(resultSet);
|
2020-07-20 19:37:00 +08:00
|
|
|
|
putMapByHashcode(newDoc, newVertexIpMap);
|
2020-07-07 18:17:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info(sql + "\n读取clickhouse v_IP时间:" + (last - start));
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
LOG.error(e.toString());
|
|
|
|
|
|
}finally {
|
|
|
|
|
|
manger.clear(statement,connection);
|
2020-07-07 18:17:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
void baseVertexSubscriber(){
|
2020-07-20 19:37:00 +08:00
|
|
|
|
initializeMap(newVertexSubscriberMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info("SUBSCRIBER resultMap初始化完成");
|
|
|
|
|
|
String sql = getVertexSubscriberSql();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
try {
|
2020-07-15 19:33:59 +08:00
|
|
|
|
connection = manger.getConnection();
|
|
|
|
|
|
statement = connection.createStatement();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
|
|
|
|
|
while (resultSet.next()){
|
2020-07-15 19:33:59 +08:00
|
|
|
|
BaseDocument newDoc = getVertexSubscriberDocument(resultSet);
|
2020-07-20 19:37:00 +08:00
|
|
|
|
putMapByHashcode(newDoc, newVertexSubscriberMap);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info(sql + "\n读取clickhouse v_SUBSCRIBER时间:" + (last - start));
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}catch (Exception e){
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.error(sql + "\n读取clickhouse v_SUBSCRIBER失败");
|
2020-06-28 18:29:39 +08:00
|
|
|
|
e.printStackTrace();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
}finally {
|
|
|
|
|
|
manger.clear(statement,connection);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
void baseRelationshipSubscriberLocateIp(){
|
2020-07-20 19:37:00 +08:00
|
|
|
|
initializeMap(newRelationSubsciberLocateIpMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info("R_LOCATE_SUBSCRIBER2IP");
|
|
|
|
|
|
String sql = getRelationshipSubsciberLocateIpSql();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
try {
|
2020-07-15 19:33:59 +08:00
|
|
|
|
connection = manger.getConnection();
|
|
|
|
|
|
statement = connection.createStatement();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
|
|
|
|
|
while (resultSet.next()){
|
2020-07-15 19:33:59 +08:00
|
|
|
|
BaseEdgeDocument newDoc = getRelationshipSubsciberLocateIpDocument(resultSet);
|
2020-07-20 19:37:00 +08:00
|
|
|
|
putMapByHashcode(newDoc, newRelationSubsciberLocateIpMap);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info(sql + "\n读取clickhouse ESubsciberLocateIp时间:" + (last - start));
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}catch (Exception e){
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.error(sql + "\n读取clickhouse ESubsciberLocateIp失败");
|
2020-06-28 18:29:39 +08:00
|
|
|
|
e.printStackTrace();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
}finally {
|
|
|
|
|
|
manger.clear(statement,connection);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
void baseRelationshipFqdnAddressIp() {
|
2020-07-20 19:37:00 +08:00
|
|
|
|
initializeMap(newRelationFqdnAddressIpMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info("R_LOCATE_FQDN2IP resultMap初始化完成");
|
|
|
|
|
|
String sql = getRelationshipFqdnAddressIpSql();
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
2020-07-07 18:17:14 +08:00
|
|
|
|
try {
|
2020-07-15 19:33:59 +08:00
|
|
|
|
connection = manger.getConnection();
|
|
|
|
|
|
statement = connection.createStatement();
|
2020-07-07 18:17:14 +08:00
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
BaseEdgeDocument newDoc = getRelationFqdnAddressIpDocument(resultSet);
|
2020-07-20 19:37:00 +08:00
|
|
|
|
putMapByHashcode(newDoc, newRelationFqdnAddressIpMap);
|
2020-07-07 18:17:14 +08:00
|
|
|
|
}
|
2020-07-15 19:33:59 +08:00
|
|
|
|
long last = System.currentTimeMillis();
|
|
|
|
|
|
LOG.info(sql + "\n读取clickhouse EFqdnAddressIp时间:" + (last - start));
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
LOG.error(e.toString());
|
|
|
|
|
|
}finally {
|
|
|
|
|
|
manger.clear(statement,connection);
|
2020-07-07 18:17:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-21 19:41:17 +08:00
|
|
|
|
void baseRelationshipFqdnSameFqdn(){
|
|
|
|
|
|
initializeMap(newRelationFqdnSameFqdnMap);
|
|
|
|
|
|
LOG.info("R_SAME_ORIGIN_FQDN2FQDN resultMap初始化完成");
|
|
|
|
|
|
String sql = getRelationshipFqdnSameFqdnSql();
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
try {
|
|
|
|
|
|
connection = manger.getConnection();
|
|
|
|
|
|
statement = connection.createStatement();
|
|
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
BaseEdgeDocument newDoc = getRelationshipFqdnSameFqdnDocument(resultSet);
|
|
|
|
|
|
putMapByHashcode(newDoc, newRelationFqdnSameFqdnMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
|
|
|
|
|
LOG.info(sql + "\n读取clickhouse EIpVisitFqdn时间:" + (last - start));
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}finally {
|
|
|
|
|
|
manger.clear(statement,connection);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
void baseRelationshipIpVisitFqdn() {
|
2020-07-20 19:37:00 +08:00
|
|
|
|
initializeMap(newRelationIpVisitFqdnMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info("R_VISIT_IP2FQDN resultMap初始化完成");
|
|
|
|
|
|
String sql = getRelationshipIpVisitFqdnSql();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
try {
|
2020-07-15 19:33:59 +08:00
|
|
|
|
connection = manger.getConnection();
|
|
|
|
|
|
statement = connection.createStatement();
|
2020-06-28 18:29:39 +08:00
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
BaseEdgeDocument newDoc = getRelationIpVisitFqdnDocument(resultSet);
|
2020-07-20 19:37:00 +08:00
|
|
|
|
putMapByHashcode(newDoc, newRelationIpVisitFqdnMap);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.info(sql + "\n读取clickhouse EIpVisitFqdn时间:" + (last - start));
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
LOG.error(e.toString());
|
|
|
|
|
|
}finally {
|
|
|
|
|
|
manger.clear(statement,connection);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private <T extends BaseDocument> void initializeMap(HashMap<Integer, HashMap<String,ArrayList<T>>> map){
|
|
|
|
|
|
try {
|
|
|
|
|
|
for (int i = 0; i < ApplicationConfig.THREAD_POOL_NUMBER; i++) {
|
|
|
|
|
|
map.put(i, new HashMap<>());
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
e.printStackTrace();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
LOG.error("初始化数据失败");
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 19:33:59 +08:00
|
|
|
|
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|