本文介绍如何在 Azure Cosmos DB for NoSQL 中的数据库或容器(集合、图形或表)上启用自动缩放吞吐量。 可以为单个容器启用自动缩放,也可以为某个数据库预配自动缩放吞吐量,然后在该数据库中的所有容器之间共享此吞吐量。
如果使用其他 API,请参阅 用于 MongoDB 的 API、 Cassandra API 或 Gremlin API。
Azure 门户
创建支持自动缩放的新数据库或容器
登录到 Azure 门户 或 Azure Cosmos DB 资源管理器。
导航到你的 Azure Cosmos DB 帐户,打开“数据资源管理器”选项卡。
选择“新建容器”。输入数据库、容器和分区键的名称。
在数据库或容器吞吐量下选择“自动缩放”选项,并设置希望数据库或容器缩放到的最大吞吐量(RU/秒)。
选择“确定”。
若要在共享吞吐量数据库上预配自动缩放,请在创建新数据库时选择“预配数据库吞吐量”选项。
注意
仅建议在数据库级别设置吞吐量,以便进行开发/测试,或者当共享吞吐量数据库中所有容器中的工作负荷统一时。 为了获得大型生产工作负荷的最佳性能,建议在容器级别而不是数据库级别设置专用吞吐量(自动缩放或手动)。
在现有的数据库或容器上启用自动缩放
登录到 Azure 门户 或 Azure Cosmos DB 资源管理器。
导航到你的 Azure Cosmos DB 帐户,打开“数据资源管理器”选项卡。
为你的容器选择“缩放和设置”,或者为你的数据库选择“缩放”。
在“缩放”下,依次选择“自动缩放”选项、“保存”。
注意
在现有数据库或容器上启用自动缩放时,最大 RU/秒的起始值由系统根据当前手动预配的吞吐量设置和存储确定。 在操作完成后,你可以根据需要更改最大 RU/秒。 若要了解详细信息,请参阅 有关自动缩放预配吞吐量的常见问题解答。
SDKs
使用以下 SDK 管理自动缩放资源:
创建具有共享吞吐量的数据库
注意
仅建议在数据库级别设置吞吐量,以便进行开发/测试,或者当共享吞吐量数据库中所有容器中的工作负荷统一时。 为了获得大型生产工作负荷的最佳性能,建议在容器级别而不是数据库级别设置专用吞吐量(自动缩放或手动)。
可以使用适用于 API for NoSQL 的 Azure Cosmos DB .NET SDK 3.9 或更高版本来管理自动缩放资源。
注意
在现有数据库或容器上启用自动缩放时,最大 RU/秒的起始值由系统根据当前手动预配的吞吐量设置和存储确定。 在操作完成后,你可以根据需要更改最大 RU/秒。 若要了解详细信息,请参阅 有关自动缩放预配吞吐量的常见问题解答。
// Create instance of CosmosClient
CosmosClient cosmosClient = new CosmosClient(Endpoint, PrimaryKey);
// Autoscale throughput settings
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.CreateAutoscaleThroughput(1000); //Set autoscale max RU/s
//Create the database with autoscale enabled
database = await cosmosClient.CreateDatabaseAsync(DatabaseName, throughputProperties: autoscaleThroughputProperties);
可以使用适用于 API for NoSQL 的 Azure Cosmos DB Java SDK 4.0 或更高版本来管理自动缩放资源。
注意
在现有数据库或容器上启用自动缩放时,最大 RU/秒的起始值由系统根据当前手动预配的吞吐量设置和存储确定。 在操作完成后,你可以根据需要更改最大 RU/秒。 若要了解详细信息,请参阅 有关自动缩放预配吞吐量的常见问题解答。
异步
// Create instance of CosmosClient
CosmosAsyncClient client = new CosmosClientBuilder()
.setEndpoint(HOST)
.setKey(PRIMARYKEY)
.setConnectionPolicy(CONNECTIONPOLICY)
.buildAsyncClient();
// Autoscale throughput settings
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.createAutoscaledThroughput(1000); //Set autoscale max RU/s
//Create the database with autoscale enabled
CosmosAsyncDatabase database = client.createDatabase(databaseName, autoscaleThroughputProperties).block().getDatabase();
Sync
// Create instance of CosmosClient
CosmosClient client = new CosmosClientBuilder()
.setEndpoint(HOST)
.setKey(PRIMARYKEY)
.setConnectionPolicy(CONNECTIONPOLICY)
.buildClient();
// Autoscale throughput settings
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.createAutoscaledThroughput(1000); //Set autoscale max RU/s
//Create the database with autoscale enabled
CosmosDatabase database = client.createDatabase(databaseName, autoscaleThroughputProperties).getDatabase();
注意
在现有数据库或容器上启用自动缩放时,最大 RU/秒的起始值由系统根据当前手动预配的吞吐量设置和存储确定。 在操作完成后,你可以根据需要更改最大 RU/秒。 若要了解详细信息,请参阅 有关自动缩放预配吞吐量的常见问题解答。
Sync
from azure.cosmos import CosmosClient, ThroughputProperties
# Create your CosmosClient instance
client = CosmosClient(host, credential)
# Autoscale throughput settings
throughput_properties = ThroughputProperties(auto_scale_max_throughput=5000) #Set autoscale max RU/s
#Create the database with autoscale enabled
client.create_database(id=database_id, offer_throughput=throughput_properties)
异步
from azure.cosmos import ThroughputProperties
from azure.cosmos.aio import CosmosClient
# Create your CosmosClient instance
async with CosmosClient(host, credential) as client:
# Autoscale throughput settings
throughput_properties = ThroughputProperties(auto_scale_max_throughput=5000) #Set autoscale max RU/s
#Create the database with autoscale enabled
await client.create_database(id=database_id, offer_throughput=throughput_properties)
可以对数据库和容器资源使用 ThroughputProperties 。
注意
在现有数据库或容器上启用自动缩放时,最大 RU/秒的起始值由系统根据当前手动预配的吞吐量设置和存储确定。 在操作完成后,你可以根据需要更改最大 RU/秒。 若要了解详细信息,请参阅 有关自动缩放预配吞吐量的常见问题解答。
// autoscale throughput properties
db_throughput := azcosmos.NewAutoscaleThroughputProperties(4000)
_, err = client.CreateDatabase(context.Background(), azcosmos.DatabaseProperties{
ID: "demo_db",
}, &azcosmos.CreateDatabaseOptions{
ThroughputProperties: &db_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(1000); //Set autoscale max RU/s
// Create the container with autoscale enabled
container = await database.CreateContainerAsync(autoscaleContainerProperties, autoscaleThroughputProperties);
异步
// 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(1000); //Set autoscale max RU/s
// Create the container with autoscale enabled
CosmosAsyncContainer container = database.createContainer(autoscaleContainerProperties, autoscaleThroughputProperties, new CosmosContainerRequestOptions())
.block()
.getContainer();
Sync
// Get reference to database that container will be created in
CosmosDatabase database = client.createDatabase("DatabaseName").getDatabase();
// Container and autoscale throughput settings
CosmosContainerProperties autoscaleContainerProperties = new CosmosContainerProperties("ContainerName", "/partitionKey");
ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.createAutoscaledThroughput(1000); //Set autoscale max RU/s
// Create the container with autoscale enabled
CosmosContainer container = database.createContainer(autoscaleContainerProperties, autoscaleThroughputProperties, new CosmosContainerRequestOptions())
.getContainer();
Sync
from azure.cosmos import CosmosClient, ThroughputProperties
# Create your CosmosClient instance
client = CosmosClient(host, credential)
# Get your DatabaseProxy object
database = client.get_database_client(database_id)
# Autoscale throughput settings
throughput_properties = ThroughputProperties(auto_scale_max_throughput=5000) #Set autoscale max RU/s
#Create the container with autoscale enabled
database.create_container(id=container_id, partition_key=partition_key, offer_throughput=throughput_properties)
异步
from azure.cosmos import ThroughputProperties
from azure.cosmos.aio import CosmosClient
# Create your CosmosClient instance
async with CosmosClient(host, credential) as client:
# Get your DatabaseProxy object
database = client.get_database_client(database_id)
# Autoscale throughput settings
throughput_properties = ThroughputProperties(auto_scale_max_throughput=5000) #Set autoscale max RU/s
#Create the container with autoscale enabled
await database.create_container(id=container_id, partition_key=partition_key, offer_throughput=throughput_properties)
pkDefinition := azcosmos.PartitionKeyDefinition{
Paths: []string{"/state"},
Kind: azcosmos.PartitionKeyKindHash,
}
// autoscale throughput properties
throughput := azcosmos.NewAutoscaleThroughputProperties(4000)
db.CreateContainer(context.Background(), azcosmos.ContainerProperties{
ID: "demo_container",
PartitionKeyDefinition: pkDefinition,
}, &azcosmos.CreateContainerOptions{
ThroughputProperties: &throughput,
})
读取当前吞吐量(RU/秒)
// 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;
异步
// 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;
Sync
// Get a reference to the resource
CosmosContainer container = client.getDatabase("DatabaseName").getContainer("ContainerName");
// Read the throughput on a resource
ThroughputProperties autoscaleContainerThroughput = container.readThroughput().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;
Sync
from azure.cosmos import CosmosClient, ThroughputProperties
# Create your CosmosClient instance
client = CosmosClient(host, credential)
# Get your DatabaseProxy object
database = client.get_database_client(database_id)
# Get your ContainerProxy object
container = database.get_container_client(container_id)
# Get your throughput settings
throughput = container.get_throughput()
# Get the autoscale max throughput (RU/s) of the resource
auto_scale_throughput = throughput.auto_scale_max_throughput
# Get the throughput (RU/s) the resource is currently scaled to
current_throughput = throughput.offer_throughput
异步
from azure.cosmos import ThroughputProperties
from azure.cosmos.aio import CosmosClient
# Create your CosmosClient instance
async with CosmosClient(host, credential) as client:
# Get your DatabaseProxy object
database = client.get_database_client(database_id)
# Get your ContainerProxy object
container = database.get_container_client(container_id)
# Get your throughput settings
throughput = await container.get_throughput()
# Get the autoscale max throughput (RU/s) of the resource
auto_scale_throughput = throughput.auto_scale_max_throughput
# Get the throughput (RU/s) the resource is currently scaled to
current_throughput = throughput.offer_throughput
更改自动缩放最大吞吐量(RU/秒)
// Change the autoscale max throughput (RU/s)
await container.ReplaceThroughputAsync(ThroughputProperties.CreateAutoscaleThroughput(newAutoscaleMaxThroughput));
异步
// Change the autoscale max throughput (RU/s)
container.replaceThroughput(ThroughputProperties.createAutoscaledThroughput(newAutoscaleMaxThroughput)).block();
Sync
// Change the autoscale max throughput (RU/s)
container.replaceThroughput(ThroughputProperties.createAutoscaledThroughput(newAutoscaleMaxThroughput));
Sync
from azure.cosmos import ThroughputProperties
# Change the autoscale max throughput (RU/s)
container.replace_throughput(ThroughputProperties(auto_scale_max_throughput=8000))
异步
from azure.cosmos import ThroughputProperties
# Change the autoscale max throughput (RU/s)
await container.replace_throughput(ThroughputProperties(auto_scale_max_throughput=8000))
Azure Resource Manager
Azure 资源管理器模板可用于在新数据库或容器级别的资源上为所有 Azure Cosmos DB API 预配自动缩放吞吐量。 有关示例,请参阅 Azure Cosmos DB 的 Azure 资源管理器模板。
根据设计,Azure 资源管理器模板不能用于在现有资源的预配吞吐量和自动缩放吞吐量之间进行迁移。
Azure CLI
Azure CLI 可用于为所有 Azure Cosmos DB API 在新数据库或容器级资源上预配自动缩放吞吐量,或对现有资源启用自动缩放。
Azure PowerShell
Azure PowerShell 可用于为所有 Azure Cosmos DB API 在新数据库或容器级资源上预配自动缩放吞吐量,或对现有资源启用自动缩放。
后续步骤