data_prep/importfunc/import_user_id_map.go

74 lines
1.4 KiB
Go
Raw Normal View History

2024-09-13 17:06:00 +08:00
package importfunc
import (
2024-09-24 12:15:44 +08:00
"bufio"
2024-09-13 17:06:00 +08:00
"context"
"fmt"
2024-09-24 12:15:44 +08:00
"io"
"os"
"strconv"
"strings"
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()
2024-09-24 11:30:23 +08:00
userIdMaps := make([]*dbstruct.UserIdMap, 0)
2024-09-23 11:24:54 +08:00
2024-09-24 12:15:44 +08:00
pathurl := "/app/file/user_id_map.txt"
infile, err := os.Open(pathurl)
if err != nil {
fmt.Printf("Open File Err : %v", err)
return
2024-09-24 11:30:23 +08:00
}
2024-09-24 12:15:44 +08:00
reader := bufio.NewReader(infile)
2024-09-23 11:24:54 +08:00
2024-09-24 12:15:44 +08:00
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
}
str = strings.ReplaceAll(str, "\n", "")
str = strings.ReplaceAll(str, "\r", "")
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)
}
2024-09-13 17:06:00 +08:00
userIdMaps = append(userIdMaps, &dbstruct.UserIdMap{
Seq: int64(seq),
UserId: int64(userId),
})
2024-09-24 11:57:24 +08:00
if len(userIdMaps) == 1000 {
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)
2024-09-13 17:11:04 +08:00
}
2024-09-13 17:06:00 +08:00
}
2024-09-24 12:15:44 +08:00
infile.Close()
2024-09-24 11:30:23 +08:00
fmt.Printf("Import into test success")
2024-09-13 17:06:00 +08:00
}