|
@@ -11,45 +11,41 @@ import (
|
|
|
es_elastic "qfw/common/src/gopkg.in/olivere/elastic.v1"
|
|
|
)
|
|
|
|
|
|
-func startFusionData() {
|
|
|
+func exportFusionMongoData() {
|
|
|
+
|
|
|
start := int(time.Now().Unix())
|
|
|
- log.Println("开始遍历索引-进行融合............")
|
|
|
+ log.Println("开始导出融合组数据......")
|
|
|
//遍历索引
|
|
|
esclient := elastic.GetEsConn()
|
|
|
defer elastic.DestoryEsConn(esclient)
|
|
|
|
|
|
if esclient == nil {
|
|
|
- log.Fatalln("连接池异常")
|
|
|
+ log.Println("连接池异常")
|
|
|
}
|
|
|
q :=es_elastic.NewBoolQuery()
|
|
|
cursor, err := esclient.Scan(esIndex).Query(es_elastic.NewBoolQuery().Must(q)).
|
|
|
Size(200).Do()
|
|
|
if err != nil {
|
|
|
- log.Fatal("cursor",err)
|
|
|
+ log.Println("cursor",err)
|
|
|
}
|
|
|
if cursor.Results == nil {
|
|
|
- log.Fatalf("results != nil; got nil")
|
|
|
+ log.Println("results != nil; got nil")
|
|
|
}
|
|
|
if cursor.Results.Hits == nil {
|
|
|
- log.Fatalf("expected results.Hits != nil; got nil")
|
|
|
+ log.Println("expected results.Hits != nil; got nil")
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
log.Println("查询正常-总数:",cursor.TotalHits())
|
|
|
-
|
|
|
-
|
|
|
//多线程 - 处理数据
|
|
|
- pool_mgo := make(chan bool, mgo_pool)
|
|
|
- wg_mgo := &sync.WaitGroup{}
|
|
|
+ pool_es := make(chan bool, es_pool)
|
|
|
+ wg_es := &sync.WaitGroup{}
|
|
|
pages,numDocs := 0,0
|
|
|
-
|
|
|
for {
|
|
|
searchResult, err := cursor.Next()
|
|
|
if err != nil {
|
|
|
if err.Error() == "EOS" {
|
|
|
break
|
|
|
}else {
|
|
|
- log.Fatal("cursor searchResult",err)
|
|
|
+ log.Println("cursor searchResult",err)
|
|
|
}
|
|
|
}
|
|
|
pages++
|
|
@@ -68,83 +64,105 @@ func startFusionData() {
|
|
|
|
|
|
numDocs++
|
|
|
fusion_ids := qu.ObjToString(tmp["allids"])
|
|
|
- fusionArr := strings.Split(fusion_ids, ",")
|
|
|
sourceid := qu.ObjToString(tmp["_id"])
|
|
|
- pool_mgo <- true
|
|
|
- wg_mgo.Add(1)
|
|
|
- go func(sourceid string, fusionArr []string) {
|
|
|
+ pool_es <- true
|
|
|
+ wg_es.Add(1)
|
|
|
+ go func(sourceid string, fusionArr string) {
|
|
|
defer func() {
|
|
|
- <-pool_mgo
|
|
|
- wg_mgo.Done()
|
|
|
+ <-pool_es
|
|
|
+ wg_es.Done()
|
|
|
}()
|
|
|
- weight := NewWeightData(fusionArr)
|
|
|
- weight.analyzeBuildStandardData()
|
|
|
-
|
|
|
- if len(fusionArr) <= 1 { //单组数据
|
|
|
- saveFusionData, saveRecordData := weight.dealWithAddFusionStruct()
|
|
|
- saveid := mgo.Save(fusion_coll_name, saveFusionData)
|
|
|
- //新增-Record 批量新增-经测试-批量新增与多线程新增 速度306s-236s 相差20%的耗时
|
|
|
- saveRecordData["_id"] = saveid
|
|
|
- UpdateRecord.add_pool <- saveRecordData //批量新增
|
|
|
-
|
|
|
- //批量更新Es -
|
|
|
- //fusion_id, template_id := BsonTOStringId(saveid), qu.ObjToString(saveFusionData["fusion_templateid"])
|
|
|
- //updateStr1 := `ctx._source.template_id=` + `"` + template_id + `";`
|
|
|
- //updateStr2 := `ctx._source.fusion_id=` + `"` + fusion_id + `"`
|
|
|
- //elastic.Update(esIndex,esType,sourceid, updateStr1+updateStr2)
|
|
|
- //UpdateElastic.update_pool <- map[string]string{
|
|
|
- // "id":sourceid,
|
|
|
- // "updateStr":updateStr1+updateStr2,
|
|
|
- //}
|
|
|
-
|
|
|
+ AddGroupPool.pool <- map[string]interface{}{
|
|
|
+ "_id":StringTOBsonId(sourceid),
|
|
|
+ "allids":fusion_ids,
|
|
|
+ }
|
|
|
+ }(sourceid, fusion_ids)
|
|
|
+ }
|
|
|
|
|
|
- UpdateRecord.add_pool <- saveRecordData
|
|
|
- }else {
|
|
|
- saveFusionData, saveRecordData := weight.dealWithMultipleAddFusionStruct()
|
|
|
- saveid := mgo.Save(fusion_coll_name, saveFusionData)
|
|
|
- //新增-Record
|
|
|
- saveRecordData["_id"] = saveid
|
|
|
- UpdateRecord.add_pool <- saveRecordData //批量新增
|
|
|
+ }
|
|
|
+ log.Println("遍历Es结束......")
|
|
|
+ wg_es.Wait()
|
|
|
|
|
|
- //批量更新Es -
|
|
|
- //fusion_id, template_id := BsonTOStringId(saveid), qu.ObjToString(saveFusionData["fusion_templateid"])
|
|
|
- //updateStr1 := `ctx._source.template_id=` + `"` + template_id + `";`
|
|
|
- //updateStr2 := `ctx._source.fusion_id=` + `"` + fusion_id + `"`
|
|
|
- //elastic.Update(esIndex,esType,sourceid, updateStr1+updateStr2)
|
|
|
- //UpdateElastic.update_pool <- map[string]string{
|
|
|
- // "id":sourceid,
|
|
|
- // "updateStr":updateStr1+updateStr2,
|
|
|
- //}
|
|
|
+ log.Println("fusion group over :",numDocs,"用时:",int(time.Now().Unix())-start,"秒")
|
|
|
+}
|
|
|
|
|
|
- }
|
|
|
- }(sourceid, fusionArr)
|
|
|
+func startFusionData() {
|
|
|
+ log.Println("开始全量融合流程...")
|
|
|
+ defer qu.Catch()
|
|
|
+ //可以开多程序-不同id段执行融合
|
|
|
+ q := map[string]interface{}{}
|
|
|
+ sess := mgo.GetMgoConn()
|
|
|
+ defer mgo.DestoryMongoConn(sess)
|
|
|
+ it := sess.DB(mgo.DbName).C(group_coll_name).Find(&q).Iter()
|
|
|
+ index,start :=0, int(time.Now().Unix())
|
|
|
+ //多线程保存数据
|
|
|
+ pool_mgo := make(chan bool, mgo_pool)
|
|
|
+ wg_mgo := &sync.WaitGroup{}
|
|
|
+ for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
|
|
|
+ if index%10000==0 {
|
|
|
+ log.Println("current index",index,tmp["_id"])
|
|
|
}
|
|
|
+ fusion_ids := qu.ObjToString(tmp["allids"])
|
|
|
+ fusionArr := strings.Split(fusion_ids, ",")
|
|
|
+ sourceid := BsonTOStringId(tmp["_id"])
|
|
|
+ pool_mgo <- true
|
|
|
+ wg_mgo.Add(1)
|
|
|
+ go func(sourceid string, fusionArr []string) {
|
|
|
+ defer func() {
|
|
|
+ <-pool_mgo
|
|
|
+ wg_mgo.Done()
|
|
|
+ }()
|
|
|
+ weight := NewWeightData(fusionArr)
|
|
|
+ weight.analyzeBuildStandardData()
|
|
|
+
|
|
|
+ saveFusionData, saveRecordData:= map[string]interface{}{},map[string]interface{}{}
|
|
|
+ if len(fusionArr) <= 1 {
|
|
|
+ saveFusionData, saveRecordData = weight.dealWithAddFusionStruct()
|
|
|
+ }else {
|
|
|
+ saveFusionData, saveRecordData = weight.dealWithMultipleAddFusionStruct()
|
|
|
+ }
|
|
|
+ //新增融合表
|
|
|
+ saveid := mgo.Save(fusion_coll_name, saveFusionData)
|
|
|
+ saveRecordData["_id"] = saveid
|
|
|
+ //批量新增日志表
|
|
|
+ AddRecordPool.pool <- saveRecordData
|
|
|
+ //批量更新分组表
|
|
|
+ UpdateGroupPool.pool <- []map[string]interface{}{
|
|
|
+ map[string]interface{}{
|
|
|
+ "_id": StringTOBsonId(sourceid),
|
|
|
+ },
|
|
|
+ map[string]interface{}{
|
|
|
+ "$set": map[string]interface{}{
|
|
|
+ "fusion_id": BsonTOStringId(saveid),
|
|
|
+ "template_id":qu.ObjToString(saveFusionData["fusion_templateid"]),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }(sourceid, fusionArr)
|
|
|
|
|
|
+ tmp = make(map[string]interface{})
|
|
|
}
|
|
|
- log.Println("遍历Es结束......")
|
|
|
wg_mgo.Wait()
|
|
|
|
|
|
- log.Println("fusion is over :",numDocs,"用时:",int(time.Now().Unix())-start,"秒")
|
|
|
+ log.Println("fusion is over:",index,"总用时:",int(time.Now().Unix())-start,"秒")
|
|
|
|
|
|
}
|
|
|
|
|
|
-func goUpdateEs(sourceid string,updateStr string) {
|
|
|
- UpdateElastic.update_pool <- map[string]string{
|
|
|
- "id":sourceid,
|
|
|
- "updateStr":updateStr,
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
func startTaskFullData(data []byte, mapInfo map[string]interface{}) {
|
|
|
|
|
|
+ //先到处具体需要融合组数据-存mongo
|
|
|
+ exportFusionMongoData()
|
|
|
+ time.Sleep(30 * time.Second)
|
|
|
|
|
|
+ //具体融合数据的方法
|
|
|
startFusionData()
|
|
|
+ time.Sleep(30 * time.Second)
|
|
|
return
|
|
|
|
|
|
|
|
|
log.Println("开始全量融合流程")
|
|
|
defer qu.Catch()
|
|
|
-
|
|
|
//区间id
|
|
|
q := map[string]interface{}{
|
|
|
"_id": map[string]interface{}{
|
|
@@ -258,16 +276,18 @@ func startTaskFullData(data []byte, mapInfo map[string]interface{}) {
|
|
|
|
|
|
}
|
|
|
log.Println("索引准备完毕睡眠30s......耗时:",int(time.Now().Unix())-start,"秒")
|
|
|
-
|
|
|
-
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
|
|
-
|
|
|
+ //先到处具体需要融合组数据-存mongo
|
|
|
+ exportFusionMongoData()
|
|
|
+ time.Sleep(30 * time.Second)
|
|
|
//具体融合数据的方法
|
|
|
startFusionData()
|
|
|
-
|
|
|
- log.Println("睡眠30秒,然后在发广播")
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
|
|
taskSendFusionUdp(mapInfo)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|