在 Microsoft Fabric 的 Cosmos DB 中配置存活时间(TTL)

Cosmos DB 中的生存时间(TTL)功能可帮助你在指定时间段( 以秒为单位)后自动删除项来管理数据的生命周期。 在本指南中,您可以使用 Fabric 门户或 Azure SDK 来修改容器和项级别上的 TTL 值。

先决条件

  • Python 3.12 或更高版本
  • Node.js 22 或更高版本
  • .NET SDK 9.0 或更高版本

在容器级别通过 Fabric 门户进行设置

首先,使用 Fabric 门户设置容器级 TTL。

  1. 打开 Fabric 门户(https://app.fabric.microsoft.com)。

  2. 请进入您现有的 Cosmos DB 数据库。

  3. 选择并展开现有容器。 然后选择 “设置”。

  4. “设置” 部分中,选择“冗余 设置” 选项卡。

  5. 找到 生存时间 (TTL) 设置。

    Fabric 门户中某个容器的“设置”部分的屏幕截图。

  6. 将设置更新为新值。 例如,可以将值设置为 On(无默认值), 将容器级别的默认生存时间设置为 -1无限),其中启用了 TTL,但默认情况下项不会过期。

  7. 选择“保存”以保存更改。

    小窍门

    有关各种生存时间(TTL)配置值的详细信息,请参阅 生存时间

在项目级别设置

接下来,更新项以在项级别设置 TTL 值。 Cosmos DB 中的项表示为 JSON 文档。 ttl每个文档的属性用于设置每个特定项的生存时间。 可以设置或未设置该 ttl 属性,相应的值会影响 TTL 过期的行为。

[
  {
    "name": "Heatker Women's Jacket",
    "category": "apparel"
  },
  {
    "name": "Vencon Kid's Coat",
    "category": "apparel",
    "ttl": -1
  },
  {
    "name": "Pila Swimsuit",
    "category": "apparel",
    "ttl": 3600
  }
]

使用 Azure SDK 在容器级别设置

现在,使用 Azure SDK 在容器级别设置 TTL 以应用于所有项。

database = client.get_database_client("<database-name>")

container = database.get_container_client("<container-name>")

# Set the container-level TTL to 7 days
await database.replace_container(
  container, 
  partition_key=PartitionKey(path='/<partition-key-path>'), 
  default_ttl=60 * 60 * 24 * 7
)
const container: Container = client.database('<database-name>').container('<container-name>');

const { resource: containerProperties } = await container.read();

if (!containerProperties) {
    return;
}

// Set the container-level TTL to 7 days
containerProperties.defaultTtl = 60 * 60 * 24 * 7;

await container.replace(containerProperties);
Container container = client
    .GetDatabase("<database-name>")
    .GetContainer("<container-name>");

ContainerProperties properties = await container.ReadContainerAsync();

// Set the container-level TTL to 7 days
properties.DefaultTimeToLive = 60 * 60 * 24 * 7;

await container.ReplaceContainerAsync(properties);

在项级别使用 Azure SDK 进行设置

最后,再次使用 Azure SDK 设置项级 TTL 值以替代容器级值。

container = client.get_database_client("<database-name>").get_container_client("<container-name>")

# Set the item-level TTL to 1 day
item = {
    "id": "ffffffff-5555-6666-7777-aaaaaaaaaaaa",
    "name": "Pila Swimsuit",
    "category": "apparel",
    "ttl": 60 * 60 * 24
}

await container.upsert_item(item)
const container: Container = client.database('<database-name>').container('<container-name>');

// Set the item-level TTL to 1 day
let item = {
    id: 'ffffffff-5555-6666-7777-aaaaaaaaaaaa',
    name: 'Pila Swimsuit',
    category: 'apparel',
    ttl: 60 * 60 * 24
};

await container.items.upsert(item);
Container container = client
    .GetDatabase("<database-name>")
    .GetContainer("<container-name>");

// Set the item-level TTL to 1 day
var item = new {
    id = "ffffffff-5555-6666-7777-aaaaaaaaaaaa",
    name = "Pila Swimsuit",
    category = "apparel",
    ttl = 60 * 60 * 24
};

await container.UpsertItemAsync(item);