master
zhangjun 5 years ago
parent b1b517b01b
commit 9345f2dd72

@ -18,56 +18,56 @@ func init() {
//初始化ES Client使用配置文件 //初始化ES Client使用配置文件
ES7Client, _ = elastic.NewClient(elastic.SetURL(ConfigUtil.ESAddress...)) ES7Client, _ = elastic.NewClient(elastic.SetURL(ConfigUtil.ESAddress...))
CTX=context.Background() CTX = context.Background()
ES7Type=make(map[string]map[string]string) ES7Type = make(map[string]map[string]string)
} }
func GetES7Type(indexName string,itemName string) (string,error) { func GetES7Type(indexName string, itemName string) (string, error) {
count,_:= RedisUtil.RedisClient.Exists("ES_Mapping_"+indexName+":"+itemName).Result() count, _ := RedisUtil.RedisClient.Exists("ES_Mapping_" + indexName + ":" + itemName).Result()
if count>0{ if count > 0 {
return RedisUtil.RedisClient.Get("ES_Mapping_"+indexName+":"+itemName).Result() return RedisUtil.RedisClient.Get("ES_Mapping_" + indexName + ":" + itemName).Result()
}else{ } else {
//获取ES Mapping //获取ES Mapping
res,_:= ES7Client.GetMapping().Index(indexName).Do(CTX) res, _ := ES7Client.GetMapping().Index(indexName).Do(CTX)
if res==nil{ if res == nil {
return "ES索引名称错误",nil return "ES索引名称错误", nil
} }
//暴力解析ES Mapping //暴力解析ES Mapping
for k, v := range res[indexName].(map[string]interface{}){ for k, v := range res[indexName].(map[string]interface{}) {
if k=="mappings"{ if k == "mappings" {
for k2, v2 := range v.(map[string]interface{}){ for k2, v2 := range v.(map[string]interface{}) {
if k2=="properties"{ if k2 == "properties" {
ES7Type[indexName]=make(map[string]string) ES7Type[indexName] = make(map[string]string)
for k3, v3 := range v2.(map[string]interface{}) { for k3, v3 := range v2.(map[string]interface{}) {
if k3=="data_content"{ if k3 == "data_content" {
for k4, v4 := range v3.(map[string]interface{}) { for k4, v4 := range v3.(map[string]interface{}) {
for k5, _ := range v4.(map[string]interface{}) { for k5, _ := range v4.(map[string]interface{}) {
if k4=="fields"{ if k4 == "fields" {
ES7Type[indexName]["data_content."+k5]="data_content."+k5+".keyword" ES7Type[indexName]["data_content."+k5] = "data_content." + k5 + ".keyword"
}else{ } else {
ES7Type[indexName]["data_content."+k5]="data_content."+k5 ES7Type[indexName]["data_content."+k5] = "data_content." + k5
} }
} }
} }
}else{ } else {
for k4, _ := range v3.(map[string]interface{}) { for k4, _ := range v3.(map[string]interface{}) {
if k4=="fields"{ if k4 == "fields" {
ES7Type[indexName][k3]=k3+".keyword" ES7Type[indexName][k3] = k3 + ".keyword"
}else{ } else {
ES7Type[indexName][k3]=k3 ES7Type[indexName][k3] = k3
} }
} }
} }
@ -80,7 +80,7 @@ func GetES7Type(indexName string,itemName string) (string,error) {
//写入Redis 缓存 //写入Redis 缓存
pipe := RedisUtil.RedisClient.Pipeline() pipe := RedisUtil.RedisClient.Pipeline()
for k, v := range ES7Type[indexName]{ for k, v := range ES7Type[indexName] {
pipe.Set("ES_Mapping_"+indexName+":"+k, v, 24*time.Hour) pipe.Set("ES_Mapping_"+indexName+":"+k, v, 24*time.Hour)
} }
@ -92,6 +92,5 @@ func GetES7Type(indexName string,itemName string) (string,error) {
} }
} }
return ES7Type[indexName][itemName],nil return ES7Type[indexName][itemName], nil
} }

@ -8,7 +8,6 @@ import (
"dsDataex/Utils/CacheUtil" "dsDataex/Utils/CacheUtil"
"dsDataex/Utils/ConfigUtil" "dsDataex/Utils/ConfigUtil"
"dsDataex/Utils/ES7SqlUtil" "dsDataex/Utils/ES7SqlUtil"
"dsDataex/Utils/ES7TypeUtil"
"dsDataex/Utils/ES7Util" "dsDataex/Utils/ES7Util"
_ "dsDataex/docs" _ "dsDataex/docs"
"fmt" "fmt"
@ -30,7 +29,7 @@ var (
g errgroup.Group g errgroup.Group
) )
func GrpcServerInit(){ func GrpcServerInit() {
s = grpc.NewServer() s = grpc.NewServer()
@ -39,7 +38,7 @@ func GrpcServerInit(){
reflection.Register(s) reflection.Register(s)
} }
func GinServerInit(){ func GinServerInit() {
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)
@ -95,6 +94,8 @@ func main() {
CacheUtil.OrgtreeCacheInit() CacheUtil.OrgtreeCacheInit()
//fmt.Println(ES7TypeUtil.GetES7Type("safety","data_id"))
GinServerInit() GinServerInit()
g.Go(func() error { g.Go(func() error {
@ -106,15 +107,13 @@ func main() {
GrpcServerInit() GrpcServerInit()
g.Go(func() error { g.Go(func() error {
lis, _ := net.Listen("tcp", ":" + ConfigUtil.ProjectGrpc) lis, _ := net.Listen("tcp", ":"+ConfigUtil.ProjectGrpc)
fmt.Printf("dsDataEX GRPC服务发布成功,服务端口: %s\n", ConfigUtil.ProjectGrpc) fmt.Printf("dsDataEX GRPC服务发布成功,服务端口: %s\n", ConfigUtil.ProjectGrpc)
return s.Serve(lis) return s.Serve(lis)
}) })
fmt.Println(ES7TypeUtil.GetES7Type("safety","data_id"))
if err := g.Wait(); err != nil { if err := g.Wait(); err != nil {
log.Fatal(err) log.Fatal(err)
} }

Loading…
Cancel
Save