管理别名
Milvus 提供别名管理功能。本页演示了创建、列出、更改和删除别名的程序。
别名概述
您可以为您的 Collections 创建别名。一个 Collection 可以有多个别名,但不能共享一个别名。
在收到针对某个 Collection 的请求时,Milvus 会根据提供的名称定位该 Collection。如果所提供名称的 Collection 不存在,Milvus 会继续定位所提供名称的别名。你可以使用 Collections 别名来调整代码,以适应不同的情况。
创建别名
以下代码片段演示了如何为 Collection 创建别名。
from pymilvus import MilvusClient
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)
# 9. Manage aliases
# 9.1. Create aliases
client.create_alias(
collection_name="customized_setup_2",
alias="bob"
)
client.create_alias(
collection_name="customized_setup_2",
alias="alice"
)
import io.milvus.v2.service.utility.request.CreateAliasReq;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
String CLUSTER_ENDPOINT = "http://localhost:19530";
String TOKEN = "root:Milvus";
// 1. Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// 9. Manage aliases
// 9.1 Create alias
CreateAliasReq createAliasReq = CreateAliasReq.builder()
.collectionName("customized_setup_2")
.alias("bob")
.build();
client.createAlias(createAliasReq);
createAliasReq = CreateAliasReq.builder()
.collectionName("customized_setup_2")
.alias("alice")
.build();
client.createAlias(createAliasReq);
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "http://localhost:19530";
const token = "root:Milvus";
const client = new MilvusClient({address, token});
// 9. Manage aliases
// 9.1 Create aliases
res = await client.createAlias({
collection_name: "customized_setup_2",
alias: "bob"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.createAlias({
collection_name: "customized_setup_2",
alias: "alice"
})
console.log(res.error_code)
// Output
//
// Success
//
// Go 缺失
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "bob",
"collectionName": "customized_setup_2"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "alice",
"collectionName": "customized_setup_2"
}'
# {
# "code": 0,
# "data": {}
# }
列出别名
以下代码片段演示了列出分配给特定 Collection 的别名的过程。
# 9.2. List aliases
res = client.list_aliases(
collection_name="customized_setup_2"
)
print(res)
# Output
#
# {
# "aliases": [
# "bob",
# "alice"
# ],
# "collection_name": "customized_setup_2",
# "db_name": "default"
# }
import io.milvus.v2.service.utility.request.ListAliasesReq;
import io.milvus.v2.service.utility.response.ListAliasResp;
// 9.2 List alises
ListAliasesReq listAliasesReq = ListAliasesReq.builder()
.collectionName("customized_setup_2")
.build();
ListAliasResp listAliasRes = client.listAliases(listAliasesReq);
System.out.println(listAliasRes.getAlias());
// Output:
// [bob, alice]
// 9.2 List aliases
res = await client.listAliases({
collection_name: "customized_setup_2"
})
console.log(res.aliases)
// Output
//
// [ 'bob', 'alice' ]
//
// Go 缺失
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{}'
# {
# "code": 0,
# "data": [
# "bob",
# "alice"
# ]
# }
描述别名
以下代码片段详细描述了特定别名,包括分配给该别名的 Collection 的名称。
# 9.3. Describe aliases
res = client.describe_alias(
alias="bob"
)
print(res)
# Output
#
# {
# "alias": "bob",
# "collection_name": "customized_setup_2",
# "db_name": "default"
# }
import io.milvus.v2.service.utility.request.DescribeAliasReq;
import io.milvus.v2.service.utility.response.DescribeAliasResp;
// 9.3 Describe alias
DescribeAliasReq describeAliasReq = DescribeAliasReq.builder()
.alias("bob")
.build();
DescribeAliasResp describeAliasRes = client.describeAlias(describeAliasReq);
System.out.println(describeAliasRes);
// Output:
// DescribeAliasResp(collectionName=customized_setup_2, alias=bob)
// 9.3 Describe aliases
res = await client.describeAlias({
collection_name: "customized_setup_2",
alias: "bob"
})
console.log(res)
// Output
//
// {
// status: {
// extra_info: {},
// error_code: 'Success',
// reason: '',
// code: 0,
// retriable: false,
// detail: ''
// },
// db_name: 'default',
// alias: 'bob',
// collection: 'customized_setup_2'
// }
//
// Go 缺失
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "bob"
}'
# {
# "code": 0,
# "data": {
# "aliasName": "bob",
# "collectionName": "customized_setup_2",
# "dbName": "default"
# }
# }
更改别名
您可以将已分配给特定集合的别名重新分配给另一个集合。
# 9.4 Reassign aliases to other collections
client.alter_alias(
collection_name="customized_setup_1",
alias="alice"
)
res = client.list_aliases(
collection_name="customized_setup_1"
)
print(res)
# Output
#
# {
# "aliases": [
# "alice"
# ],
# "collection_name": "customized_setup_1",
# "db_name": "default"
# }
res = client.list_aliases(
collection_name="customized_setup_2"
)
print(res)
# Output
#
# {
# "aliases": [
# "bob"
# ],
# "collection_name": "customized_setup_2",
# "db_name": "default"
# }
import io.milvus.v2.service.utility.request.AlterAliasReq;
// 9.4 Reassign alias to other collections
AlterAliasReq alterAliasReq = AlterAliasReq.builder()
.collectionName("customized_setup_1")
.alias("alice")
.build();
client.alterAlias(alterAliasReq);
ListAliasesReq listAliasesReq = ListAliasesReq.builder()
.collectionName("customized_setup_1")
.build();
ListAliasResp listAliasRes = client.listAliases(listAliasesReq);
System.out.println(listAliasRes.getAlias());
listAliasesReq = ListAliasesReq.builder()
.collectionName("customized_setup_2")
.build();
listAliasRes = client.listAliases(listAliasesReq);
System.out.println(listAliasRes.getAlias());
// Output:
// [bob]
// 9.4 Reassign aliases to other collections
res = await client.alterAlias({
collection_name: "customized_setup_1",
alias: "alice"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.listAliases({
collection_name: "customized_setup_1"
})
console.log(res.aliases)
// Output
//
// [ 'alice' ]
//
res = await client.listAliases({
collection_name: "customized_setup_2"
})
console.log(res.aliases)
// Output
//
// [ 'bob' ]
//
// Go 缺失
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/alter" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "alice",
"collectionName": "customized_setup_1"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "bob"
}'
# {
# "code": 0,
# "data": {
# "aliasName": "bob",
# "collectionName": "customized_setup_2",
# "dbName": "default"
# }
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "alice"
}'
# {
# "code": 0,
# "data": {
# "aliasName": "alice",
# "collectionName": "customized_setup_1",
# "dbName": "default"
# }
# }