管理 Partition
在 Zilliz Cloud 中,Partition 是 Collection 的子集。每个 Partition 都与其所在的 Collection 具有相同的数据结构,但只包含该 Collection 的一部分数据。本节将介绍如何管理 Partition 及相关注意事项。
概述
在 Zilliz Cloud 中,每个 Collection 都包含一个名为 _default 的 Partition。如果 Collection 中不再新增 Partition,所有插入到该 Collection 的数据都会进入这个 Partition 中。所有针对该 Collection 的 Search 和 Query 操作也都是在这个 Partition 中完成的。
您也可以在 Collection 中创建多个 Partition,然后按照某个标准将您的数据分别存放到这些 Partition 中。这样一来,您就可以将 Search 和 Query 操作的范围限制在一个或多个 Partition 中,提升搜索效率。
在 Zilliz Cloud 中,一个 Collection 最多可以分成 1,024 个 Partition。
在 Zilliz Cloud 中,Partition Key 是一种基于 Partition 的搜索优化方案,用来根据用户指定的标量字段的取值自动将 Entity 存放到不同的 Partition 中。该功能通常用来实现基于 Partition 的多租户能力,提升搜索性能。如果您想要了解 Partition Key 的详细信息,可以参考使用 Partition Key。
查看 Partition 列表
每个 Collection 在完成创建后都会包含一个名为 _default 的 Partition。您可以使用如下代码列出指定 Collection 中定义的 Partition。
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
res = client.list_partitions(
collection_name="quick_setup"
)
print(res)
# Output
#
# ["_default"]
import io.milvus.v2.service.partition.request.ListPartitionsReq;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import java.util.*;
String CLUSTER_ENDPOINT = "YOUR_CLUSTER_ENDPOINT";
String TOKEN = "YOUR_CLUSTER_TOKEN";
// 1. Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
ListPartitionsReq listPartitionsReq = ListPartitionsReq.builder()
.collectionName("quick_setup")
.build();
List<String> partitionNames = client.listPartitions(listPartitionsReq);
System.out.println(partitionNames);
// Output:
// [_default]
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "YOUR_CLUSTER_ENDPOINT";
const token = "YOUR_CLUSTER_TOKEN";
const client = new MilvusClient({address, token});
let res = await client.listPartitions({
collection_name: "quick_setup"
})
console.log(res);
// Output
// ["_default"]
import (
"context"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle error
}
defer cli.Close(ctx)
partitionNames, err := cli.ListPartitions(ctx, milvusclient.NewListPartitionOption("quick_setup"))
if err != nil {
// handle error
}
fmt.Println(partitionNames)
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup"
}'
# {
# "code": 0,
# "data": [
# "_default"
# ]
# }
创建 Partition
您也可以根据需要在指定 Collection 中创建多个Partition,以便于分门别类地存放数据。
- Python
- Java
- NodeJS
- Go
- cURL
client.create_partition(
collection_name="quick_setup",
partition_name="partitionA"
)
res = client.list_partitions(
collection_name="quick_setup"
)
print(res)
# Output
#
# ["_default", "partitionA"]
import io.milvus.v2.service.partition.request.CreatePartitionReq;
CreatePartitionReq createPartitionReq = CreatePartitionReq.builder()
.collectionName("quick_setup")
.partitionName("partitionA")
.build();
client.createPartition(createPartitionReq);
ListPartitionsReq listPartitionsReq = ListPartitionsReq.builder()
.collectionName("quick_setup")
.build();
List<String> partitionNames = client.listPartitions(listPartitionsReq);
System.out.println(partitionNames);
// Output:
// [_default, partitionA]
await client.createPartition({
collection_name: "quick_setup",
partition_name: "partitionA"
})
res = await client.listPartitions({
collection_name: "quick_setup"
})
console.log(res)
// Output
// ["_default", "partitionA"]
import (
"fmt"
client "github.com/milvus-io/milvus/client/v2/milvusclient"
)
err = cli.CreatePartition(ctx, milvusclient.NewCreatePartitionOption("quick_setup", "partitionA"))
if err != nil {
// handle error
}
partitionNames, err := cli.ListPartitions(ctx, milvusclient.NewListPartitionOption("quick_setup"))
if err != nil {
// handle error
}
fmt.Println(partitionNames)
// Output
// ["_default", "partitionA"]
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionName": "partitionA"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup"
}'
# {
# "code": 0,
# "data": [
# "_default",
# "partitionA"
# ]
# }
检查 Partition 是否存在
如果您知道某个 Partition 的名字,但不确定其是否存在,可以参考如下代码检查该 Partition 是否存在。
- Python
- Java
- NodeJS
- Go
- cURL
res = client.has_partition(
collection_name="quick_setup",
partition_name="partitionA"
)
print(res)
# Output
#
# True
import io.milvus.v2.service.partition.request.HasPartitionReq;
HasPartitionReq hasPartitionReq = HasPartitionReq.builder()
.collectionName("quick_setup")
.partitionName("partitionA")
.build();
Boolean hasPartitionRes = client.hasPartition(hasPartitionReq);
System.out.println(hasPartitionRes);
// Output:
// true
res = await client.hasPartition({
collection_name: "quick_setup",
partition_name: "partitionA"
})
console.log(res.value)
// Output
// true
import (
"fmt"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
result, err := cli.HasPartition(ctx, milvusclient.NewHasPartitionOption("quick_setup", "partitionA"))
if err != nil {
// handle error
}
fmt.Println(result)
// Output:
// true
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/has" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionName": "partitionA"
}'
# {
# "code": 0,
# "data": {
# "has": true
# }
# }
Load 和 Release Partition
您可以对 Collection 中若干 Partition 单独进行 Load 和 Release 操作。
Load 指定 Partition
您可以 Load 指定 Collection 中的一个或多个 Partition。值得注意的是,只要 Collection 中存在任意一个未加载的 Partition,Collection 的 Load 状态始终为 NotLoaded。
- Python
- Java
- NodeJS
- Go
- cURL
client.load_partitions(
collection_name="quick_setup",
partition_names=["partitionA"]
)
res = client.get_load_state(
collection_name="quick_setup",
partition_name="partitionA"
)
print(res)
# Output
#
# {
# "state": "<LoadState: Loaded>"
# }
import io.milvus.v2.service.partition.request.LoadPartitionsReq;
import io.milvus.v2.service.collection.request.GetLoadStateReq;
LoadPartitionsReq loadPartitionsReq = LoadPartitionsReq.builder()
.collectionName("quick_setup")
.partitionNames(Collections.singletonList("partitionA"))
.build();
client.loadPartitions(loadPartitionsReq);
GetLoadStateReq getLoadStateReq = GetLoadStateReq.builder()
.collectionName("quick_setup")
.partitionName("partitionA")
.build();
Boolean getLoadStateRes = client.getLoadState(getLoadStateReq);
System.out.println(getLoadStateRes);
// True
await client.loadPartitions({
collection_name: "quick_setup",
partition_names: ["partitionA"]
})
res = await client.getLoadState({
collection_name: "quick_setup",
partition_name: "partitionA"
})
console.log(res)
// Output
//
// LoadStateLoaded
//
import (
"context"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
task, err := cli.LoadPartitions(ctx, milvusclient.NewLoadPartitionsOption("quick_setup", "partitionA"))
// sync wait collection to be loaded
err = task.Await(ctx)
if err != nil {
// handle error
}
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/load" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionNames": ["partitionA"]
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/get_load_state" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionNames": ["partitionA"]
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 100,
# "loadState": "LoadStateLoaded",
# "message": ""
# }
# }
Release 指定 Partition
您也可以 Release 指定 Collection 中的一个或多个 Partition。
- Python
- Java
- NodeJS
- Go
- cURL
client.release_partitions(
collection_name="quick_setup",
partition_names=["partitionA"]
)
res = client.get_load_state(
collection_name="quick_setup",
partition_name="partitionA"
)
print(res)
# Output
#
# {
# "state": "<LoadState: NotLoaded>"
# }
import io.milvus.v2.service.partition.request.ReleasePartitionsReq;
ReleasePartitionsReq releasePartitionsReq = ReleasePartitionsReq.builder()
.collectionName("quick_setup")
.partitionNames(Collections.singletonList("partitionA"))
.build();
client.releasePartitions(releasePartitionsReq);
GetLoadStateReq getLoadStateReq = GetLoadStateReq.builder()
.collectionName("quick_setup")
.partitionName("partitionA")
.build();
Boolean getLoadStateRes = client.getLoadState(getLoadStateReq);
System.out.println(getLoadStateRes);
// False
await client.releasePartitions({
collection_name: "quick_setup",
partition_names: ["partitionA"]
})
res = await client.getLoadState({
collection_name: "quick_setup",
partition_name: "partitionA"
})
console.log(res)
// Output
//
// LoadStateNotLoaded
//
import "github.com/milvus-io/milvus/client/v2/milvusclient"
err = cli.ReleasePartitions(ctx, milvusclient.NewReleasePartitionsOptions("quick_setup", "partitionA"))
if err != nil {
// handle error
}
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/release" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionNames": ["partitionA"]
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/get_load_state" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionNames": ["partitionA"]
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 0,
# "loadState": "LoadStateNotLoaded",
# "message": ""
# }
# }
Partition 中的数据操作
插入与删除 Entity
您也可以在指定 Partition 中执行 Insert、Upsert 和 Delete 操作。具体可参考:
搜索与查询
您也可以在指定 Partition 中执行 Search 和 Query 操作。具体可参考
删除 Partition
您也可以删除不再需要的 Partition。在删除 Partition 前,请确保该 Partition 处于未加载的状态。
- Python
- Java
- NodeJS
- Go
- cURL
client.release_partitions(
collection_name="quick_setup",
partition_names=["partitionA"]
)
client.drop_partition(
collection_name="quick_setup",
partition_name="partitionA"
)
res = client.list_partitions(
collection_name="quick_setup"
)
print(res)
# ["_default"]
import io.milvus.v2.service.partition.request.DropPartitionReq;
import io.milvus.v2.service.partition.request.ReleasePartitionsReq;
import io.milvus.v2.service.partition.request.ListPartitionsReq;
ReleasePartitionsReq releasePartitionsReq = ReleasePartitionsReq.builder()
.collectionName("quick_setup")
.partitionNames(Collections.singletonList("partitionA"))
.build();
client.releasePartitions(releasePartitionsReq);
DropPartitionReq dropPartitionReq = DropPartitionReq.builder()
.collectionName("quick_setup")
.partitionName("partitionA")
.build();
client.dropPartition(dropPartitionReq);
ListPartitionsReq listPartitionsReq = ListPartitionsReq.builder()
.collectionName("quick_setup")
.build();
List<String> partitionNames = client.listPartitions(listPartitionsReq);
System.out.println(partitionNames);
// Output:
// [_default]
await client.releasePartitions({
collection_name: "quick_setup",
partition_names: ["partitionA"]
})
await client.dropPartition({
collection_name: "quick_setup",
partition_name: "partitionA"
})
res = await client.listPartitions({
collection_name: "quick_setup"
})
console.log(res)
// Output
// ["_default"]
import "github.com/milvus-io/milvus/client/v2/milvusclient"
err := cli.DropPartition(ctx, milvusclient.NewDropPartitionOption("quick_setup", "partitionA"))
if err != nil {
// handle error
}
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/release" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionNames": ["partitionA"]
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/drop" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"partitionName": "partitionA"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup"
}'
# {
# "code": 0,
# "data": [
# "_default"
# ]
# }