forked from milvus-io/milvus_cli
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from zilliztech/support-alias-operation
support alias operations
- Loading branch information
Showing
7 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters