init.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/elastic"
  5. "app.yhyue.com/data_processing/common_utils/log"
  6. "app.yhyue.com/data_processing/common_utils/mongodb"
  7. "app.yhyue.com/data_processing/common_utils/mysqldb"
  8. "context"
  9. "esindex/config"
  10. "fmt"
  11. es7 "github.com/olivere/elastic/v7"
  12. "go.uber.org/zap"
  13. "os"
  14. "strings"
  15. "time"
  16. )
  17. var (
  18. ProjectField = make(map[string]string, 500) //项目字段
  19. ProjectListF = make(map[string]string, 200)
  20. BiddingField = make(map[string]string, 200) //bidding_processing_field, level=1 最外层字段,
  21. BiddingLevelField = make(map[string]map[string]string) //level=2 的第二层字段
  22. )
  23. // InitLog @Description
  24. // @Author J 2022/7/26 15:30
  25. func InitLog() {
  26. now := time.Now()
  27. logcfg := config.Conf.Log
  28. err := log.InitLog(
  29. log.Path(logcfg.LogPath),
  30. log.Level(logcfg.LogLevel),
  31. log.Compress(logcfg.Compress),
  32. log.MaxSize(logcfg.MaxSize),
  33. log.MaxBackups(logcfg.MaxBackups),
  34. log.MaxAge(logcfg.MaxAge),
  35. log.Format(logcfg.Format),
  36. )
  37. if err != nil {
  38. fmt.Printf("InitLog failed: %v\n", err)
  39. os.Exit(1)
  40. }
  41. log.Info("InitLog", zap.Any("duration", time.Since(now)))
  42. }
  43. func InitMgo() {
  44. now := time.Now()
  45. MgoB = &mongodb.MongodbSim{
  46. MongodbAddr: config.Conf.DB.MongoB.Addr,
  47. DbName: config.Conf.DB.MongoB.Dbname,
  48. Size: config.Conf.DB.MongoB.Size,
  49. UserName: config.Conf.DB.MongoB.User,
  50. Password: config.Conf.DB.MongoB.Password,
  51. }
  52. MgoB.InitPool()
  53. MgoP = &mongodb.MongodbSim{
  54. MongodbAddr: config.Conf.DB.MongoP.Addr,
  55. DbName: config.Conf.DB.MongoP.Dbname,
  56. Size: config.Conf.DB.MongoP.Size,
  57. UserName: config.Conf.DB.MongoP.User,
  58. Password: config.Conf.DB.MongoP.Password,
  59. }
  60. MgoP.InitPool()
  61. MgoQ = &mongodb.MongodbSim{
  62. MongodbAddr: config.Conf.DB.MongoQ.Addr,
  63. DbName: config.Conf.DB.MongoQ.Dbname,
  64. Size: config.Conf.DB.MongoQ.Size,
  65. UserName: config.Conf.DB.MongoQ.User,
  66. Password: config.Conf.DB.MongoQ.Password,
  67. }
  68. MgoQ.InitPool()
  69. MysqlB = &mysqldb.Mysql{
  70. Address: config.Conf.DB.MysqlB.Addr,
  71. DBName: config.Conf.DB.MysqlB.Dbname,
  72. UserName: config.Conf.DB.MysqlB.Username,
  73. PassWord: config.Conf.DB.MysqlB.Password,
  74. }
  75. MysqlB.Init()
  76. log.Info("InitMgo", zap.Any("duration", time.Since(now)))
  77. }
  78. func InitEs() {
  79. now := time.Now()
  80. Es = &elastic.Elastic{
  81. S_esurl: config.Conf.DB.Es.Addr,
  82. I_size: config.Conf.DB.Es.Size,
  83. Username: config.Conf.DB.Es.Username,
  84. Password: config.Conf.DB.Es.Password,
  85. }
  86. Es.InitElasticSize()
  87. Es1 = &elastic.Elastic{
  88. S_esurl: config.Conf.DB.Es.AddrP,
  89. I_size: config.Conf.DB.Es.Size,
  90. Username: config.Conf.DB.Es.Username,
  91. Password: config.Conf.DB.Es.Password,
  92. }
  93. Es1.InitElasticSize()
  94. log.Info("InitEs", zap.Any("duration", time.Since(now)))
  95. }
  96. func InitField() {
  97. now := time.Now()
  98. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "project"}`, nil, nil, false, -1, -1)
  99. if len(*info) > 0 {
  100. for _, m := range *info {
  101. if util.IntAll(m["level"]) == 1 {
  102. ProjectField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  103. } else if util.IntAll(m["level"]) == 2 {
  104. ProjectListF[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  105. }
  106. }
  107. }
  108. log.Info("InitField", zap.Int("ProjectField", len(ProjectField)), zap.Int("ProjectListF", len(ProjectListF)))
  109. log.Info("InitField", zap.Any("duration", time.Since(now)))
  110. }
  111. //InitEsBiddingField 初始化 bidding 索引字段
  112. func InitEsBiddingField() {
  113. now := time.Now()
  114. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "bidding"}`, nil, nil, false, -1, -1)
  115. if len(*info) > 0 {
  116. for _, m := range *info {
  117. if util.IntAll(m["level"]) == 1 {
  118. BiddingField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  119. } else if util.IntAll(m["level"]) == 2 {
  120. pfield := util.ObjToString(m["pfield"])
  121. pfieldMap := BiddingLevelField[pfield]
  122. if pfieldMap == nil {
  123. pfieldMap = make(map[string]string, 0)
  124. }
  125. pfieldMap[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  126. BiddingLevelField[pfield] = pfieldMap
  127. }
  128. }
  129. }
  130. log.Info("InitEsBiddingField", zap.Int("BiddingField es 一级字段数量", len(BiddingField)))
  131. log.Info("InitEsBiddingField", zap.Int("BiddingLevelField es 二级字段数量", len(BiddingLevelField)))
  132. log.Info("InitEsBiddingField", zap.Any("duration", time.Since(now)))
  133. }
  134. //verifyESFields 验证es 定义字段类型和 MongoDB 数据字段
  135. func verifyESFields() {
  136. now := time.Now()
  137. log.Info("verifyESFields", zap.String("开始类型检测", ""))
  138. client, _ := es7.NewClient(
  139. es7.SetURL(config.Conf.DB.Es.Addr),
  140. es7.SetBasicAuth(config.Conf.DB.Es.Username, config.Conf.DB.Es.Password),
  141. es7.SetSniff(false),
  142. )
  143. index := config.Conf.DB.Es.IndexB //索引表 bidding
  144. // 获取 Elasticsearch 索引的 mapping 信息
  145. mapping, err := client.GetMapping().Index(index).Do(context.Background())
  146. if err != nil {
  147. log.Info("verifyESFields", zap.Any("getting Elasticsearch mapping:", err))
  148. }
  149. indexName, _ := GetIndexName(client, index)
  150. if indexName == "" {
  151. log.Info("verifyESFields", zap.String("索引不存在,请检查索引", index))
  152. return
  153. }
  154. properties := mapping[indexName].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})
  155. var errField = make([]string, 0)
  156. var okField = make([]string, 0)
  157. var analyzerMap = make(map[string]string) // 分词信息
  158. var esMap = make(map[string]string) //存储es 字段类型
  159. //
  160. for field, ftype := range BiddingField {
  161. eftypeMap, _ := properties[field].(map[string]interface{})
  162. var etype string
  163. var analyzer string
  164. if fftype, ok := eftypeMap["type"]; ok {
  165. etype = fftype.(string)
  166. esMap[field] = etype
  167. }
  168. if ffanalyzer, ok := eftypeMap["analyzer"]; ok {
  169. analyzer = ffanalyzer.(string)
  170. analyzerMap[field] = analyzer
  171. }
  172. if ftype != "" {
  173. if chargeType(ftype, etype) {
  174. okField = append(okField, field)
  175. } else {
  176. errField = append(errField, field)
  177. }
  178. } else {
  179. if field == "_id" {
  180. continue
  181. } else if field == "purchasinglist" || field == "package" || field == "winnerorder" || field == "procurementlist" {
  182. if eproperties, ok := eftypeMap["properties"]; ok {
  183. if eproMap, ok := eproperties.(map[string]interface{}); ok {
  184. for k, v := range eproMap {
  185. if innerMap, ok := v.(map[string]interface{}); ok {
  186. if innerType, ok := innerMap["type"]; ok {
  187. innerLevel := BiddingLevelField[field]
  188. esMap[fmt.Sprintf("%s.%s", field, k)] = innerType.(string)
  189. if chargeType(innerLevel[k], innerType.(string)) {
  190. okField = append(okField, fmt.Sprintf("%s.%s", field, k))
  191. } else {
  192. errField = append(errField, fmt.Sprintf("%s.%s", field, k))
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. if len(errField) > 0 {
  203. log.Info("verifyESFields", zap.Int("错误字段数量", len(errField)))
  204. for _, field := range errField {
  205. if strings.Contains(field, ".") {
  206. fe := strings.Split(field, ".")
  207. log.Info(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingLevelField[fe[0]][fe[1]]), esMap[field]))
  208. } else {
  209. log.Info(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingField[field]), esMap[field]))
  210. }
  211. }
  212. } else {
  213. log.Info("es 字段类型检测结束,", zap.Int("所有字段都符合,检测字段数量为:", len(okField)))
  214. }
  215. log.Info("verifyESFields", zap.Any("duration", time.Since(now)))
  216. }
  217. func GetIndexName(client *es7.Client, name string) (string, error) {
  218. // 判断 name 是否为一个别名
  219. res, err := client.Aliases().Alias(name).Do(context.Background())
  220. if err != nil {
  221. // 错误处理
  222. if err.(*es7.Error).Status != 404 && err.(*es7.Error).Details != nil {
  223. return "", err
  224. }
  225. }
  226. if res != nil {
  227. for k, v := range res.Indices {
  228. for _, vv := range v.Aliases {
  229. if vv.AliasName == name {
  230. return k, nil
  231. }
  232. }
  233. }
  234. }
  235. // 判断 name 是否为一个正式索引名称
  236. resa, err := client.IndexExists(name).Do(context.Background())
  237. if err != nil {
  238. // 错误处理
  239. return "", err
  240. }
  241. if resa {
  242. return name, nil
  243. }
  244. // 如果 name 既不是别名,也不是正式索引名称,则返回空字符串
  245. return "", nil
  246. }