コレクションTTLの設定
データがコレクションに挿入されると、デフォルトではそこに残り続けます。ただし、一定期間後にデータを削除またはクリーンアップしたい場合があります。このような場合、コレクションのTTL(Time-to-Live)プロパティを設定し、TTLが期限切れになったときにZilliz Cloudがデータを自動的に削除するようにできます。
概要
TTL(Time-to-Live)は、データベースで一般的に使用される概念であり、挿入または変更後の一定期間のみデータの有効性またはアクセシビリティを維持すべきケースに使用されます。その後、データは自動的に削除されます。
例えば、毎日データをインジェストするが、過去14日間のレコードのみを保存する必要がある場合、コレクションのTTLを14 × 24 × 3600 = 1209600秒に設定することで、Zilliz Cloudがそれより古いすべてのデータを自動的に削除するように構成できます。これにより、コレクション内には直近14日分のデータのみが保持されることが保証されます。
期限切れのエンティティは検索またはクエリ結果には表示されません。ただし、データは後続のデータコンパクションが行われるまで(24時間以内に行われる予定)ストレージに残ることがあります。
Zilliz CloudコレクションのTTLプロパティは、秒単位の整数で指定されます。設定後、TTLを超えたデータはコレクションから自動的に削除されます。
削除プロセスは非同期であるため、指定されたTTLの経過後に検索結果からデータが正確に削除されるとは限りません。代わりに、ごみ集め(GC)およびコンパクションプロセスが不定期に行われるため、削除には遅延が生じることがあります。
TTLの設定
TTLプロパティは以下のタイミングで設定できます:
コレクション作成時のTTL設定
以下のコードスニペットは、コレクション作成時にTTLプロパティを設定する方法を示しています。
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient
# TTL付き
client.create_collection(
collection_name="my_collection",
schema=schema,
properties={
"collection.ttl.seconds": 1209600
}
)
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;
// TTL付き
CreateCollectionReq customizedSetupReq = CreateCollectionReq.builder()
.collectionName("my_collection")
.collectionSchema(schema)
.property(Constant.TTL_SECONDS, "1209600")
.build();
client.createCollection(customizedSetupReq);
const createCollectionReq = {
collection_name: "my_collection",
schema: schema,
properties: {
"collection.ttl.seconds": 1209600
}
}
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("my_collection", schema).
WithProperty(common.CollectionTTLConfigKey, 1209600)) // TTL in seconds
if err != nil {
fmt.Println(err.Error())
// handle error
}
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\": \"my_collection\",
\"schema\": $schema,
\"params\": $params
}"
既存コレクションのTTL設定
以下のコードスニペットは、既存のコレクションで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
}
})
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").
WithProperty(common.CollectionTTLConfigKey, 60))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/alter_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_collection\",
\"properties\": {
\"collection.ttl.seconds\": 1209600
}
}"
TTL設定の削除
コレクション内のデータを無期限に保持することにした場合、そのコレクションから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"]
})
err = client.DropCollectionProperties(ctx, milvusclient.NewDropCollectionPropertiesOption("my_collection", common.CollectionTTLConfigKey))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/alter_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_collection\",
\"properties\": {
\"collection.ttl.seconds\": 60
}
}"