data_prep/importfunc/import_user_id_map.go

57 lines
1.1 KiB
Go
Raw Normal View History

2024-09-13 17:06:00 +08:00
package importfunc
import (
"context"
"fmt"
2024-09-13 17:11:04 +08:00
"math/rand"
2024-09-13 17:06:00 +08:00
2024-09-13 17:06:40 +08:00
"data_prep/dbstruct"
"data_prep/mongo"
2024-09-13 17:06:00 +08:00
)
func ImportUserIdMap() {
mcli, err := mongo.NewMongo()
if err != nil {
fmt.Printf("mongo client init fail : %v", err)
return
}
ctx := context.Background()
userIdMaps := make([]*dbstruct.UserIdMap, 0)
2024-09-13 17:11:04 +08:00
// 打乱
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]
})
2024-09-13 17:06:00 +08:00
2024-09-13 17:11:04 +08:00
for i := range list {
seq := int64(1000000) + int64(i)
userId := list[i]
2024-09-13 17:06:00 +08:00
userIdMaps = append(userIdMaps, &dbstruct.UserIdMap{
Seq: int64(seq),
UserId: int64(userId),
})
if len(userIdMaps) == 10000 {
2024-09-13 17:11:04 +08:00
err := mcli.CreateMappedUserIds(ctx, userIdMaps)
if err != nil {
fmt.Printf("CreateMappedUserIds err :%v", err)
}
2024-09-13 17:06:00 +08:00
userIdMaps = make([]*dbstruct.UserIdMap, 0)
}
}
if len(userIdMaps) > 0 {
2024-09-13 17:11:04 +08:00
err := mcli.CreateMappedUserIds(ctx, userIdMaps)
if err != nil {
fmt.Printf("CreateMappedUserIds err :%v", err)
}
2024-09-13 17:06:00 +08:00
}
2024-09-13 17:11:04 +08:00
fmt.Printf("Import into test success")
2024-09-13 17:06:00 +08:00
}