Gerir aliases
O Milvus fornece capacidades de gestão de aliases. Esta página demonstra os procedimentos para criar, listar, alterar e eliminar aliases.
Visão geral
É possível criar aliases para as suas colecções. Uma coleção pode ter vários aliases, mas as colecções não podem partilhar um alias.
Ao receber um pedido para uma coleção, o Milvus localiza a coleção com base no nome fornecido. Se a coleção com o nome fornecido não existir, o Milvus continua a localizar o nome fornecido como um alias. Pode utilizar aliases de colecções para adaptar o seu código a diferentes cenários.
Criar alias
O trecho de código a seguir demonstra como criar um alias para uma coleção.
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": {}
# }
Listar aliases
O seguinte trecho de código demonstra o procedimento para listar os aliases alocados a uma coleção específica.
# 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"
# ]
# }
Descrever aliases
O trecho de código a seguir descreve um alias específico em detalhes, incluindo o nome da coleção à qual ele foi alocado.
# 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"
# }
# }
Alterar alias
É possível reatribuir o alias já atribuído a uma coleção específica a outra.
# 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"
# }
# }