package ES7SqlUtil import ( "bytes" "context" "dsDataex/Utils/ConfigUtil" "encoding/json" es7 "github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v7/esapi" "strings" ) var ES7Client *es7.Client var ServerVersion string var CTX context.Context func init() { cfg := es7.Config{Addresses: ConfigUtil.ESAddress,} ES7Client, _ = es7.NewClient(cfg) CTX=context.Background() if ConfigUtil.ESEnable == 1 { res, _ := ES7Client.Info() defer res.Body.Close() var result map[string]interface{} json.NewDecoder(res.Body).Decode(&result) ServerVersion=(result["version"].(map[string]interface{}))["number"].(string) } } func SqlQueryJson(sql string,param []string) map[string]interface{} { query := map[string]interface{}{ "query": sql, "params":param, "fetch_size":10000, } jsonBody, _ := json.Marshal(query) req := esapi.SQLQueryRequest{ Body: bytes.NewReader(jsonBody), Format: "json", } res, _ := req.Do(CTX, ES7Client) defer res.Body.Close() var result map[string]interface{} json.NewDecoder(res.Body).Decode(&result) return result } func SqlQueryTxt(sql string,param []string) string { query := map[string]interface{}{ "query": sql, "params":param, "fetch_size":10000, } jsonBody, _ := json.Marshal(query) req := esapi.SQLQueryRequest{ Body: bytes.NewReader(jsonBody), Format: "txt", } res, _ := req.Do(CTX, ES7Client) defer res.Body.Close() var result=res.String() result=strings.Replace(result,"[200 OK]","",-1) result=strings.Replace(result," ","",-1) return result } func SqlQueryObj(sql string,param []string) ESSqlResult { query := map[string]interface{}{ "query": sql, "params":param, "fetch_size":10000, } jsonBody, _ := json.Marshal(query) req := esapi.SQLQueryRequest{ Body: bytes.NewReader(jsonBody), Format: "json", } res, _ := req.Do(CTX, ES7Client) defer res.Body.Close() var obj ESSqlResult json.NewDecoder(res.Body).Decode(&obj) return obj }