在 Azure Cosmos 容器上预配标准(手动)吞吐量Provision standard (manual) throughput on an Azure Cosmos container

本文介绍如何在 Azure Cosmos DB 中为容器(集合、图形或表)预配标准(手动)吞吐量。This article explains how to provision standard (manual) throughput on a container (collection, graph, or table) in Azure Cosmos DB. 可以为单个容器预配吞吐量,也可以为数据库预配吞吐量,并在数据库中的容器之间共享。You can provision throughput on a single container, or provision throughput on a database and share it among the containers within the database. 可以使用 Azure 门户、Azure CLI 或 Azure Cosmos DB SDK 为容器预配吞吐量。You can provision throughput on a container using Azure portal, Azure CLI, or Azure Cosmos DB SDKs.

Azure 门户Azure portal

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户,或选择现有的 Azure Cosmos 帐户。Create a new Azure Cosmos account, or select an existing Azure Cosmos account.

  3. 打开“数据资源管理器”窗格,然后选择“新建集合” 。Open the Data Explorer pane, and select New Collection. 接下来,请提供以下详细信息:Next, provide the following details:

    • 表明要创建新数据库还是使用现有数据库。Indicate whether you are creating a new database or using an existing one.
    • 输入容器(或表或图)ID。Enter a Container (or Table or Graph) ID.
    • 输入分区键值(例如 /userid)。Enter a partition key value (for example, /userid).
    • 输入要预配的吞吐量(例如,1000 RU)Enter a throughput that you want to provision (for example, 1000 RUs).
    • 选择“确定” 。Select OK.

    数据资源管理器的屏幕截图,突出显示“新建集合”

Azure CLI 或 PowerShellAzure CLI or PowerShell

若要创建具有专用吞吐量的容器,请参阅To create a container with dedicated throughput see,

备注

若要在 Azure Cosmos DB 帐户(使用用于 MongoDB 的 Azure Cosmos DB API)为容器预配吞吐量,请使用 /myShardKey 作为分区键路径。If you are provisioning throughput on a container in an Azure Cosmos account configured with the Azure Cosmos DB API for MongoDB, use /myShardKey for the partition key path. 若要使用 Cassandra API 在 Azure Cosmos DB 帐户中为容器预配吞吐量,请使用 /myPrimaryKey 作为分区键路径。If you are provisioning throughput on a container in an Azure Cosmos account configured with Cassandra API, use /myPrimaryKey for the partition key path.

.NET SDK.NET SDK

备注

使用适用于 SQL API 的 Cosmos SDK 为除 Cassandra 和 MongoDB API 之外的所有 Cosmos DB API 预配吞吐量。Use the Cosmos SDKs for SQL API to provision throughput for all Cosmos DB APIs, except Cassandra and MongoDB API.

SQL、Gremlin 和表 APISQL, Gremlin, and Table APIs

// Create a container with a partition key and provision throughput of 400 RU/s
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = "myContainerName";
myCollection.PartitionKey.Paths.Add("/myPartitionKey");

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

JavaScript SDKJavaScript SDK

// Create a new Client
const client = new CosmosClient({ endpoint, key });

// Create a database
const { database } = await client.databases.createIfNotExists({ id: "databaseId" });

// Create a container with the specified throughput
const { resource } = await database.containers.createIfNotExists({
id: "containerId",
throughput: 1000
});

// To update an existing container or databases throughput, you need to user the offers API
// Get all the offers
const { resources: offers } = await client.offers.readAll().fetchAll();

// Find the offer associated with your container or the database
const offer = offers.find((_offer) => _offer.offerResourceId === resource._rid);

// Change the throughput value
offer.content.offerThroughput = 2000;

// Replace the offer.
await client.offer(offer.id).replace(offer);

MongoDB APIMongoDB API

// refer to MongoDB .NET Driver
// https://docs.mongodb.com/drivers/csharp

// Create a new Client
String mongoConnectionString = "mongodb://DBAccountName:Password@DBAccountName.documents.azure.cn:10255/?ssl=true&replicaSet=globaldb";
mongoUrl = new MongoUrl(mongoConnectionString);
mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
mongoClient = new MongoClient(mongoClientSettings);

// Change the database name
mongoDatabase = mongoClient.GetDatabase("testdb");

// Change the collection name, throughput value then update via MongoDB extension commands
// https://docs.azure.cn/cosmos-db/mongodb-custom-commands#update-collection

var result = mongoDatabase.RunCommand<BsonDocument>(@"{customAction: ""UpdateCollection"", collection: ""testcollection"", offerThroughput: 400}");

Cassandra APICassandra API

类似的命令可以通过任何 CQL 兼容的驱动程序发出。Similar commands can be issued through any CQL-compliant driver.

// Create a Cassandra table with a partition (primary) key and provision throughput of 400 RU/s
session.Execute("CREATE TABLE myKeySpace.myTable(
    user_id int PRIMARY KEY,
    firstName text,
    lastName text) WITH cosmosdb_provisioned_throughput=400");

修改或更改 Cassandra 表的吞吐量Alter or change throughput for Cassandra table

// Altering the throughput too can be done through code by issuing following command
session.Execute("ALTER TABLE myKeySpace.myTable WITH cosmosdb_provisioned_throughput=5000");

后续步骤Next steps

请参阅以下文章,了解如何在 Azure Cosmos DB 中预配吞吐量:See the following articles to learn about throughput provisioning in Azure Cosmos DB: