-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_user_collection.py
executable file
·154 lines (121 loc) · 4.82 KB
/
create_user_collection.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
from typing import Any, Dict, List
import contextlib
import io
import random
import string
from pymilvus import DataType, MilvusClient
import numpy as np
import pymilvus
EMBEDDING_DIMENSION = 8
NUM_ENTITIES = 300
URI = 'http://localhost:19530'
ROOT_USER = 'root'
ROOT_PASS = 'Milvus'
USERNAME = 'user1'
USERPASS = 'user1_password'
USERROLE = 'user_role'
USER_COLLECTION = 'user_collection'
class FieldName:
PK = 'pk'
RANDOM = 'random'
EMBEDDINGS = 'embeddings'
def remove_user(root_client):
try:
with contextlib.redirect_stderr(io.StringIO()):
# .. note:: The typehint is List[Dict], but functionally it is Dict.
# noinspection PyTypeChecker
privileges = root_client.describe_role(USERROLE)['privileges']
except pymilvus.exceptions.MilvusException:
privileges = []
# .. note:: describe role is type hinted as List[Dict], but the actual return is Dict.
for privilege in privileges:
root_client.revoke_privilege(USERROLE,
privilege['object_type'],
privilege['privilege'],
privilege['object_name'])
try:
with contextlib.redirect_stderr(io.StringIO()):
root_client.revoke_role(USERNAME, USERROLE)
except pymilvus.exceptions.MilvusException:
pass
try:
with contextlib.redirect_stderr(io.StringIO()):
root_client.drop_role(USERROLE)
except pymilvus.exceptions.MilvusException:
pass
root_client.drop_user(USERNAME)
def add_user(root_client) -> MilvusClient:
root_client.create_user(USERNAME, password=USERPASS)
try:
with contextlib.redirect_stderr(io.StringIO()):
root_client.create_role(USERROLE)
except pymilvus.exceptions.MilvusException as err:
if 'already exists' not in str(err):
raise err
root_client.grant_privilege(role_name=USERROLE,
object_type='Global',
privilege='All',
object_name='*')
root_client.grant_privilege(role_name=USERROLE,
object_type='Global',
privilege='CreateCollection',
object_name='CreateCollection')
root_client.grant_privilege(role_name=USERROLE,
object_type='Global',
privilege='ShowCollections',
object_name='ShowCollections')
root_client.grant_role(USERNAME, USERROLE)
return MilvusClient(URI, USERNAME, password=USERPASS)
def create_collection(client: MilvusClient):
schema = client.create_schema(
auto_id=False,
enable_dynamic_fields=True,
description="A test collection.",
)
schema.add_field(field_name=FieldName.PK,
datatype=DataType.VARCHAR, is_primary=True, max_length=100)
schema.add_field(field_name=FieldName.RANDOM, datatype=DataType.DOUBLE)
schema.add_field(field_name=FieldName.EMBEDDINGS,
datatype=DataType.FLOAT_VECTOR, dim=EMBEDDING_DIMENSION)
client.create_collection(
collection_name=USER_COLLECTION,
schema=schema,
consistency_level="Strong"
)
def generate_random_string(length):
return ''.join(random.choice(string.ascii_letters + string.digits)
for _ in range(length))
def generate_random_entities(num_entities, dim) -> List[Dict[str, Any]]:
entities_ = []
for _ in range(num_entities):
pk = generate_random_string(10)
random_value = random.random()
embeddings = np.random.rand(dim).tolist()
entities_.append({FieldName.PK: pk,
FieldName.RANDOM: random_value,
FieldName.EMBEDDINGS: embeddings})
return entities_
entities = generate_random_entities(NUM_ENTITIES, EMBEDDING_DIMENSION)
client.insert(collection_name=USER_COLLECTION, data=entities)
index_params = client.prepare_index_params()
index_params.add_index(field_name=FieldName.PK)
index_params.add_index(
field_name=FieldName.EMBEDDINGS,
index_type="IVF_FLAT",
metric_type="L2",
params={"nlist": 128}
)
client.create_index(
collection_name=USER_COLLECTION,
index_params=index_params
)
def main():
root_client = MilvusClient(URI, user=ROOT_USER, password=ROOT_PASS)
remove_user(root_client)
user_client = add_user(root_client)
create_collection(user_client)
print(f'root client list collections: {root_client.list_collections()}')
print(f'{USERNAME} client list collections: {user_client.list_collections()}')
if __name__ == '__main__':
main()