master
zhangjun 5 years ago
parent b1b517b01b
commit 9345f2dd72

@ -18,56 +18,56 @@ func init() {
//初始化ES Client使用配置文件
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()
}else{
return RedisUtil.RedisClient.Get("ES_Mapping_" + indexName + ":" + itemName).Result()
} else {
//获取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
for k, v := range res[indexName].(map[string]interface{}){
for k, v := range res[indexName].(map[string]interface{}) {
if k=="mappings"{
for k2, v2 := range v.(map[string]interface{}){
if k2=="properties"{
if k == "mappings" {
for k2, v2 := range v.(map[string]interface{}) {
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"{
for k4, v4 := range v3.(map[string]interface{}) {
for k5, _ := range v4.(map[string]interface{}) {
if k3 == "data_content" {
for k4, v4 := range v3.(map[string]interface{}) {
for k5, _ := range v4.(map[string]interface{}) {
if k4=="fields"{
ES7Type[indexName]["data_content."+k5]="data_content."+k5+".keyword"
}else{
ES7Type[indexName]["data_content."+k5]="data_content."+k5
if k4 == "fields" {
ES7Type[indexName]["data_content."+k5] = "data_content." + k5 + ".keyword"
} else {
ES7Type[indexName]["data_content."+k5] = "data_content." + k5
}
}
}
}else{
for k4, _ := range v3.(map[string]interface{}) {
} else {
for k4, _ := range v3.(map[string]interface{}) {
if k4=="fields"{
ES7Type[indexName][k3]=k3+".keyword"
}else{
ES7Type[indexName][k3]=k3
if k4 == "fields" {
ES7Type[indexName][k3] = k3 + ".keyword"
} else {
ES7Type[indexName][k3] = k3
}
}
}
@ -80,7 +80,7 @@ func GetES7Type(indexName string,itemName string) (string,error) {
//写入Redis 缓存
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)
}
@ -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/ConfigUtil"
"dsDataex/Utils/ES7SqlUtil"
"dsDataex/Utils/ES7TypeUtil"
"dsDataex/Utils/ES7Util"
_ "dsDataex/docs"
"fmt"
@ -30,7 +29,7 @@ var (
g errgroup.Group
)
func GrpcServerInit(){
func GrpcServerInit() {
s = grpc.NewServer()
@ -39,7 +38,7 @@ func GrpcServerInit(){
reflection.Register(s)
}
func GinServerInit(){
func GinServerInit() {
gin.SetMode(gin.DebugMode)
@ -95,6 +94,8 @@ func main() {
CacheUtil.OrgtreeCacheInit()
//fmt.Println(ES7TypeUtil.GetES7Type("safety","data_id"))
GinServerInit()
g.Go(func() error {
@ -106,15 +107,13 @@ func main() {
GrpcServerInit()
g.Go(func() error {
lis, _ := net.Listen("tcp", ":" + ConfigUtil.ProjectGrpc)
lis, _ := net.Listen("tcp", ":"+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 {
log.Fatal(err)
}

Loading…
Cancel
Save