init.go 7.8 KB

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