在 Azure Cosmos DB - API for NoSQL 中的資料庫或容器上佈建自動調整輸送量

適用於:NoSQL

本文說明如何在 Azure Cosmos DB for NoSQL 中的資料庫或容器 (集合、圖表或資料表) 上佈建自動調整輸送量。 您可在單一容器上啟用自動調整,或在資料庫上佈建自動調整輸送量,並在資料庫中的所有容器之間共用。

如果您使用不同的 API,請參閱 API for MongoDBAPI for CassandraAPI for Gremlin 文章來佈建輸送量。

Azure 入口網站

建立可自動調整的新資料庫或容器

  1. 登入 Azure 入口網站Azure Cosmos DB 總管

  2. 巡覽至 Azure Cosmos DB 帳戶並開啟 [資料總管] 索引標籤。

  3. 選取 [新增容器]。輸入資料庫的名稱、容器和分割區索引鍵。 在資料庫或容器輸送量下,選取 [自動調整] 選項,然後設定資料庫或容器可調整的最大輸送量 (RU/秒)

    Creating a container and configuring autoscale provisioned throughput

  4. 選取 [確定]。

若要在共用輸送量資料庫上佈建自動調整,請在建立新的資料庫時,選取 [佈建資料庫輸送量] 選項。

在現有的資料庫或容器上啟用自動調整

  1. 登入 Azure 入口網站Azure Cosmos DB 總管

  2. 巡覽至 Azure Cosmos DB 帳戶並開啟 [資料總管] 索引標籤。

  3. 針對容器選取 [調整與設定],或針對資料庫選取 [調整]

  4. 在 [調整] 下,依序選取 [自動調整] 選項和 [儲存]

    Enabling autoscale on an existing container

注意

當在現有的資料庫或容器上啟用自動調整時,最大 RU/秒的起始值是由系統根據您目前手動佈建輸送量設定和儲存體來決定。 作業完成之後,即可視需要變更最大 RU/秒。 深入了解

Azure Cosmos DB .NET V3 SDK

使用 Azure Cosmos DB .NET SDK for API for NoSQL 3.9 版或更高版本來管理自動調整資源。

重要

您可使用此 .NET SDK 來建立新的自動調整資源。 此 SDK 不支援在自動調整與標準 (手動) 輸送量之間進行移轉。 目前只有 Azure 入口網站CLIPowerShell 中支援移轉案例。

建立具有共用輸送量的資料庫

// 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);

建立具有專用輸送量的容器

// 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);

讀取目前的輸送量 (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;

變更自動調整的最大輸送量 (RU/秒)

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

Azure Cosmos DB Java V4 SDK

您可以使用 Azure Cosmos DB Java SDK for API for NoSQL 4.0 版或更高版本來管理自動調整資源。

重要

您可使用此 Java SDK 來建立新的自動調整資源。 此 SDK 不支援在自動調整與標準 (手動) 輸送量之間進行移轉。 目前只有 Azure 入口網站CLIPowerShell 中支援移轉案例。

建立具有共用輸送量的資料庫

// 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();

建立具有專用輸送量的容器

// 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();

讀取目前的輸送量 (RU/秒)

// 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)
container.replaceThroughput(ThroughputProperties.createAutoscaledThroughput(newAutoscaleMaxThroughput)).block();

Azure Resource Manager

您可以使用 Azure Resource Manager 範本,在新資料庫或容器層級資源上為所有 Azure Cosmos DB API 佈建自動調整輸送量。 如需範例,請參閱適用於 Azure Cosmos DB 的 Azure Resource Manager 範本。 根據設計,Azure Resource Manager 範本不能用來在現有資源上的已佈建和自動調整輸送量之間進行移轉。

Azure CLI

Azure CLI 可以用來在新的資料庫或容器層級的資源上為所有 Azure Cosmos DB API 佈建自動調整輸送量,或在現有資源上啟用自動調整。 如需範例,請參閱適用於 Azure Cosmos DB 的 Azure CLI 範例

Azure PowerShell

Azure PowerShell 可以用來在新的資料庫或容器層級的資源上為所有 Azure Cosmos DB API 佈建自動調整輸送量,或在現有資源上啟用自動調整。 如需範例,請參閱適用於 Azure Cosmos DB 的 Azure PowerShell 範例

下一步