init.go 8.5 KB

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