Skip to content

Commit

Permalink
Merge pull request #45 from zilliztech/support-alias-operation
Browse files Browse the repository at this point in the history
support alias operations
  • Loading branch information
nameczz authored Aug 22, 2023
2 parents 0bddd84 + 6c4ec18 commit a5faa79
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 1 deletion.
41 changes: 41 additions & 0 deletions milvus_cli/Alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pymilvus import utility


class MilvusAlias(object):
alias = "default"

def create_alias(self, collectionName, aliasName, alias=None):
tempAlias = alias if alias else self.alias
try:
utility.create_alias(
collection_name=collectionName, alias=aliasName, using=tempAlias
)
return f"Create alias {aliasName} successfully!"
except Exception as e:
raise Exception(f"Create alias error!{str(e)}")

def list_aliases(self, collectionName, alias=None):
tempAlias = alias if alias else self.alias
try:
res = utility.list_aliases(collection_name=collectionName, using=tempAlias)
return res
except Exception as e:
raise Exception(f"List alias error!{str(e)}")

def drop_alias(self, aliasName, alias=None):
tempAlias = alias if alias else self.alias
try:
utility.drop_alias(alias=aliasName, using=tempAlias)
return f"Drop alias {aliasName} successfully!"
except Exception as e:
raise Exception(f"Drop alias error!{str(e)}")

def alter_alias(self, aliasName, collectionName, alias=None):
tempAlias = alias if alias else self.alias
try:
utility.alter_alias(
alias=aliasName, collection_name=collectionName, using=tempAlias
)
return f"Alter alias {aliasName} successfully!"
except Exception as e:
raise Exception(f"Alter alias error!{str(e)}")
2 changes: 2 additions & 0 deletions milvus_cli/Cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from Collection import MilvusCollection
from Index import MilvusIndex
from Data import MilvusData
from Alias import MilvusAlias
from pymilvus import __version__
from Types import ParameterException

Expand All @@ -25,3 +26,4 @@ class MilvusCli(object):
collection = MilvusCollection()
index = MilvusIndex()
data = MilvusData()
alias = MilvusAlias()
1 change: 1 addition & 0 deletions milvus_cli/Collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def create_collection(

def list_collections(self, alias=None):
tempAlias = alias if alias else self.alias

try:
res = list_collections(using=tempAlias)
except Exception as e:
Expand Down
100 changes: 100 additions & 0 deletions milvus_cli/scripts/alias_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from helper_cli import create, getList, delete
import click


@create.command("alias")
@click.option(
"-c",
"--collection-name",
"collectionName",
help="Collection name to be specified alias.",
type=str,
)
@click.option(
"-a",
"--alias-name",
"aliasName",
help="The alias of the collection.",
type=str,
)
@click.option(
"-A",
"--alter",
"alter",
help="[Optional, Flag] - Change an existing alias to current collection.",
default=False,
is_flag=True,
)
@click.pass_obj
def create_alias(
obj,
collectionName,
aliasName,
alter,
):
"""
Specify alias for a collection.
Alias cannot be duplicated, you can't assign same alias to different collections.
But you can specify multiple aliases for a collection, for example:
create alias -c car -a carAlias1
You can also change alias of a collection to another collection.
If the alias doesn't exist, it will return error.
Use "-A" option to change alias owner collection, for example:
create alias -c car2 -A -a carAlias1
"""
try:
if alter:
result = obj.alias.alter_alias(collectionName, aliasName)
else:
result = obj.alias.create_alias(collectionName, aliasName)
except Exception as ce:
click.echo("Error!\n{}".format(str(ce)))
else:
click.echo(result)


@getList.command("aliases")
@click.option(
"-c",
"--collection-name",
"collectionName",
help="Collection name to be specified alias.",
type=str,
)
@click.pass_obj
def list_aliases(obj, collectionName):
"""
List all aliases of a collection.
Example:
list aliases -c car
"""
try:
click.echo(obj.alias.list_aliases(collectionName))
except Exception as e:
click.echo(message=e, err=True)


@delete.command("alias")
@click.option(
"-a",
"--alias-name",
"aliasName",
help="The alias of the collection.",
type=str,
)
@click.pass_obj
def delete_alias(obj, aliasName):
"""
Delete alias of a collection.
Example:
delete alias -a carAlias1
"""
try:
click.echo(obj.alias.drop_alias(aliasName))
except Exception as e:
click.echo(message=e, err=True)
1 change: 1 addition & 0 deletions milvus_cli/scripts/milvus_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collection_cli import *
from index_cli import *
from data_cli import *
from alias_cli import *


if __name__ == "__main__":
Expand Down
72 changes: 72 additions & 0 deletions milvus_cli/test/test_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import unittest
import sys
import os

current_dir = os.path.dirname(os.path.realpath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)
from Connection import MilvusConnection
from Collection import MilvusCollection
from Alias import MilvusAlias

uri = "http://localhost:19530"
tempAlias = "zilliz2"
collectionName = "test_collection"
collectionName2 = "test_collection2"
colAlias = "collection_alias"

milvusConnection = MilvusConnection()
collection = MilvusCollection()
alias = MilvusAlias()


class TestAlias(unittest.TestCase):
@classmethod
def setUpClass(cls):
milvusConnection.connect(uri=uri, alias=tempAlias)
fields = [
"id:VARCHAR:128",
"title:VARCHAR:512",
"title_vector:FLOAT_VECTOR:768",
]
collection.create_collection(
collectionName=collectionName,
fields=fields,
alias=tempAlias,
autoId=False,
description="this is a test collection",
primaryField="id",
isDynamic=True,
consistencyLevel="Strong",
)

collection.create_collection(
collectionName=collectionName2,
fields=fields,
alias=tempAlias,
autoId=False,
description="this is a test collection",
primaryField="id",
isDynamic=True,
consistencyLevel="Strong",
)

@classmethod
def tearDownClass(cls):
collection.drop_collection(collectionName=collectionName, alias=tempAlias)
collection.drop_collection(collectionName=collectionName2, alias=tempAlias)

milvusConnection.disconnect(alias=tempAlias)

def test_create_alias(self):
result = alias.create_alias(
collectionName=collectionName, aliasName=colAlias, alias=tempAlias
)
print(result)
aliasList = alias.list_aliases(collectionName, tempAlias)
self.assertIn(colAlias, aliasList)

def test_drop_alias(self):
alias.drop_alias(aliasName=colAlias, alias=tempAlias)
aliasList = alias.list_aliases(collectionName, tempAlias)
self.assertNotIn(colAlias, aliasList)
9 changes: 8 additions & 1 deletion milvus_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,14 @@ class Completer(object):
"help": [],
"import": [],
"insert": [],
"list": ["collections", "databases", "partitions", "indexes", "users"],
"list": [
"collections",
"databases",
"partitions",
"indexes",
"users",
"aliases",
],
"load_balance": [],
"load": ["collection", "partition"],
"query": [],
Expand Down

0 comments on commit a5faa79

Please sign in to comment.