简体   繁体   English

我们如何在使用 Azure.ResourceManager.CosmosDB 1.0.1 创建 cosmos DB SQL 容器时传递 cosmos DB 分区键定义

[英]How do we pass cosmos DB partition key definition while creating a cosmos DB SQL Container using Azure.ResourceManager.CosmosDB 1.0.1

I am trying to create a cosmos db partition using the below sample.我正在尝试使用以下示例创建一个 cosmos db 分区。 I am using managed identity to authenticate my requests.我正在使用托管身份来验证我的请求。 I am using Azure.ResourceManager v1.3.1 and Azure.ResourceManager.CosmosDB v1.0.1我正在使用 Azure.ResourceManager v1.3.1 和 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."]} Azure.RequestFailedException: 'Message: {"code":"BadRequest","message":"Message: {"Errors": ["A Partition key definition is not specified in the request."]}

I am using .NET Framework 4.7.2我正在使用 .NET Framework 4.7.2

I am not sure how we to pass the partition key definition while creating a cosmos DB SQL Container.我不确定我们如何在创建 cosmos DB SQL 容器时传递分区键定义。 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;
        }
    }

This is a sample for Microsoft.Azure.Management.CosmosDB but should be the same.这是 Microsoft.Azure.Management.CosmosDB 的示例,但应该相同。

Assuming a string parameter of partitionKey this is how you create a partition key definition.假设partitionKey的字符串参数是创建分区键定义的方式。

PartitionKey = new ContainerPartitionKey
{
   Kind = "Hash",
   Paths = new List<string> { partitionKey },
   Version = 1 //version 2 for large partition key
},

More samples like this at Azure Management Libraries for .NET for Azure Cosmos DB Azure 管理库 .NET Azure Cosmos DB 的更多示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM