Anupam Chand
Anupam Chand

Reputation: 2687

Update Cosmos throughput via python SDK

Our use case is we have a Cosmos DB with a bunch of documents. These are exposed via an function API. The usage of the API is low but each night , we need to insert a big batch of documents using data factory. When this process runs, it consumes all the Request units provisioned (400-4000 RU/s). We are therefore looking for a solution where we can update the throughput programmatically, execute the upload of documents, and then bring the throughout down again.

We are looking to do this using python SDK via function execution. I found sample code Update throughput using python online but this seems to only work when the throughput is manually provisioned. When I tried the same code with an auto scaled throughout, there was no error but it did not get updated either.

Can someone point me to some sample code where I can update the throughput when it is set to auto scale?

Upvotes: 0

Views: 1124

Answers (3)

virtualdvid
virtualdvid

Reputation: 2411

Finally there is a way:

from azure.cosmos import CosmosClient, PartitionKey, ThroughputProperties

client = CosmosClient(
    url='url',
    credential='key',
    connection_mode='Gateway'
)

dbClient = client.get_database_client('database_name')
container = dbClient.get_container_client('container_name')

throughput_properties = ThroughputProperties(auto_scale_max_throughput=1000)
container.replace_throughput(throughput_properties)

We can also create databases or containers with autoscale throughput

dbClient.create_database_if_not_exists(
    id='database_name',
    offer_throughput=throughput_properties
)

dbClient.create_container_if_not_exists(
    id='container_name',
    partition_key=PartitionKey(path='/partition_key'),
    offer_throughput=throughput_properties
)

Upvotes: 0

Rodrigo Souza
Rodrigo Souza

Reputation: 51

This gap is now listed in the SDK limitations section and there are no plans to address it in short term. Workarounds are CLI and PowerShell.

For more information, check here:

https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/cosmos/azure-cosmos#limitations

Tks

Upvotes: 1

Gaurav Mantri
Gaurav Mantri

Reputation: 136246

Can someone point me to some sample code where I can update the throughput when it is set to auto scale?

I looked up the SDK code here and I believe the ability to change auto-scale throughput is not available in the current version of SDK (version 4.2.0).

There's also an open issue on Github regarding the same: https://github.com/Azure/azure-sdk-for-python/issues/17518. I would suggest you track that issue as to when this feature will be available in the SDK.

Upvotes: 1

Related Questions