设置 Collection 生存时间
数据插入 Collection 后,默认情况下仍保留在该 Collection 中。但是,在某些情况下,您可能希望在一定期限后删除或清理数据。在这种情况下,您可以配置 Collection 的生存时间(TTL)属性,以便 Zilliz Cloud 在TTL到期后自动删除数据。
概述
生存时间(TTL)通常用于如下场景:数据库中的数据在插入或修改后只能保持有效或可访问一段时间。然后,数据需要被自动删除。
例如,如果您希望所有插入某个 Collection 的数据仅需要保留 14 天,您就可以通过为该 Collection 配置生存时间属性来要求 Zilliz Cloud 在数据插入 14 x 24 x 3600 = 1209600 秒后自动删除数据。这就保证了 Collection 中只保存最多 14 天的数据。
在 Zilliz Cloud 中, TTL 的值是一个单位为秒的正整数。一旦设置了该参数,所有生存时间超过该参数值的数据都会被删除。
值得注意的是,数据删除操作是异步的。这就意味着当某些数据超期后并不会马上被删除。在数据被删除和数据不可查之间有一定的延迟。这是由垃圾回收(GC)机制和数据压缩耗时决定的。Zilliz Cloud 会不定期的触发这些操作。
设置 TTL
您可以在如下情况下设置 TTL
-
在创建 Collection 时,或
-
需要修改指定 Collection 的 TTL 时。
在创建 Collection 时设置 TTL
如下示例演示了如何在创建 Collection 时设置 TTL。
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient
# With TTL
client.create_collection(
collection_name="customized_setup_5",
schema=schema,
# highlight-start
properties={
"collection.ttl.seconds": 1209600
}
# highlight-end
)
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.AlterCollectionReq;
import io.milvus.param.Constant;
import java.util.HashMap;
import java.util.Map;
// With TTL
CreateCollectionReq customizedSetupReq5 = CreateCollectionReq.builder()
.collectionName("customized_setup_5")
.collectionSchema(schema)
// highlight-next-line
.property(Constant.TTL_SECONDS, "1209600")
.build();
client.createCollection(customizedSetupReq5);
const createCollectionReq = {
collection_name: "customized_setup_5",
schema: schema,
// highlight-start
properties: {
"collection.ttl.seconds": 1209600
}
// highlight-end
}
import (
"context"
"fmt"
"log"
"github.com/milvus-io/milvus/client/v2/milvusclient"
"github.com/milvus-io/milvus/pkg/common"
)
err = cli.CreateCollection(ctx, client.NewCreateCollectionOption("customized_setup_5", schema).
WithProperty(common.CollectionTTLConfigKey, 1209600)) // TTL in seconds
if err != nil {
// handle error
}
fmt.Println("collection created")
export params='{
"ttlSeconds": 1209600
}'
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_5\",
\"schema\": $schema,
\"params\": $params
}"
修改指定 Collection 的 TTL
如下示例演示了如何修改指定 Collection 的 TTL。
- Python
- Java
- NodeJS
- Go
- cURL
client.alter_collection_properties(
collection_name="my_collection",
properties={"collection.ttl.seconds": 1209600}
)
Map<String, String> properties = new HashMap<>();
properties.put("collection.ttl.seconds", "1209600");
AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
.collectionName("my_collection")
.properties(properties)
.build();
client.alterCollection(alterCollectionReq);
res = await client.alterCollection({
collection_name: "my_collection",
properties: {
"collection.ttl.seconds": 1209600
}
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
err = cli.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").WithProperty(common.CollectionTTLConfigKey, 60))
if err != nil {
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/alter_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_5\",
\"properties\": {
\"collection.ttl.seconds\": 1209600
}
}"
重置 TTL
如果您希望 Collection 中的数据能够永久保存,您可以通过重置 TTL 设置来实现这一点。
- Python
- Java
- NodeJS
- Go
- cURL
client.drop_collection_properties(
collection_name="my_collection",
property_keys=["collection.ttl.seconds"]
)
propertyKeys = new String[1]
propertyKeys[0] = "collection.ttl.second"
DropCollectionReq dropCollectionReq = DropCollectionReq.builder()
.collectionName("my_collection")
.propertyKeys(propertyKeys)
.build();
client.dropCollection(dropCollectionReq);
res = await client.dropCollectionProperties({
collection_name: "my_collection",
properties: ["collection.ttl.seconds"]
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
err = cli.DropCollectionProperties(ctx, milvusclient.NewDropCollectionPropertiesOption("my_collection", common.CollectionTTLConfigKey))
if err != nil {
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/alter_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_5\",
\"properties\": {
\"collection.ttl.seconds\": 60
}
}"