68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package importfunc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/rand"
|
|
|
|
"data_prep/dbstruct"
|
|
"data_prep/mongo"
|
|
)
|
|
|
|
func ImportUserIdMap() {
|
|
|
|
mcli, err := mongo.NewMongo()
|
|
if err != nil {
|
|
fmt.Printf("mongo client init fail : %v", err)
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
|
|
// 打乱
|
|
list := make([]int64, 0)
|
|
for i := int64(1000000); i < 100000000; i++ {
|
|
list = append(list, i)
|
|
}
|
|
rand.Shuffle(99000000, func(i, j int) {
|
|
list[i], list[j] = list[j], list[i]
|
|
})
|
|
|
|
for i := 0; i < len(list); i += 1000000 {
|
|
lower := i
|
|
upper := i + 999999
|
|
if upper >= len(list) {
|
|
upper = len(list) - 1
|
|
}
|
|
go ImportAsList(mcli, ctx, list[lower:upper], i)
|
|
}
|
|
|
|
fmt.Printf("Import into test success")
|
|
}
|
|
|
|
func ImportAsList(mcli *mongo.Mongo, ctx context.Context, list []int64, s int) {
|
|
fmt.Printf("Importing %dth", s)
|
|
userIdMaps := make([]*dbstruct.UserIdMap, 0)
|
|
for i := range list {
|
|
seq := int64(1000000) + int64(i)
|
|
userId := list[i]
|
|
userIdMaps = append(userIdMaps, &dbstruct.UserIdMap{
|
|
Seq: int64(seq),
|
|
UserId: int64(userId),
|
|
})
|
|
if len(userIdMaps) == 10000 {
|
|
err := mcli.CreateMappedUserIds(ctx, userIdMaps)
|
|
if err != nil {
|
|
fmt.Printf("CreateMappedUserIds err :%v", err)
|
|
}
|
|
userIdMaps = make([]*dbstruct.UserIdMap, 0)
|
|
}
|
|
}
|
|
if len(userIdMaps) > 0 {
|
|
err := mcli.CreateMappedUserIds(ctx, userIdMaps)
|
|
if err != nil {
|
|
fmt.Printf("CreateMappedUserIds err :%v", err)
|
|
}
|
|
}
|
|
fmt.Printf("%dth imported", s)
|
|
}
|