CosmosDB - Setting a Partition Key

Paul Foley 1 Reputation point
2022-10-07T16:21:37.53+00:00

Can anyone tell me how to programmatically set a partition key on cosmosdb?

If you have an example (don't mind which language) id be really grateful.

Thanks,
Paul.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Oury Ba-MSFT 21,121 Reputation points Microsoft Employee Moderator
    2022-10-07T20:10:20.883+00:00

    Hi @Anonymous Thank you for posting your question on Microsoft Q&A and for using Azure Services.

    As per my understanding you are trying to automatically set a partition key on Cosmos DB. Please let me know if my understanding is not correct.

    Programmatically setting partition KEY in Azure Cosmos DB is not yet available. You can choose the partition key for your container but cannot automatically set up partition key.
    Azure Cosmos DB automatically can create new physical partitions by splitting existing ones. see Partitioning and horizontal scaling in Azure Cosmos DB

    248508-image.png

    Regards,
    Oury

    0 comments No comments

  2. Paul Foley 1 Reputation point
    2022-10-08T16:09:41.267+00:00

    Hi Oury,
    thanks for the answer - that's not good.

    I create a lot of data in my app (about 20-25 gb) which is the limit for the partition keys, how would you address needing more partition keys?

    Thanks,
    Paul.


  3. Matteo Bortolazzo 1 Reputation point
    2022-10-09T21:51:27.707+00:00

    I think it's a bit too late if a partition hits the actual limit.

    It depends on your use cases. If it's a write-heavy, read-heavy scenario, you can use a hierarchical partition key on two or more properties. If you don't care about reading the data (for example you have a change feed processor or a connector to Kafka), you can use a synthetic partition key composed of multiple fields and even add a random value at the end, so there are as many partitions are documents (which is fine).

    In both cases, you need to create a new container and migrate. The existing partitions are full, and you can't change the partition key path. You can do it with a FeedIterator on the change feed with the pull model. Or you can find migration tools, I think Alpaqa offers one.

    Technically you can even leave the data where it is and store new data in a new container, but it always depends on how you use them.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.