init.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // qyxy_std 数据
  95. Mgo = &mongodb.MongodbSim{
  96. MongodbAddr: GF.Mongo.Host,
  97. //MongodbAddr: "127.0.0.1:27083",
  98. Size: 10,
  99. DbName: GF.Mongo.DB,
  100. UserName: GF.Mongo.Username,
  101. Password: GF.Mongo.Password,
  102. Direct: GF.Mongo.Direct,
  103. }
  104. Mgo.InitPool()
  105. }
  106. func InitMysql() {
  107. username := GF.Mysql.Username
  108. password := GF.Mysql.Password
  109. host := GF.Mysql.Host // 本地
  110. //host := "172.17.162.27:14000" //线上
  111. database := GF.Mysql.DB
  112. dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, host, database)
  113. // 连接到数据库
  114. var err error
  115. MysqlDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
  116. Logger: logger.Default.LogMode(logger.Error), //不打印日志
  117. })
  118. if err != nil {
  119. log.Info("InitMysql", zap.String("初始化MySQL失败", database), zap.Error(err))
  120. return
  121. } else {
  122. log.Info("InitMysql", zap.String("初始化MySQL成功", database))
  123. }
  124. }
  125. // CodeArea 表示 code_area 数据表的结构体
  126. type CodeArea struct {
  127. ID uint16 `gorm:"primaryKey;autoIncrement;not null"` // id 为主键,自增
  128. Code string `gorm:"size:10;not null;unique"` // code 列,长度 10,唯一
  129. Area string `gorm:"size:45;not null"` // area 列,长度 45,不允许为空
  130. City string `gorm:"size:45"` // city 列,长度 45,可以为空
  131. District string `gorm:"size:45"` // district 列,长度 45,可以为空
  132. Alias string `gorm:"size:10"` // alias 列,长度 10,可以为空
  133. }
  134. // TableName 指定数据库表名
  135. func (CodeArea) TableName() string {
  136. return "code_area"
  137. }
  138. // InitAreaCode 初始化地区code;
  139. func InitAreaCode() {
  140. // 查询所有数据
  141. var codeAreas []CodeArea
  142. MysqlDB.Find(&codeAreas) // 查询所有记录
  143. for _, ca := range codeAreas {
  144. key := ca.Area + "~" + ca.City + "~" + ca.District + "~"
  145. code := ca.Code
  146. RegionCodeData[key] = code
  147. }
  148. }
  149. func InitMgoQY() {
  150. //181 凭安库
  151. MgoQY = &mongodb.MongodbSim{
  152. //MongodbAddr: "172.17.4.181:27001",
  153. MongodbAddr: GF.MongoQy.Host,
  154. DbName: GF.MongoQy.DB,
  155. Size: 10,
  156. UserName: GF.MongoQy.Username,
  157. Password: GF.MongoQy.Password,
  158. Direct: GF.MongoQy.Direct,
  159. }
  160. MgoQY.InitPool()
  161. log.Info("InitMgoQY", zap.String("初始化成功", GF.MongoQy.Host))
  162. }
  163. // InitLabels 获取现有设计好的标签以及对应bitmap 占位
  164. func InitLabels() {
  165. log.Info("getLabels", zap.String("开始处理标签", "-------"))
  166. type entLabel struct {
  167. LabelName string
  168. BitmapNum uint64
  169. }
  170. ctx := context.Background()
  171. sql := `
  172. SELECT label_name,bitmap_num FROM ent_label
  173. `
  174. rows, err := ClickHouseConn.Query(ctx, sql)
  175. if err != nil {
  176. log.Info("getLabels", zap.Error(err))
  177. }
  178. // 遍历结果集
  179. for rows.Next() {
  180. var label entLabel
  181. // 将当前行的值扫描到结构体中
  182. err = rows.Scan(&label.LabelName, &label.BitmapNum)
  183. if err != nil {
  184. log.Info("getLabels", zap.Error(err))
  185. continue
  186. }
  187. entLabelMap[label.BitmapNum] = label.LabelName
  188. nameBitMap[label.LabelName] = label.BitmapNum
  189. }
  190. // 检查是否有任何错误
  191. if err = rows.Err(); err != nil {
  192. log.Info("getLabels", zap.Error(err))
  193. }
  194. log.Info("getLabels", zap.String("标签处理完毕", "-------"))
  195. }