init.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/spf13/viper"
  6. "go.uber.org/zap"
  7. "gorm.io/driver/mysql"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/logger"
  10. es "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  13. "os"
  14. "time"
  15. )
  16. func init() {
  17. InitConfig()
  18. InitLog()
  19. InitClickhouse()
  20. InitEs()
  21. //
  22. InitMgo()
  23. InitMgoQY()
  24. InitMysql()
  25. InitAreaCode()
  26. InitLabels() //初始化获取标签和对应bitmap站位
  27. }
  28. func InitConfig() (err error) {
  29. viper.SetConfigFile("config.toml") // 指定配置文件路径
  30. viper.SetConfigName("config") // 配置文件名称(无扩展名)
  31. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  32. viper.AddConfigPath("./")
  33. viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
  34. viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
  35. err = viper.ReadInConfig() // 查找并读取配置文件
  36. if err != nil { // 处理读取配置文件的错误
  37. return
  38. }
  39. err = viper.Unmarshal(&GF)
  40. return err
  41. }
  42. func InitEs() {
  43. if GF.Esa.URL != "" {
  44. Esa = &es.Elastic{
  45. S_esurl: GF.Esa.URL,
  46. I_size: 5,
  47. Username: GF.Esa.Username,
  48. Password: GF.Esa.Password,
  49. }
  50. Esa.InitElasticSize()
  51. }
  52. if GF.Esb.URL != "" {
  53. Esb = &es.Elastic{
  54. S_esurl: GF.Esb.URL,
  55. I_size: 5,
  56. Username: GF.Esb.Username,
  57. Password: GF.Esb.Password,
  58. }
  59. Esb.InitElasticSize()
  60. }
  61. }
  62. func InitLog() {
  63. now := time.Now()
  64. err := log.InitLog(
  65. log.Path(GF.Log.LogPath),
  66. log.Level(GF.Log.LogLevel),
  67. log.Compress(GF.Log.Compress),
  68. log.MaxSize(GF.Log.MaxSize),
  69. log.MaxBackups(GF.Log.MaxBackups),
  70. log.MaxAge(GF.Log.MaxAge),
  71. log.Format(GF.Log.Format),
  72. )
  73. if err != nil {
  74. fmt.Printf("InitLog failed: %v\n", err)
  75. os.Exit(1)
  76. }
  77. log.Info("InitLog", zap.Any("duration", time.Since(now).Seconds()))
  78. }
  79. // InitClickhouse 初始化Clickhouse
  80. func InitClickhouse() {
  81. username := GF.Clickhouse.Username
  82. password := GF.Clickhouse.Password
  83. host := GF.Clickhouse.Host
  84. dbname := GF.Clickhouse.DB
  85. // 测试环境
  86. //host := "192.168.3.207:19000"
  87. //username := "jytop"
  88. //password := "pwdTopJy123"
  89. //dbname := "information"
  90. ClickHouseConn, _ = connectClickhouse(host, username, password, dbname)
  91. }
  92. // InitMgo 初始化标讯,mixdata.qyxy_std
  93. func InitMgo() {
  94. Mgo = &mongodb.MongodbSim{
  95. MongodbAddr: GF.Mongo.Host,
  96. //MongodbAddr: "127.0.0.1:27083",
  97. Size: 10,
  98. DbName: GF.Mongo.DB,
  99. UserName: GF.Mongo.Username,
  100. Password: GF.Mongo.Password,
  101. Direct: GF.Mongo.Direct,
  102. }
  103. Mgo.InitPool()
  104. }
  105. func InitMysql() {
  106. username := GF.Mysql.Username
  107. password := GF.Mysql.Password
  108. host := GF.Mysql.Host // 本地
  109. //host := "172.17.162.27:14000" //线上
  110. database := GF.Mysql.DB
  111. dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, host, database)
  112. // 连接到数据库
  113. var err error
  114. MysqlDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
  115. Logger: logger.Default.LogMode(logger.Error), //不打印日志
  116. })
  117. if err != nil {
  118. log.Info("InitMysql", zap.String("初始化MySQL失败", database), zap.Error(err))
  119. return
  120. } else {
  121. log.Info("InitMysql", zap.String("初始化MySQL成功", database))
  122. }
  123. }
  124. // CodeArea 表示 code_area 数据表的结构体
  125. type CodeArea struct {
  126. ID uint16 `gorm:"primaryKey;autoIncrement;not null"` // id 为主键,自增
  127. Code string `gorm:"size:10;not null;unique"` // code 列,长度 10,唯一
  128. Area string `gorm:"size:45;not null"` // area 列,长度 45,不允许为空
  129. City string `gorm:"size:45"` // city 列,长度 45,可以为空
  130. District string `gorm:"size:45"` // district 列,长度 45,可以为空
  131. Alias string `gorm:"size:10"` // alias 列,长度 10,可以为空
  132. }
  133. // TableName 指定数据库表名
  134. func (CodeArea) TableName() string {
  135. return "code_area"
  136. }
  137. // InitAreaCode 初始化地区code;
  138. func InitAreaCode() {
  139. // 查询所有数据
  140. var codeAreas []CodeArea
  141. MysqlDB.Find(&codeAreas) // 查询所有记录
  142. for _, ca := range codeAreas {
  143. key := ca.Area + "~" + ca.City + "~" + ca.District + "~"
  144. code := ca.Code
  145. RegionCodeData[key] = code
  146. }
  147. }
  148. func InitMgoQY() {
  149. //181 凭安库
  150. MgoQY = &mongodb.MongodbSim{
  151. //MongodbAddr: "172.17.4.181:27001",
  152. MongodbAddr: GF.MongoQy.Host,
  153. DbName: GF.MongoQy.DB,
  154. Size: 10,
  155. UserName: GF.MongoQy.Username,
  156. Password: GF.MongoQy.Password,
  157. Direct: GF.MongoQy.Direct,
  158. }
  159. MgoQY.InitPool()
  160. log.Info("InitMgoQY", zap.String("初始化成功", GF.MongoQy.Host))
  161. }
  162. // InitLabels 获取现有设计好的标签以及对应bitmap 占位
  163. func InitLabels() {
  164. log.Info("getLabels", zap.String("开始处理标签", "-------"))
  165. type entLabel struct {
  166. LabelName string
  167. BitmapNum uint64
  168. }
  169. ctx := context.Background()
  170. sql := `
  171. SELECT label_name,bitmap_num FROM ent_label
  172. `
  173. rows, err := ClickHouseConn.Query(ctx, sql)
  174. if err != nil {
  175. log.Info("getLabels", zap.Error(err))
  176. }
  177. // 遍历结果集
  178. for rows.Next() {
  179. var label entLabel
  180. // 将当前行的值扫描到结构体中
  181. err = rows.Scan(&label.LabelName, &label.BitmapNum)
  182. if err != nil {
  183. log.Info("getLabels", zap.Error(err))
  184. continue
  185. }
  186. entLabelMap[label.BitmapNum] = label.LabelName
  187. nameBitMap[label.LabelName] = label.BitmapNum
  188. }
  189. // 检查是否有任何错误
  190. if err = rows.Err(); err != nil {
  191. log.Info("getLabels", zap.Error(err))
  192. }
  193. log.Info("getLabels", zap.String("标签处理完毕", "-------"))
  194. }