Nikunj Aggarwal
Nikunj Aggarwal

Reputation: 406

Update Azure Cosmos db partition key to re-distribute existing data (document db)

Currently, my data is in azure cosmos db (DocumentDB) with wrong or un-optimized partition key. Now I want to update partition key and re distribute my data but not able to figure it out. How can I do it?

Upvotes: 12

Views: 7768

Answers (3)

Sarang Kulkarni
Sarang Kulkarni

Reputation: 367

Its not possible in the same container, however you can create new containers in the same database with same or different partition key and then you can provision ADF pipelines on top of your source collections (you can update partition key using derived column) and dump in the target collections.

Upvotes: 0

Sunil Purushothaman
Sunil Purushothaman

Reputation: 9501

Firstly, take advantage of the auto-migration feature, by using the V3 version of SDKs. After that, your container definition will be enhanced with a _partitionKey property. Important bit is that, the existing documents within the container aren’t auto migrated.

You will have to repartition the existing documents by reading them one by one through a script or program, and rewrite them back with _partitionKey property in the documents.

You can use a combination of Item Id and PartitionKey.None to iterate through existing documents, for example,

CosmosItemResponse<MyItem> readResponse = 
await migratedContainer.Items.ReadItemAsync<MyItem>( 
  partitionKey: PartitionKey.None, 
  id: device.Id
);

Refer this Microsoft documentation for further information:

Optional: Bulk Update

You can leverage the Bulk update library, which is available in .NET and Java flavors. Microsoft documentation link

Upvotes: 0

Gaurav Mantri
Gaurav Mantri

Reputation: 136206

Now I want to update partition key and re distribute my data but not able to figure it out how can I do it.

Currently it is not possible to update the partition key attribute in a collection or change partition key value in a document.

If you want to change the partition key attribute, you would need to delete that collection and create a new one with correct partition key attribute.

Similarly, if you wish to update the partition key value in a document you must first delete the document and create a new document with correct partition key value.

Upvotes: 14

Related Questions