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-23 19:11:12 +08:00
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
import java.util.function.Supplier;
|
2020-06-28 18:29:39 +08:00
|
|
|
|
|
2020-07-23 19:11:12 +08:00
|
|
|
|
import static cn.ac.iie.service.read.ReadClickhouseData.putMapByHashcode;
|
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-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-23 19:11:12 +08:00
|
|
|
|
private static ClickhouseConnect manger = ClickhouseConnect.getInstance();
|
2020-07-15 19:33:59 +08:00
|
|
|
|
private DruidPooledConnection connection;
|
|
|
|
|
|
private Statement statement;
|
2020-06-28 18:29:39 +08:00
|
|
|
|
|
2020-07-31 19:25:33 +08:00
|
|
|
|
<T extends BaseDocument> void baseDocumentFromClickhouse(HashMap<Integer, HashMap<String, ArrayList<T>>> newMap,
|
|
|
|
|
|
Supplier<String> getSqlSupplier,
|
|
|
|
|
|
Function<ResultSet,T> formatResultFunc){
|
2020-06-28 18:29:39 +08:00
|
|
|
|
long start = System.currentTimeMillis();
|
2020-07-23 19:11:12 +08:00
|
|
|
|
initializeMap(newMap);
|
|
|
|
|
|
String sql = getSqlSupplier.get();
|
2020-07-31 19:25:33 +08:00
|
|
|
|
LOG.info(sql);
|
2020-06-28 18:29:39 +08:00
|
|
|
|
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-23 19:11:12 +08:00
|
|
|
|
int i = 0;
|
2020-07-15 19:33:59 +08:00
|
|
|
|
while (resultSet.next()) {
|
2020-07-23 19:11:12 +08:00
|
|
|
|
T newDoc = formatResultFunc.apply(resultSet);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
if (newDoc != null) {
|
2020-07-23 19:11:12 +08:00
|
|
|
|
i+=1;
|
|
|
|
|
|
putMapByHashcode(newDoc, newMap);
|
2020-07-15 19:33:59 +08:00
|
|
|
|
}
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
2020-07-31 19:25:33 +08:00
|
|
|
|
LOG.info("读取"+i+"条数据,运行时间:" + (last - start));
|
2020-06-28 18:29:39 +08:00
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
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
|
|
|
|
private <T extends BaseDocument> void initializeMap(HashMap<Integer, HashMap<String,ArrayList<T>>> map){
|
|
|
|
|
|
try {
|
|
|
|
|
|
for (int i = 0; i < ApplicationConfig.THREAD_POOL_NUMBER; i++) {
|
2020-07-23 19:11:12 +08:00
|
|
|
|
map.put(i, new HashMap<>(16));
|
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
|
|
|
|
}
|