by Robin at 20240924
This commit is contained in:
parent
9dd85ff4ad
commit
af0b4b78bf
|
@ -1,13 +1,9 @@
|
|||
package importfunc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"math/rand"
|
||||
|
||||
"data_prep/dbstruct"
|
||||
"data_prep/mongo"
|
||||
|
@ -22,59 +18,17 @@ func ImportUserIdMap() {
|
|||
}
|
||||
ctx := context.Background()
|
||||
|
||||
for i := 1; i < 100; i++ {
|
||||
fmt.Printf("Importing %dth", i)
|
||||
pathurl := fmt.Sprintf("/app/file/user_id_map%v.txt", i)
|
||||
infile, err := os.Open(pathurl)
|
||||
if err != nil {
|
||||
fmt.Printf("Open File Err : %v", err)
|
||||
return
|
||||
}
|
||||
reader := bufio.NewReader(infile)
|
||||
userIdMaps := make([]*dbstruct.UserIdMap, 0)
|
||||
for {
|
||||
str, err := reader.ReadString('\n')
|
||||
if len(str) == 0 {
|
||||
continue
|
||||
}
|
||||
if err == io.EOF {
|
||||
err := mcli.CreateMappedUserIds(ctx, userIdMaps)
|
||||
if err != nil {
|
||||
fmt.Printf("CreateMappedUserIds err :%v", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
strs := strings.Split(str, ":")
|
||||
seq, err := strconv.Atoi(strs[0])
|
||||
if err != nil {
|
||||
fmt.Printf("Atoi err :%v", err)
|
||||
}
|
||||
userId, err := strconv.Atoi(strs[1])
|
||||
if err != nil {
|
||||
fmt.Printf("Atoi err :%v", err)
|
||||
}
|
||||
userIdMaps = append(userIdMaps, &dbstruct.UserIdMap{
|
||||
Seq: int64(seq),
|
||||
UserId: int64(userId),
|
||||
})
|
||||
if len(userIdMaps) == 1000 {
|
||||
err := mcli.CreateMappedUserIds(ctx, userIdMaps)
|
||||
if err != nil {
|
||||
fmt.Printf("CreateMappedUserIds err :%v", err)
|
||||
}
|
||||
userIdMaps = make([]*dbstruct.UserIdMap, 0)
|
||||
}
|
||||
}
|
||||
infile.Close()
|
||||
fmt.Printf("%dth imported", 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)
|
||||
|
||||
// 打乱
|
||||
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 := range list {
|
||||
seq := int64(1000000) + int64(i)
|
||||
userId := list[i]
|
||||
|
@ -90,11 +44,13 @@ func ImportAsList(mcli *mongo.Mongo, ctx context.Context, list []int64, s int) {
|
|||
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)
|
||||
|
||||
fmt.Printf("Import into test success")
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ func NewMongo() (mongo *Mongo, err error) {
|
|||
const (
|
||||
DBUserIdSeq = "user_id_seq"
|
||||
COLUserIdSeq = "user_id_seq"
|
||||
COLUserIdMap = "user_id_map_2"
|
||||
COLUserIdMap = "user_id_map2"
|
||||
COLUserIdImportSeq = "user_id_import_seq"
|
||||
|
||||
DBAccountIdSeq = "account_id_seq"
|
||||
|
|
Loading…
Reference in New Issue