main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package main
  2. import (
  3. "log"
  4. "mgo_tidb_update/config"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. )
  8. func do() {
  9. tag_subinformation := ""
  10. tag_subinformation_arr := []string{"物业1", "物业2"}
  11. for k, v := range tag_subinformation_arr {
  12. tag_subinformation += v
  13. if k != len(tag_subinformation_arr)-1 {
  14. tag_subinformation += ","
  15. }
  16. }
  17. log.Println(tag_subinformation)
  18. }
  19. func do2() {
  20. // ok := config.Tidb.Update("dws_f_bid_baseinfo", map[string]interface{}{
  21. // "infoid": "5ad8c18fa5cb26b9b72ce09d",
  22. // }, map[string]interface{}{
  23. // "tag_topinformation": "物业",
  24. // })
  25. // log.Println(ok)
  26. // return
  27. values := []string{"infoid", "tag_topinformation"}
  28. args := [][]interface{}{
  29. []interface{}{"5ad8c18fa5cb26b9b72ce09d", "物业1"},
  30. []interface{}{"5ad8c1e9a5cb26b9b72d01c0", "物业2"},
  31. []interface{}{"5ad8c1e9a5cb26b9b72d01cb", "物业1,物业2"},
  32. []interface{}{"123", "物业11"},
  33. }
  34. config.Tidb.UpdateBath("dws_f_bid_baseinfo", values, args)
  35. log.Println(config.Tidb.SelectBySql(`select infoid,tag_topinformation from dws_f_bid_baseinfo limit 1`))
  36. }
  37. func initBidding() {
  38. query := map[string]interface{}{
  39. "tag_topinformation": map[string]interface{}{
  40. "$exists": true,
  41. },
  42. }
  43. config.Mgo_Bidding.Count("bidding", query)
  44. return
  45. m := map[string]int{}
  46. session := config.Mgo_Bidding.GetMgoConn()
  47. defer config.Mgo_Bidding.DestoryMongoConn(session)
  48. iter := session.DB("qfw_data").C("bidding").Find(query).Sort("_id").Iter()
  49. ct := 0
  50. for thisData := map[string]interface{}{}; iter.Next(&thisData); {
  51. ct++
  52. if ct%50000 == 0 {
  53. log.Println("已加载", ct)
  54. }
  55. _id := mongodb.BsonIdToSId(thisData["_id"])
  56. m[_id]++
  57. thisData = map[string]interface{}{}
  58. }
  59. log.Println(len(m))
  60. }
  61. func main2() {
  62. values := []string{"infoid", "tag_topinformation"}
  63. config.Mgo_Bidding.GetMgoConn()
  64. defer config.Mgo_Bidding.DestoryMongoConn(session)
  65. args := [][]interface{}{}
  66. // do2()
  67. // return
  68. values := []string{"infoid", "tag_topinformation"}
  69. session := config.Mgo_Bidding.GetMgoConn()
  70. defer config.Mgo_Bidding.DestoryMongoConn(session)
  71. args := [][]interface{}{}
  72. iter := session.DB("qfw_data").C("bidding").Find(nil).Sort("_id").Iter()
  73. ct := 0
  74. for thisData := map[string]interface{}{}; iter.Next(&thisData); {
  75. ct++
  76. if ct%50000 == 0 {
  77. log.Println("已加载", ct)
  78. }
  79. _id := mongodb.BsonIdToSId(thisData["_id"])
  80. if thisData["tag_topinformation"] != nil {
  81. tag_topinformation := ""
  82. tag_topinformation_arr := gconv.Strings(thisData["tag_topinformation"])
  83. for k, v := range tag_topinformation_arr {
  84. tag_topinformation += v
  85. if k != len(tag_topinformation_arr)-1 {
  86. tag_topinformation += ","
  87. }
  88. }
  89. log.Println(_id)
  90. args = append(args, []interface{}{_id, tag_topinformation})
  91. }
  92. if ct%100 == 0 {
  93. if len(args) > 0 {
  94. config.Tidb.UpdateBath("dws_f_bid_baseinfo", values, args)
  95. args = [][]interface{}{}
  96. }
  97. }
  98. thisData = map[string]interface{}{}
  99. }
  100. if len(args) > 0 {
  101. config.Tidb.UpdateBath("dws_f_bid_baseinfo", values, args)
  102. args = [][]interface{}{}
  103. }
  104. log.Println("结束")
  105. }
  106. func main() {
  107. do()
  108. return
  109. limit := 200
  110. c := 0
  111. values := []string{"infoid", "tag_topinformation"}
  112. args := [][]interface{}{}
  113. bl := false
  114. lastid := int64(0)
  115. // lastid = int64(16547665)
  116. // lastid = int64(16547574)
  117. for {
  118. config.Tidb.SelectByBath(limit, func(l *[]map[string]interface{}) bool {
  119. for _, v := range *l {
  120. lastid = gconv.Int64(v["id"])
  121. c++
  122. if c%10000 == 0 {
  123. log.Println("已加载", c, lastid)
  124. }
  125. infoid := gconv.String(v["infoid"])
  126. d, ok := config.Mgo_Bidding.FindById("bidding", infoid, `["tag_topinformation":1}`)
  127. if ok && d != nil && len(*d) > 0 {
  128. if (*d)["tag_topinformation"] != nil {
  129. tag_topinformation := ""
  130. tag_topinformation_arr := gconv.Strings((*d)["tag_topinformation"])
  131. for k, v := range tag_topinformation_arr {
  132. tag_topinformation += v
  133. if k != len(tag_topinformation_arr)-1 {
  134. tag_topinformation += ","
  135. }
  136. }
  137. args = append(args, []interface{}{infoid, tag_topinformation})
  138. }
  139. }
  140. if len(args) == 50 {
  141. config.Tidb.UpdateBath("dws_f_bid_baseinfo", values, args)
  142. args = [][]interface{}{}
  143. }
  144. }
  145. if len(*l) < limit {
  146. bl = true
  147. }
  148. return true
  149. }, `SELECT infoid,id FROM dws_f_bid_baseinfo where id > ? order by id LIMIT ? `, lastid, limit)
  150. if bl {
  151. break
  152. }
  153. }
  154. if len(args) > 0 {
  155. config.Tidb.UpdateBath("dws_f_bid_baseinfo", values, args)
  156. args = [][]interface{}{}
  157. }
  158. log.Println("结束", lastid)
  159. }