data_prep/importfunc/import_user_id_map.go

74 lines
1.4 KiB
Go

package importfunc
import (
"bufio"
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"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()
userIdMaps := make([]*dbstruct.UserIdMap, 0)
pathurl := "/app/file/user_id_map.txt"
infile, err := os.Open(pathurl)
if err != nil {
fmt.Printf("Open File Err : %v", err)
return
}
reader := bufio.NewReader(infile)
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)
}
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("Import into test success")
}