Browse Source

添加插叙 配置方法

andy 1 year ago
parent
commit
5f60095332
1 changed files with 23 additions and 0 deletions
  1. 23 0
      src/main/java/com/scbfkj/uni/process/Elasticsearch.java

+ 23 - 0
src/main/java/com/scbfkj/uni/process/Elasticsearch.java

@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.scbfkj.uni.library.DataFormatUtil;
 import com.scbfkj.uni.library.UniReturnUtil;
+import com.scbfkj.uni.system.Config;
 import org.apache.http.Header;
 import org.apache.http.HttpHost;
 import org.apache.http.auth.AuthScope;
@@ -27,6 +28,10 @@ public class Elasticsearch {
 
     private static final Map<String, RestClient> restClientMap = new HashMap<>();
 
+    private static final DataBase DATA_BASE = new DataBase();
+    public static Map<String ,Object> execByDataSourceId(String dataSourceId,String indexName, String event, List<String> datas) throws Exception {
+        return exec(queryConnectionStr(dataSourceId),indexName,event,datas);
+    }
 
     public static Map<String, Object> exec(String connection, String indexName, String event, List<String> datas) {
         if (Objects.isNull(datas) || datas.isEmpty()) {
@@ -224,4 +229,22 @@ public class Elasticsearch {
         restClientMap.put(connection, restClient);
         return restClient;
     }
+
+
+    private static String queryConnectionStr(String datasourceId) throws Exception {
+        List<Map<String, Object>> result = DATA_BASE.query(Config.getCenterConnectionStr(), """
+                select host
+                from datasource
+                where datasourceid = ?""", datasourceId);
+        if (result.isEmpty()) {
+            throw new RuntimeException("数据源错误:没有找到数据源");
+        }
+        return result.stream().findFirst().map(it -> {
+            HashMap<String, Object> hashMap = new HashMap<>();
+            hashMap.put("host", it.get("host"));
+            hashMap.put("username", it.get("username"));
+            hashMap.put("password", it.get("password"));
+            return hashMap;
+        }).map(DataFormatUtil::toString).get();
+    }
 }