You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
4.5 KiB
112 lines
4.5 KiB
package com.guozhi.bloodanalysis.service;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.github.pagehelper.PageHelper;
|
|
import com.guozhi.bloodanalysis.entity.DataLineageInfo;
|
|
import com.guozhi.bloodanalysis.entity.vo.BloodAnalysisBySql;
|
|
import com.guozhi.bloodanalysis.mapper.DataLineageInfoMapper;
|
|
import com.guozhi.bloodanalysis.mapper.MetaBloodAnalysisMapper;
|
|
import com.guozhi.bloodanalysis.parser.SqlParser;
|
|
import com.guozhi.bloodanalysis.utils.RedisUtils;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class BloodAnalysisService {
|
|
|
|
@Autowired
|
|
DataLineageInfoMapper dataLineageInfoMapper;
|
|
|
|
@Autowired
|
|
ApplicationContext applicationContext;
|
|
|
|
@Value("${blood-analysis.pageNum}")
|
|
Integer pageNum;
|
|
|
|
@Value("${blood-analysis.pageSize}")
|
|
Integer pageSize;
|
|
|
|
@Value("${databaseUrl}")
|
|
String databaseUrl;
|
|
|
|
@Autowired
|
|
MetaBloodAnalysisMapper mapper;
|
|
|
|
@Autowired
|
|
RedisUtils redisUtils;
|
|
|
|
@Async
|
|
public void analysis(String dashUserName,String dashPassword) {
|
|
redisUtils.set("startBloodAnalysis",true);
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
// 1. 创建 POST 请求
|
|
HttpGet get = new HttpGet(databaseUrl);
|
|
// 2. 设置请求头(可选)
|
|
get.setHeader("Content-Type", "application/json");
|
|
get.setHeader("dashUserName",dashUserName);
|
|
get.setHeader("dashPassword",dashPassword);
|
|
// 4. 执行请求
|
|
try (CloseableHttpResponse response = httpClient.execute(get)) {
|
|
// 5. 获取响应
|
|
HttpEntity entity = response.getEntity();
|
|
if (entity != null) {
|
|
String responseStr = EntityUtils.toString(entity);
|
|
JSONObject obj = JSON.parseObject(responseStr);
|
|
JSONObject data = obj.getJSONObject("data");
|
|
JSONArray totalList = data.getJSONArray("totalList");
|
|
List<Map<Integer,String>> list = new ArrayList<>();
|
|
if (totalList !=null && totalList.size()>0){
|
|
for (int i = 0; i < totalList.size(); i++) {
|
|
JSONObject database = totalList.getJSONObject(i);
|
|
Map<Integer,String> map = new HashMap<>();
|
|
map.put(database.getInteger("id"),database.getString("type"));
|
|
list.add(map);
|
|
}
|
|
mapper.deleteAllBloodData();
|
|
parse(pageNum,pageSize,list);
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
redisUtils.set("startBloodAnalysis",false);
|
|
}
|
|
}
|
|
|
|
public void parse(Integer pageNum, Integer pageSize, List<Map<Integer,String>> databaseList) throws Exception {
|
|
PageHelper.startPage(pageNum,pageSize);
|
|
List<DataLineageInfo> list = dataLineageInfoMapper.search();
|
|
if (list != null && list.size()>0){
|
|
//访问dolphinscheduler接口,获取数据库名称以及对应的类型列表
|
|
for (DataLineageInfo dataLineageInfo : list) {
|
|
SqlParser sqlParser = applicationContext.getBean(SqlParser.class);
|
|
sqlParser.parse(dataLineageInfo,databaseList);
|
|
}
|
|
}
|
|
if (list != null && list.size() == pageSize){
|
|
parse(pageNum+1,pageSize,databaseList);
|
|
}
|
|
}
|
|
|
|
public List<Map<String, String>> analysisBySQL(BloodAnalysisBySql sqlModel) throws Exception {
|
|
SqlParser sqlParser = applicationContext.getBean(SqlParser.class);
|
|
return sqlParser.parse(sqlModel.getSql(), sqlModel.getSqlType(), sqlModel.getDefaultSystem(), sqlModel.getDefaultModel());
|
|
}
|
|
}
|
|
|