-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (120 loc) · 3.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io"
"log"
"os"
"strconv"
"time"
)
var CollectionTest *mongo.Collection
var myTestModel TestModel
const MaxExecTime = 300
const TB = "test"
type TestModel struct {
Action int32
Value1 string
Value2 int32
Tag []string
OldId int64 `bson:"old_id,omitempty" json:"old_id"`
}
func GetMongoClient(addr string) (*mongo.Client, error) {
//获取mongo客户端
clientOptions := options.Client().ApplyURI(addr)
clientOptions.SetMaxPoolSize(1000)
//初始化连接设置超时时间
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
// 检查db是否连接成功
ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
return client, nil
}
func InitLocalConnection() {
// 连接本地db
mongoAddr := "mongodb://root:xxx@xxxx:27017"
client, _ := GetMongoClient(mongoAddr)
// 利用client连接到具体的数据库,创建handle
CollectionTest = client.Database("miaoyc").Collection(TB)
}
// ReadFileContent2db
/*
*逐行解析文件里面的内容
*将文件里面的内容批量添加到db
*/
func ReadFileContent2db(fileName, tableName string) error {
fi, err := os.Open(fileName)
defer fi.Close()
buf := bufio.NewReader(fi)
count := 0
var docs []interface{}
for {
a, c := buf.ReadString('\n')
if c == io.EOF {
break
}
count++
json.Unmarshal([]byte(a), &myTestModel)
s, _ := bson.Marshal(myTestModel)
docs = append(docs, s)
if count == 10 {
err = BatchCreateDocument(docs, CollectionTest)
fmt.Println("BatchCreate is", err)
count = 0
docs = docs[:0]
}
}
err = BatchCreateDocument(docs, CollectionTest)
CreateIndex(CollectionTest, "old_id", TB)
return err
}
// BatchCreateDocument 在db中批量创建文档
func BatchCreateDocument(docs []interface{}, tableCollection *mongo.Collection) error {
opts := options.InsertMany().SetOrdered(false)
_, err := tableCollection.InsertMany(context.TODO(), docs, opts)
if err != nil {
return err
}
return nil
}
// CreateIndex 创建索引
func CreateIndex(tableCollection *mongo.Collection, filedName, tableName string) error {
indexName := tableName + "_Index"
model := mongo.IndexModel{
Keys: bson.D{{filedName, 1}},
Options: options.Index().SetName(indexName),
}
opts := options.CreateIndexes().SetMaxTime(MaxExecTime * time.Second)
_, err := tableCollection.Indexes().CreateOne(context.TODO(), model, opts)
if err != nil {
return err
}
return nil
}
func (m *TestModel) GetByOid(oldId string) TestModel {
var info TestModel
n, _ := strconv.ParseInt(oldId, 10, 64)
manualCondition := bson.M{"old_id": n}
CollectionTest.FindOne(context.TODO(), manualCondition).Decode(&info)
return info
}
func main() {
InitLocalConnection()
fileName := "case.json"
ReadFileContent2db(fileName, TB)
var info TestModel
r := info.GetByOid("8088464930757410816")
fmt.Println(r)
}