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

↘锁芯ラ 提交于 2020-08-11 18:39:51

问题


As the title describes, I'm trying to change the TTL of a cosmos db table. I couldn't find anything in c#/powershell/arm templates Here is what I'm trying to achieve The only thing I was able to find is the api call that is triggered in azure portal, but I'm wondering if it is safe to use this API directly?


回答1:


In Cosmos DB Table API, Tables are essentially Containers thus you can use Cosmos DB SQL API SDK to manipulate the Table. Here's the sample code to do so:

    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();


来源:https://stackoverflow.com/questions/62550860/is-there-a-way-to-programmatically-change-ttl-on-a-cosmos-db-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!