-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.py
67 lines (54 loc) · 1.52 KB
/
insert.py
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
from pymilvus import (
connections,
FieldSchema,
DataType,
CollectionSchema,
utility,
Collection,
)
import pickle
import time
import sys
# 连接 Milvus
conn = connections.connect(host="10.100.2.241", port=19530)
# 指定每个字段的 Schema
entry_id = FieldSchema("entry_id", DataType.INT64, is_primary=True)
entry_text = FieldSchema("entry_text", DataType.VARCHAR, max_length=1024)
entry_vector = FieldSchema("entry_vector", DataType.FLOAT_VECTOR, dim=768)
# 指定集合的 Schema
schema = CollectionSchema([entry_id, entry_text, entry_vector])
# 创建空集合
utility.drop_collection("test")
collection = Collection("test", schema)
pickle_file = "./100w_0.pickle"
with open(pickle_file, "rb") as f:
# 读取 pickle 文件
data_dict: dict = pickle.load(f)
num = len(data_dict["text_list"])
data_id = [i for i in range(num)]
tic = time.perf_counter()
i, step = 0, 2100
print(type(data_dict))
while i < num:
# 将数据插入集合
collection.insert(
[
data_id[i : i + step],
data_dict["text_list"][i : i + step],
data_dict["text_vector"][i : i + step],
]
)
i += step
# print(i)
# 确保所有数据被密封和索引
collection.flush()
toc = time.perf_counter()
print(f"inserted {num} entities in {toc - tic:0.4f} seconds")
# 创建向量索引
index_params = {
"metric_type": "L2",
"index_type": "IVF_FLAT",
"params": {"nlist": 1024},
}
collection.create_index("entry_vector", index_params)
# collection.drop_index()