繁体   English   中英

有没有办法以编程方式更改 cosmos db 表上的 TTL

[英]Is there a way to programmatically change TTL on a cosmos db Table

如标题所述,我正在尝试更改 cosmos db 表的 TTL。 我在 c#/powershell/arm 模板中找不到任何东西这是我想要实现的在此处输入图像描述 我唯一能找到的是在 azure 门户中触发的 api 调用,但我想知道直接使用这个 API 是否安全? 在此处输入图像描述

In Cosmos DB Table API, Tables are essentially Containers thus you can use Cosmos DB SQL API SDK to manipulate the Table. 这是执行此操作的示例代码:

    var cosmosClient = new CosmosClient(CosmosConnectionString);

    var database = cosmosClient.GetDatabase(Database);
    var container = database.GetContainer("test");
    var containerResponse = await container.ReadContainerAsync();
    var containerProperties = containerResponse.Resource;
    Console.WriteLine("Current TTL on the container is: " + containerProperties.DefaultTimeToLive);
    containerProperties.DefaultTimeToLive = 120;//
    containerResponse = await container.ReplaceContainerAsync(containerProperties);
    containerProperties = containerResponse.Resource;
    Console.WriteLine("Current TTL on the container is: " + containerProperties.DefaultTimeToLive);
    Console.ReadKey();

现在支持通过Microsoft.Azure.Cosmos.Table直接设置 TTL,版本 >= 1.0.8。

// Get the table reference for table operations
CloudTable table = <tableClient>.GetTableReference(<tableName>);

table.CreateIfNotExists(defaultTimeToLive: <ttlInSeconds>);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM