Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all the Partition Keys in Azure Cosmos DB collection

I have recently started using Azure Cosmos DB in our project. For the reporting purpose, we need to get all the Partition Keys in the collection. I could not find any suitable API to achieve it.

like image 614
user2364893 Avatar asked Jul 14 '17 01:07

user2364893


People also ask

How do I get Azure cosmos access key?

Navigate to your Azure Cosmos DB account on the Azure portal. Select Keys from the left menu, then select Regenerate Primary Key from the ellipsis on the right of your primary key.

What is Cosmos partition key?

Cosmos DB Partition Keys Overview Partition keys are the core element to distributing your data efficiently into different logical and physical sets so that the queries performed against the database are completed as quickly as possible.

How many partitions does Cosmos DB have?

There is no limit to the number of logical partitions in your container. Each logical partition can store up to 20GB of data. Good partition key choices have a wide range of possible values.


1 Answers

UPDATE: According to Brian in the comments below, DISTINCT is now supported. Try something like:

SELECT DISTINCT c.partitionKey FROM c

Prior answer (could still be the best way if the SELECT DISTINCT... approach doesn't scale)

The only way to get the actual partition key values is to do a unique aggregate on that field. However, there is a relatively easy way to get the partition key ranges. It's not supported directly in any of the SDKs as far as I know, but you can directly hit the REST endpoint at https://{your endpoint domain}.documents.azure.com/dbs/{your collection's uri fragment}/pkranges to pull back the ranges for the partition keys for each partition. Note, the lower side is inclusive, so you can use that to do your own fan out.

Warning: There is a slim possibility that the pkranges can change between the time you retrieve them and the time you go to do something with them. Either accept this slim risk or code around it.

like image 138
Larry Maccherone Avatar answered Sep 30 '22 18:09

Larry Maccherone