main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. es7 "github.com/olivere/elastic/v7"
  6. "github.com/spf13/viper"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/mongo/options"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  10. "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  12. "log"
  13. "sync"
  14. "time"
  15. )
  16. var (
  17. MongoBase *mongodb.MongodbSim
  18. MongoStd *mongodb.MongodbSim
  19. GF GlobalConf
  20. Es *elastic.Elastic
  21. EsClient *es7.Client
  22. //更新es
  23. updateEsPool = make(chan []map[string]interface{}, 5000)
  24. updateEsSp = make(chan bool, 5) //保存协程
  25. // 更新mongo
  26. //updatePool = make(chan []map[string]interface{}, 5000)
  27. //updateSp = make(chan bool, 5)
  28. )
  29. func InitConfig() (err error) {
  30. viper.SetConfigFile("config.toml") // 指定配置文件路径
  31. viper.SetConfigName("config") // 配置文件名称(无扩展名)
  32. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  33. viper.AddConfigPath("./")
  34. viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
  35. viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
  36. err = viper.ReadInConfig() // 查找并读取配置文件
  37. if err != nil { // 处理读取配置文件的错误
  38. return
  39. }
  40. err = viper.Unmarshal(&GF)
  41. return err
  42. }
  43. func InitES() {
  44. //Es = &elastic.Elastic{
  45. // //S_esurl: "http://127.0.0.1:19805",
  46. // S_esurl: "http://172.17.4.184:19805",
  47. // I_size: 5,
  48. // Username: "es_all",
  49. // Password: "TopJkO2E_d1x",
  50. //}
  51. //Es.InitElasticSize()
  52. //Es = &elastic.Elastic{
  53. // //S_esurl: "http://127.0.0.1:19805",
  54. // S_esurl: "http://192.168.3.149:9201",
  55. // I_size: 5,
  56. // Username: "",
  57. // Password: "",
  58. //}
  59. //Es.InitElasticSize()
  60. //url := "http://127.0.0.1:19805"
  61. url := GF.ES.URL
  62. username := GF.ES.Username
  63. password := GF.ES.Password
  64. // 创建 Elasticsearch 客户端
  65. EsClient, _ = es7.NewClient(
  66. es7.SetURL(url),
  67. es7.SetBasicAuth(username, password),
  68. es7.SetSniff(false),
  69. )
  70. fmt.Println(EsClient)
  71. }
  72. func InitMgo() {
  73. //qyxy_std
  74. MongoStd = &mongodb.MongodbSim{
  75. //MongodbAddr: "127.0.0.1:27083",
  76. MongodbAddr: GF.MongoStd.Host,
  77. Size: 10,
  78. DbName: GF.MongoStd.DB,
  79. UserName: GF.MongoStd.Username,
  80. Password: GF.MongoStd.Password,
  81. Direct: GF.MongoStd.Direct,
  82. }
  83. MongoStd.InitPool()
  84. //company_base
  85. MongoBase = &mongodb.MongodbSim{
  86. //MongodbAddr: "127.0.0.1:27001",
  87. MongodbAddr: GF.MongoBase.Host,
  88. Size: 10,
  89. DbName: GF.MongoBase.DB,
  90. UserName: GF.MongoBase.Username,
  91. Password: GF.MongoBase.Password,
  92. Direct: GF.MongoBase.Direct,
  93. }
  94. MongoBase.InitPool()
  95. }
  96. func main() {
  97. InitConfig()
  98. InitMgo()
  99. InitES()
  100. go updateEsMethod()
  101. sess := MongoStd.GetMgoConn()
  102. defer MongoStd.DestoryMongoConn(sess)
  103. autoid := int64(GF.Env.Autoid)
  104. endid := int64(GF.Env.Endid)
  105. ctx, _ := context.WithTimeout(context.Background(), 99999*time.Hour)
  106. coll := sess.M.C.Database("mixdata").Collection("qyxy_std")
  107. find := options.Find().SetBatchSize(1000).SetSort(bson.D{bson.E{"autoid", 1}}).SetProjection(bson.M{"_id": 1, "autoid": 1, "nseo_id": 1})
  108. cur, err := coll.Find(ctx, bson.M{"autoid": bson.M{"$gt": autoid, "$lte": endid}}, find)
  109. if err != nil {
  110. log.Println(err)
  111. } else {
  112. log.Println("autoid:", autoid, "endid: ", endid)
  113. }
  114. count := 0
  115. ch := make(chan bool, 10)
  116. wg := &sync.WaitGroup{}
  117. for tmp := make(map[string]interface{}); cur.Next(ctx); count++ {
  118. if cur != nil {
  119. cur.Decode(&tmp)
  120. }
  121. autoid = util.Int64All(tmp["autoid"])
  122. if count%10000 == 0 {
  123. log.Println("current ", count, autoid, tmp["_id"])
  124. }
  125. if autoid == 0 {
  126. continue
  127. }
  128. ch <- true
  129. wg.Add(1)
  130. go func(tmp map[string]interface{}) {
  131. defer func() {
  132. <-ch
  133. wg.Done()
  134. }()
  135. companyId := util.ObjToString(tmp["_id"])
  136. stdUpdate := make(map[string]interface{}, 0)
  137. stdUpdate["nseo_id"] = tmp["nseo_id"]
  138. stdUpdate["autoid"] = tmp["autoid"]
  139. //
  140. ////更新MongoDB
  141. //updatePool <- []map[string]interface{}{
  142. // {"_id": companyId},
  143. // {"$set": stdUpdate},
  144. //}
  145. //
  146. // 更新es
  147. updateEsPool <- []map[string]interface{}{
  148. {"_id": companyId},
  149. stdUpdate,
  150. }
  151. }(tmp)
  152. tmp = make(map[string]interface{})
  153. }
  154. wg.Wait()
  155. log.Println("over ---- ", count)
  156. select {}
  157. }
  158. //updateEsMethod 更新es
  159. func updateEsMethod() {
  160. arru := make([][]map[string]interface{}, 1000)
  161. indexu := 0
  162. ctx := context.Background()
  163. for {
  164. select {
  165. case v := <-updateEsPool:
  166. arru[indexu] = v
  167. indexu++
  168. if indexu == 1000 {
  169. updateEsSp <- true
  170. go func(arru [][]map[string]interface{}) {
  171. defer func() {
  172. <-updateEsSp
  173. }()
  174. UpdateBulk(EsClient, ctx, "qyxy", arru...)
  175. }(arru)
  176. arru = make([][]map[string]interface{}, 1000)
  177. indexu = 0
  178. }
  179. case <-time.After(1000 * time.Millisecond):
  180. if indexu > 0 {
  181. updateEsSp <- true
  182. go func(arru [][]map[string]interface{}) {
  183. defer func() {
  184. <-updateEsSp
  185. }()
  186. UpdateBulk(EsClient, ctx, "qyxy", arru...)
  187. }(arru[:indexu])
  188. arru = make([][]map[string]interface{}, 1000)
  189. indexu = 0
  190. }
  191. }
  192. }
  193. }
  194. func UpdateBulk(client *es7.Client, c context.Context, index string, docs ...[]map[string]interface{}) (err error) {
  195. bulkService := client.Bulk().Index(index).Type("_doc")
  196. for _, d := range docs {
  197. id := d[0]["_id"].(string)
  198. doc := es7.NewBulkUpdateRequest().Id(id).Doc(d[1])
  199. bulkService.Add(doc)
  200. }
  201. _, err = bulkService.Do(c)
  202. if err != nil {
  203. fmt.Printf("UpdateBulk all success err is %v\n", err)
  204. }
  205. return
  206. }