|
|
package main
|
|
|
|
|
|
import (
|
|
|
"bufio"
|
|
|
"bytes"
|
|
|
"dsTools/Utils/DbUtil"
|
|
|
"dsTools/Utils/FileUtil"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"os"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
func main() {
|
|
|
//1、定义参加创建默认mapping的表有哪些
|
|
|
var createDefaultMappingTables []string
|
|
|
//读取
|
|
|
f, err := os.Open("./Config/createDefaultMapping.txt")
|
|
|
if err != nil {
|
|
|
fmt.Println("没有找到创建默认mapping的数据文件createDefaultMapping.txt!")
|
|
|
return
|
|
|
}
|
|
|
buf := bufio.NewReader(f)
|
|
|
var shouldBreak = false
|
|
|
for {
|
|
|
line, err := buf.ReadString('\n')
|
|
|
if err != nil || io.EOF == err {
|
|
|
shouldBreak = true
|
|
|
}
|
|
|
line = strings.TrimSpace(line)
|
|
|
if strings.Index(line, "#") < 0 && len(line) > 0 {
|
|
|
createDefaultMappingTables = append(createDefaultMappingTables, line)
|
|
|
}
|
|
|
if shouldBreak {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
//头部
|
|
|
head := `{
|
|
|
"settings": {
|
|
|
"number_of_shards": 5,
|
|
|
"index.refresh_interval": "5s"
|
|
|
},
|
|
|
"aliases": [
|
|
|
"#indexName#"
|
|
|
],
|
|
|
"mappings": {
|
|
|
"properties": {`
|
|
|
|
|
|
tail := `}}}`
|
|
|
//遍历每一个表
|
|
|
for i := 0; i < len(createDefaultMappingTables); i++ {
|
|
|
tableName := createDefaultMappingTables[i]
|
|
|
//到mysql中查询
|
|
|
sql := "desc " + tableName
|
|
|
list, err := DbUtil.Engine.QueryInterface(sql)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
}
|
|
|
//这个是在数据中心区别各地上报数据的主键字段,是拼接出来的
|
|
|
str := `"id":{"type":"keyword"},`
|
|
|
//处理每一个字段
|
|
|
for j := 0; j < len(list); j++ {
|
|
|
c := list[j]
|
|
|
field := string(c["Field"].([]byte))
|
|
|
Type := string(c["Type"].([]byte))
|
|
|
Key := string(c["Key"].([]byte))
|
|
|
//如果是主键,那么mapping的字段名称为pk
|
|
|
if Key == "PRI" {
|
|
|
field = "pk"
|
|
|
}
|
|
|
if strings.Index(Type, "int") >= 0 {
|
|
|
str += `"` + field + `": {
|
|
|
"type": "long"
|
|
|
},`
|
|
|
} else if (strings.Index(Type, "datetime")) >= 0 {
|
|
|
str += `"` + field + `": {
|
|
|
"format":"yyyy-MM-dd HH:mm:ss",
|
|
|
"type": "date"
|
|
|
},`
|
|
|
} else if (strings.Index(Type, "date")) >= 0 {
|
|
|
str += `"` + field + `": {
|
|
|
"format":"yyyy-MM-dd",
|
|
|
"type": "date"
|
|
|
},`
|
|
|
} else if (strings.Index(Type, "timestamp")) >= 0 {
|
|
|
str += `"` + field + `": {
|
|
|
"format":"yyyy-MM-dd HH:mm:ss",
|
|
|
"type": "date"
|
|
|
},`
|
|
|
} else {
|
|
|
str += `"` + field + `": {"type": "keyword"},`
|
|
|
}
|
|
|
}
|
|
|
//添加表名
|
|
|
str += `"table_name":{"type": "keyword"}`
|
|
|
//替换别名
|
|
|
head=strings.Replace(head,"#indexName#",tableName,-1)
|
|
|
//输出
|
|
|
p := "MappingDefaultJson/"
|
|
|
//添加头与尾
|
|
|
mappingStr := head + str + tail
|
|
|
|
|
|
//格式化json
|
|
|
var str2 bytes.Buffer
|
|
|
_ = json.Indent(&str2, []byte(mappingStr), "", " ")
|
|
|
//写入json文件
|
|
|
FileUtil.WriteToFile(p+tableName+".json", str2.String())
|
|
|
fmt.Println("正在生成" + tableName + "...")
|
|
|
}
|
|
|
fmt.Println("恭喜,所有初始化mapping文件成功生成!")
|
|
|
}
|