by Robin at 20240913

This commit is contained in:
Leufolium 2024-09-13 17:11:04 +08:00
parent e563f5ee4e
commit 1cf1a7ca75
2 changed files with 27 additions and 43 deletions

5
cmd/main.go Normal file
View File

@ -0,0 +1,5 @@
package cmd
func main() {
}

View File

@ -1,13 +1,9 @@
package importfunc package importfunc
import ( import (
"bufio"
"context" "context"
"fmt" "fmt"
"io" "math/rand"
"os"
"strconv"
"strings"
"data_prep/dbstruct" "data_prep/dbstruct"
"data_prep/mongo" "data_prep/mongo"
@ -22,56 +18,39 @@ func ImportUserIdMap() {
} }
ctx := context.Background() ctx := context.Background()
infilePath := "/app/dataprep/file/user_id_map.txt"
infile, err := os.Open(infilePath)
if err != nil {
fmt.Printf("Open File Err : %v", err)
}
defer infile.Close()
reader := bufio.NewReader(infile)
userIdMaps := make([]*dbstruct.UserIdMap, 0) userIdMaps := make([]*dbstruct.UserIdMap, 0)
for { // 打乱
str, err := reader.ReadString('\n') list := make([]int64, 0)
if err == io.EOF { for i := int64(1000000); i < 100000000; i++ {
break list = append(list, i)
} }
if err != nil { rand.Shuffle(99000000, func(i, j int) {
fmt.Printf("ReadString fail : %v", err) list[i], list[j] = list[j], list[i]
return })
}
strs := strings.Split(str, ":") for i := range list {
seqStr := strs[0] seq := int64(1000000) + int64(i)
userIdStr := strs[1] userId := list[i]
userIdStr = strings.ReplaceAll(userIdStr, "\n", "")
userIdStr = strings.ReplaceAll(userIdStr, "\r", "")
seq, err := strconv.Atoi(seqStr)
if err != nil {
fmt.Printf("atoi fail : %v", err)
return
}
userId, err := strconv.Atoi(userIdStr)
if err != nil {
fmt.Printf("atoi fail : %v", err)
return
}
userIdMaps = append(userIdMaps, &dbstruct.UserIdMap{ userIdMaps = append(userIdMaps, &dbstruct.UserIdMap{
Seq: int64(seq), Seq: int64(seq),
UserId: int64(userId), UserId: int64(userId),
}) })
if len(userIdMaps) == 10000 { if len(userIdMaps) == 10000 {
mcli.CreateMappedUserIds(ctx, userIdMaps) err := mcli.CreateMappedUserIds(ctx, userIdMaps)
if err != nil {
fmt.Printf("CreateMappedUserIds err :%v", err)
}
userIdMaps = make([]*dbstruct.UserIdMap, 0) userIdMaps = make([]*dbstruct.UserIdMap, 0)
} }
} }
if len(userIdMaps) > 0 { if len(userIdMaps) > 0 {
mcli.CreateMappedUserIds(ctx, userIdMaps) err := mcli.CreateMappedUserIds(ctx, userIdMaps)
if err != nil {
fmt.Printf("CreateMappedUserIds err :%v", err)
}
} }
fmt.Printf("Import into test success")
} }