在 Azure Cosmos DB 中的数据库或容器上预配自动缩放吞吐量Provision autoscale throughput on database or container in Azure Cosmos DB

本文介绍了如何为 Azure Cosmos DB 中的数据库或容器(集合、图形或表)预配自动缩放吞吐量。This article explains how to provision autoscale throughput on a database or container (collection, graph, or table) in Azure Cosmos DB. 可以为单个容器启用自动缩放,也可以为某个数据库预配自动缩放吞吐量,然后在该数据库中的所有容器之间共享此吞吐量。You can enable autoscale on a single container, or provision autoscale throughput on a database and share it among all the containers in the database.

Azure 门户Azure portal

创建支持自动缩放的新数据库或容器Create new database or container with autoscale

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

  2. 导航到你的 Azure Cosmos DB 帐户,打开“数据资源管理器”选项卡。Navigate to your Azure Cosmos DB account and open the Data Explorer tab.

  3. 选择“新建容器”。Select New Container. 为你的数据库、容器输入一个名称并输入分区键。Enter a name for your database, container, and a partition key. 在“吞吐量”下选择“自动缩放”选项,并设置希望数据库或容器缩放到的最大吞吐量(RU/秒)Under Throughput, select the autoscale option, and set the maximum throughput (RU/s) that you want the database or container to scale to.

    创建容器并配置自动缩放预配吞吐量

  4. 选择“确定”。Select OK.

若要在共享吞吐量数据库上预配自动缩放,请在创建新数据库时选择“预配数据库吞吐量”选项。To provision autoscale on shared throughput database, select the Provision database throughput option when creating a new database.

在现有的数据库或容器上启用自动缩放Enable autoscale on existing database or container

重要

在当前版本中,只能通过 Azure 门户在自动缩放与标准(手动)预配吞吐量之间进行迁移。In the current release, the Azure portal is the only way to migrate between autoscale and standard (manual) provisioned throughput.

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

  2. 导航到你的 Azure Cosmos DB 帐户,打开“数据资源管理器”选项卡。Navigate to your Azure Cosmos DB account and open the Data Explorer tab.

  3. 为你的容器选择“缩放和设置”,或者为你的数据库选择“缩放”。 Select Scale and Settings for your container, or Scale for your database.

  4. 在“缩放”下,依次选择“自动缩放”选项、“保存”。 Under Scale, select the Autoscale option and Save.

    在现有容器上启用自动缩放

备注

在现有数据库或容器上启用自动缩放时,最大 RU/秒的起始值由系统根据当前手动预配的吞吐量设置和存储确定。When you enable autoscale on an existing database or container, the starting value for max RU/s is determined by the system, based on your current manual provisioned throughput settings and storage. 在操作完成后,你可以根据需要更改最大 RU/秒。After the operation completes, you can change the max RU/s if needed. 了解详细信息。Learn more.

适用于 SQL API 的 Azure Cosmos DB .NET V3 SDKAzure Cosmos DB .NET V3 SDK for SQL API

可以使用适用于 SQL API 的 Azure Cosmos DB .NET SDK 3.9 或更高版本来管理自动缩放资源。Use version 3.9 or higher of the Azure Cosmos DB .NET SDK for SQL API to manage autoscale resources.

重要

可以使用该 .NET SDK 创建新的自动缩放资源。You can use the .NET SDK to create new autoscale resources. 该 SDK 不支持在自动缩放与标准(手动)吞吐量之间迁移。The SDK does not support migrating between autoscale and standard (manual) throughput. 目前只有 Azure 门户支持迁移方案。The migration scenario is currently supported in only the Azure portal.

创建具有共享吞吐量的数据库Create database with shared throughput

// Create instance of CosmosClient
CosmosClient cosmosClient = new CosmosClient(Endpoint, PrimaryKey);

// Autoscale throughput settings
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.CreateAutoscaleThroughput(4000); //Set autoscale max RU/s

//Create the database with autoscale enabled
database = await cosmosClient.CreateDatabaseAsync(DatabaseName, throughputProperties: autoscaleThroughputProperties);

创建具有专用吞吐量的容器Create container with dedicated throughput

// Get reference to database that container will be created in
Database database = await cosmosClient.GetDatabase("DatabaseName");

// Container and autoscale throughput settings
ContainerProperties autoscaleContainerProperties = new ContainerProperties("ContainerName", "/partitionKey");
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.CreateAutoscaleThroughput(4000); //Set autoscale max RU/s

// Create the container with autoscale enabled
container = await database.CreateContainerAsync(autoscaleContainerProperties, autoscaleThroughputProperties);

读取当前吞吐量(RU/秒)Read the current throughput (RU/s)

// Get a reference to the resource
Container container = cosmosClient.GetDatabase("DatabaseName").GetContainer("ContainerName");

// Read the throughput on a resource
ThroughputProperties autoscaleContainerThroughput = await container.ReadThroughputAsync(requestOptions: null); 

// The autoscale max throughput (RU/s) of the resource
int? autoscaleMaxThroughput = autoscaleContainerThroughput.AutoscaleMaxThroughput;

// The throughput (RU/s) the resource is currently scaled to
int? currentThroughput = autoscaleContainerThroughput.Throughput;

更改自动缩放最大吞吐量(RU/秒)Change the autoscale max throughput (RU/s)

// Change the autoscale max throughput (RU/s)
await container.ReplaceThroughputAsync(ThroughputProperties.CreateAutoscaleThroughput(newAutoscaleMaxThroughput));

适用于 SQL API 的 Azure Cosmos DB Java V4 SDKAzure Cosmos DB Java V4 SDK for SQL API

可以使用适用于 SQL API 的 Azure Cosmos DB Java SDK 4.0 或更高版本来管理自动缩放资源。You can use version 4.0 or higher of the Azure Cosmos DB Java SDK for SQL API to manage autoscale resources.

重要

可以使用该 Java SDK 创建新的自动缩放资源。You can use the Java SDK to create new autoscale resources. 该 SDK 不支持在自动缩放与标准(手动)吞吐量之间迁移。The SDK does not support migrating between autoscale and standard (manual) throughput. 目前只有 Azure 门户支持迁移方案。The migration scenario is currently supported in only the Azure portal.

创建具有共享吞吐量的数据库Create database with shared throughput

// Create instance of CosmosClient
CosmosAsyncClient client = new CosmosClientBuilder()
    .setEndpoint(HOST)
    .setKey(MASTER)
    .setConnectionPolicy(CONNECTIONPOLICY)
    .buildAsyncClient();

// Autoscale throughput settings
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.createAutoscaledThroughput(4000); //Set autoscale max RU/s

//Create the database with autoscale enabled
CosmosAsyncDatabase database = client.createDatabase(databaseName, autoscaleThroughputProperties).block().getDatabase();

创建具有专用吞吐量的容器Create container with dedicated throughput

// Get reference to database that container will be created in
CosmosAsyncDatabase database = client.createDatabase("DatabaseName").block().getDatabase();

// Container and autoscale throughput settings
CosmosContainerProperties autoscaleContainerProperties = new CosmosContainerProperties("ContainerName", "/partitionKey");
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.createAutoscaledThroughput(4000); //Set autoscale max RU/s

// Create the container with autoscale enabled
CosmosAsyncContainer container = database.createContainer(autoscaleContainerProperties, autoscaleThroughputProperties, new CosmosContainerRequestOptions())
                                .block()
                                .getContainer();

读取当前吞吐量(RU/秒)Read the current throughput (RU/s)

// Get a reference to the resource
CosmosAsyncContainer container = client.getDatabase("DatabaseName").getContainer("ContainerName");

// Read the throughput on a resource
ThroughputProperties autoscaleContainerThroughput = container.readThroughput().block().getProperties();

// The autoscale max throughput (RU/s) of the resource
int autoscaleMaxThroughput = autoscaleContainerThroughput.getAutoscaleMaxThroughput();

// The throughput (RU/s) the resource is currently scaled to
int currentThroughput = autoscaleContainerThroughput.Throughput;

更改自动缩放最大吞吐量(RU/秒)Change the autoscale max throughput (RU/s)

// Change the autoscale max throughput (RU/s)
container.replaceThroughput(ThroughputProperties.createAutoscaledThroughput(newAutoscaleMaxThroughput)).block();

Cassandra APICassandra API

可以使用 CQL 命令Azure CLIAzure PowerShellAzure 资源管理器模板来预配用于 Cassandra API 的 Azure Cosmos DB 帐户,以进行自动缩放。Azure Cosmos DB accounts for Cassandra API can be provisioned for autoscale using CQL commands, Azure CLI, Azure PowerShell or Azure Resource Manager templates.

用于 MongoDB 的 Azure Cosmos DB APIAzure Cosmos DB API for MongoDB

可以使用 MongoDB 扩展命令Azure CLIAzure PowerShellAzure 资源管理器模板来配置用于 MongoDB API 的 Azure Cosmos DB 帐户,以进行自动缩放。Azure Cosmos DB accounts for MongoDB API can be provisioned for autoscale using MongoDB extension commands, Azure CLI, Azure PowerShell or Azure Resource Manager templates.

Azure 资源管理器Azure Resource Manager

Azure 资源管理器模板可用于在数据库或容器级资源上为所有 Azure Cosmos DB API 预配自动缩放吞吐量。Azure Resource Manager templates can be used to provision autoscale throughput on database or container-level resources for all Azure Cosmos DB APIs. 有关示例,请参阅 Azure Cosmos DB 的 Azure 资源管理器模板See Azure Resource Manager templates for Azure Cosmos DB for samples.

Azure CLIAzure CLI

Azure CLI 可用于在数据库或容器级资源上为所有 Azure Cosmos DB API 预配自动缩放吞吐量。Azure CLI can be used to provision autoscale throughput on a database or container-level resources for all Azure Cosmos DB APIs. 有关示例,请参阅用于 Azure Cosmos DB 的 Azure CLI 示例For samples see Azure CLI Samples for Azure Cosmos DB.

Azure PowerShellAzure PowerShell

Azure PowerShell 可用于在数据库或容器级资源上为所有 Azure Cosmos DB API 预配自动缩放吞吐量。Azure PowerShell can be used to provision autoscale throughput on a database or container-level resources for all Azure Cosmos DB APIs. 有关示例,请参阅适用于 Azure Cosmos DB 的 Azure PowerShell 示例For samples see Azure PowerShell samples for Azure Cosmos DB.

后续步骤Next steps