parent
2ee0f0ab24
commit
1a2aec187c
@ -1,33 +1,64 @@
|
||||
package DataEX
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ESData struct {
|
||||
|
||||
SystemId string `json:"system_id"`
|
||||
DatasourceId string `json:"datasource_id"`
|
||||
ProvinceId string `json:"province_id"`
|
||||
ProvinceId string `json:"province_code"`
|
||||
ProvinceName string `json:"province_name"`
|
||||
CityId string `json:"city_id"`
|
||||
CityId string `json:"city_code"`
|
||||
CityName string `json:"city_name"`
|
||||
AreaId string `json:"area_id"`
|
||||
AreaName string `json:"area_name"`
|
||||
AreaId string `json:"district_code"`
|
||||
AreaName string `json:"district_name"`
|
||||
BureauId string `json:"bureau_id"`
|
||||
RegionId string `json:"region_id"`
|
||||
MainId string `json:"main_id"`
|
||||
OrgId string `json:"org_id"`
|
||||
OrgName string `json:"org_name" `
|
||||
OrgType int `json:"org_type"`
|
||||
SchoolType string `json:"school_type"`
|
||||
SchoolTypeName string `json:"school_typename"`
|
||||
DeptId string `json:"dept_id"`
|
||||
StageId string `json:"stage_id"`
|
||||
GradeId string `json:"grade_id"`
|
||||
ClassId string `json:"class_id"`
|
||||
DataId string `json:"data_id"`
|
||||
FileUri string `json:"file_uri"`
|
||||
BeginTime time.Time `json:"begin_time"`
|
||||
EndTime time.Time `json:"end_time"`
|
||||
BeginTime JsonDate `json:"begin_time"`
|
||||
EndTime JsonDate `json:"end_time"`
|
||||
DelFlag int `json:"del_flag"`
|
||||
EnableFlag int `json:"enable_flag"`
|
||||
|
||||
DataContent map[string]interface{} `json:"data_content"`
|
||||
}
|
||||
|
||||
|
||||
//add by zhangjun 2020-07-01
|
||||
type JsonDate time.Time
|
||||
|
||||
const (
|
||||
timeFormat = "2006/01/02 15:04:05"
|
||||
)
|
||||
|
||||
// JsonDate反序列化
|
||||
func (t *JsonDate) UnmarshalJSON(data []byte) (err error) {
|
||||
newTime, err := time.ParseInLocation("\""+timeFormat+"\"", string(data), time.Local)
|
||||
*t = JsonDate(newTime)
|
||||
return
|
||||
}
|
||||
|
||||
// JsonDate序列化
|
||||
func (t JsonDate) MarshalJSON() ([]byte, error) {
|
||||
timeStr := fmt.Sprintf("\"%s\"", time.Time(t).Format(timeFormat))
|
||||
return []byte(timeStr),nil
|
||||
}
|
||||
|
||||
// string方法
|
||||
func (t JsonDate) String() string {
|
||||
return time.Time(t).Format(timeFormat)
|
||||
}
|
||||
|
@ -0,0 +1,78 @@
|
||||
package ES7SqlUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"dsDataex/Utils/ConfigUtil"
|
||||
"encoding/json"
|
||||
es7 "github.com/elastic/go-elasticsearch/v7"
|
||||
"github.com/elastic/go-elasticsearch/v7/esapi"
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
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) map[string]interface{} {
|
||||
query := map[string]interface{}{
|
||||
"query": sql,
|
||||
}
|
||||
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) string {
|
||||
query := map[string]interface{}{
|
||||
"query": sql,
|
||||
}
|
||||
jsonBody, _ := json.Marshal(query)
|
||||
|
||||
req := esapi.SQLQueryRequest{
|
||||
Body: bytes.NewReader(jsonBody),
|
||||
Format: "txt",
|
||||
}
|
||||
|
||||
res, _ := req.Do(CTX, ES7Client)
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
var bts []byte
|
||||
|
||||
res.Body.Read(bts)
|
||||
var result=string(bts)
|
||||
|
||||
return result
|
||||
}
|
Loading…
Reference in new issue