azure-sdk-for-net
azure-sdk-for-net copied to clipboard
How do we pass cosmos DB partition key definition while creating a cosmos DB SQL Container?
Library name and version
Azure.ResourceManager.CosmosDB 1.0.1
Query/Question
I am trying to create a cosmos db partition using the below sample. I am using managed identity to authenticate my requests. I am using Azure.ResourceManager v1.3.1 and Azure.ResourceManager.CosmosDB v1.0.1
While trying to run below snippet, I am getting:
Azure.RequestFailedException: 'Message: {"code":"BadRequest","message":"Message: {"Errors": ["A Partition key definition is not specified in the request."]}
I am not sure how we to pass the partition key definition while creating a cosmos DB SQL Container. Can anyone please help here. Thanks.
private static async Task TestStub1()
{
var subscriptionId = "xxx";
var resourceGroupName = "xxx";
var accountName = "xxx";
var databaseName = "xxx";
var containerName = "xxx";
var throughputProperties = ThroughputProperties.CreateAutoscaleThroughput(4000);
var accountEndpoint = "xxx";
var location = AzureLocation.EastUS;
try
{
var tokenCredential = new DefaultAzureCredential();
var armClient = new ArmClient(tokenCredential);
var dbAccountIdentifier = new ResourceIdentifier($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DocumentDB/databaseAccounts/{accountName}");
var dbAccount = armClient.GetCosmosDBAccountResource(dbAccountIdentifier);
var databases = dbAccount.GetCosmosDBSqlDatabases();
var cosmosDBSqlDatabaseResourceInfo = new CosmosDBSqlDatabaseResourceInfo(databaseName);
var cosmosDBSqlDatabaseCreateOrUpdateContent = new CosmosDBSqlDatabaseCreateOrUpdateContent(location, cosmosDBSqlDatabaseResourceInfo);
var cosmosDBSqlResource = await databases.CreateOrUpdateAsync(Azure.WaitUntil.Completed, databaseName, cosmosDBSqlDatabaseCreateOrUpdateContent);
if (cosmosDBSqlResource.HasValue)
{
var cosmosDBSqlContainers = cosmosDBSqlResource.Value.GetCosmosDBSqlContainers();
var cosmosDBContainerPartitionKey = new CosmosDBContainerPartitionKey();
cosmosDBContainerPartitionKey.Kind = CosmosDBPartitionKind.Hash;
var cosmosDBSqlContainerResourceInfo = new CosmosDBSqlContainerResourceInfo(containerName);
cosmosDBSqlContainerResourceInfo.PartitionKey = cosmosDBContainerPartitionKey;
var cosmosDBSqlContainerCreateOrUpdateContent = new CosmosDBSqlContainerCreateOrUpdateContent(location, cosmosDBSqlContainerResourceInfo);
var cosmosDBSqlContainer = await cosmosDBSqlContainers.CreateOrUpdateAsync(WaitUntil.Completed, containerName, cosmosDBSqlContainerCreateOrUpdateContent);
}
}
catch (Exception ex)
{
throw;
}
}
Environment
.NET Framework 4.7.2
Microsoft Visual Studio Enterprise 2022 (64-bit) - Current Version 17.3.4
Label prediction was below confidence level 0.6 for Model:ServiceLabels: 'Cosmos:0.581087,Storage:0.115030214,Azure.Identity:0.04924447'
Thank you for your feedback. Tagging and routing to the team member best able to assist.
@vaibhavsingla according to this document https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview#choose-partitionkey you should specify the key partition path, you can use the below code to setup partition key path
public void CreateCosmosDB(string resourceGroupName)
{
var rg=this._client.GetDefaultSubscription().GetResourceGroup(resourceGroupName).Value;
var dbAccount = rg.GetCosmosDBAccount("elendiltest").Value;
var databases = dbAccount.GetCosmosDBSqlDatabases();
var databaseName = "xxx";
var location = AzureLocation.EastUS;
var containerName = "xxx";
var cosmosDBSqlDatabaseResourceInfo = new CosmosDBSqlDatabaseResourceInfo(databaseName);
var cosmosDBSqlDatabaseCreateOrUpdateContent = new CosmosDBSqlDatabaseCreateOrUpdateContent(location, cosmosDBSqlDatabaseResourceInfo);
var cosmosDBSqlResource = databases.CreateOrUpdate(Azure.WaitUntil.Completed, databaseName, cosmosDBSqlDatabaseCreateOrUpdateContent);
if (cosmosDBSqlResource.HasValue)
{
var cosmosDBSqlContainers = cosmosDBSqlResource.Value.GetCosmosDBSqlContainers();
var cosmosDBContainerPartitionKey = new CosmosDBContainerPartitionKey();
cosmosDBContainerPartitionKey.Kind = CosmosDBPartitionKind.Hash;
cosmosDBContainerPartitionKey.Paths.Add("/userId"); //Setup partition key path
var cosmosDBSqlContainerResourceInfo = new CosmosDBSqlContainerResourceInfo(containerName);
cosmosDBSqlContainerResourceInfo.PartitionKey = cosmosDBContainerPartitionKey;
var cosmosDBSqlContainerCreateOrUpdateContent = new CosmosDBSqlContainerCreateOrUpdateContent(location, cosmosDBSqlContainerResourceInfo);
var cosmosDBSqlContainer = cosmosDBSqlContainers.CreateOrUpdate(WaitUntil.Completed, containerName, cosmosDBSqlContainerCreateOrUpdateContent);
}
}
If you have any other feedback regarding our Azure SDK, feel free to let us know in this survey
Hi, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!