78 lines
1.4 KiB
Go
78 lines
1.4 KiB
Go
|
package importfunc
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/Leufolium/test/dbstruct"
|
||
|
"github.com/Leufolium/test/mongo"
|
||
|
)
|
||
|
|
||
|
func ImportUserIdMap() {
|
||
|
|
||
|
mcli, err := mongo.NewMongo()
|
||
|
if err != nil {
|
||
|
fmt.Printf("mongo client init fail : %v", err)
|
||
|
return
|
||
|
}
|
||
|
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)
|
||
|
|
||
|
for {
|
||
|
str, err := reader.ReadString('\n')
|
||
|
if err == io.EOF {
|
||
|
break
|
||
|
}
|
||
|
if err != nil {
|
||
|
fmt.Printf("ReadString fail : %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
strs := strings.Split(str, ":")
|
||
|
seqStr := strs[0]
|
||
|
userIdStr := strs[1]
|
||
|
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{
|
||
|
Seq: int64(seq),
|
||
|
UserId: int64(userId),
|
||
|
})
|
||
|
if len(userIdMaps) == 10000 {
|
||
|
mcli.CreateMappedUserIds(ctx, userIdMaps)
|
||
|
userIdMaps = make([]*dbstruct.UserIdMap, 0)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if len(userIdMaps) > 0 {
|
||
|
mcli.CreateMappedUserIds(ctx, userIdMaps)
|
||
|
}
|
||
|
}
|