使用不同的分区模式在 Azure DocumentDB 中创建集合

Create Collection in Azure DocumentDB with Different Partition Mode

有一些示例代码使用 Microsoft.Azure.DocumentDB 在 azure documentDB 中创建集合。但是,我找不到有关如何使用 c# 创建具有不同分区模式的集合的信息。

门户中有两种模式:单一分区和分区。

我们可以在使用 Microsoft.Azure.DocumentDB 创建集合时使用一个或另一个吗?

您需要 SDK 版本 1.6.0 或更高版本才能支持文档数据库分区。 使用 SDK,您需要设置 OfferThroughput 值,如下所示。

在此示例中,我们将 /deviceId 设置为分区键。

DocumentClient client = new DocumentClient(new Uri(endpoint), authKey);
await client.CreateDatabaseAsync(new Database { Id = "db" });

// Collection for device telemetry. Here the JSON property deviceId will be used as the partition key to 
// spread across partitions. Configured for 10K RU/s throughput and an indexing policy that supports 
// sorting against any number or string property.
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = "coll";
myCollection.PartitionKey.Paths.Add("/deviceId");

await client.CreateDocumentCollectionAsync(
    UriFactory.CreateDatabaseUri("db"),
    myCollection,
    new RequestOptions { OfferThroughput = 20000 });

注:

为了创建分区的 collections,您必须指定一个 throughput 每秒 > 10,000 个请求单位的值。由于吞吐量是 100 的倍数,因此必须为 10,100 或更高。

因此,当您的 OfferThroughput 设置为小于 20000 时,您的 collection 将是单一分区。