Share via


Operazioni di velocità effettiva (UR/s) con l'interfaccia della riga di comando di Azure per un database o un grafo per Azure Cosmos DB for MongoDB

SI APPLICA A: MongoDB

Lo script in questo articolo crea un database MongoDB con velocità effettiva condivisa e una raccolta con velocità effettiva dedicata, quindi aggiorna la velocità effettiva di entrambi. Lo script esegue quindi la migrazione dalla velocità effettiva standard alla velocità effettiva con scalabilità automatica e, al termine della migrazione, legge il valore della velocità effettiva con scalabilità automatica.

Se non si ha una sottoscrizione di Azure, creare un account Azure gratuito prima di iniziare.

Prerequisiti

  • Questo articolo richiede la versione 2.30 o successiva. Eseguire az --version per trovare la versione. Se è necessario eseguire l'installazione o l'aggiornamento, vedere Installare l'interfaccia della riga di comando di Azure. Se si usa Azure Cloud Shell, la versione più recente è già installata.

Script di esempio

Avviare Azure Cloud Shell

Azure Cloud Shell è una shell interattiva gratuita che può essere usata per eseguire la procedura di questo articolo. Include strumenti comuni di Azure preinstallati e configurati per l'uso con l'account.

Per aprire Cloud Shell, basta selezionare Prova nell'angolo superiore destro di un blocco di codice. È anche possibile avviare Cloud Shell in una scheda separata del browser visitando https://shell.azure.com.

Quando si apre Cloud Shell, verificare che sia selezionato Bash per l'ambiente. Le sessioni successive useranno l'interfaccia della riga di comando di Azure in un ambiente Bash. Selezionare Copia per copiare i blocchi di codice, incollarli in Cloud Shell e premere Invio per eseguirli.

Accedere ad Azure

Cloud Shell viene autenticato automaticamente con l'account iniziale con cui è stato eseguito l'accesso. Usare lo script seguente per accedere usando una sottoscrizione diversa, sostituendo <Subscription ID> con l'ID sottoscrizione di Azure. Se non si ha una sottoscrizione di Azure, creare un account Azure gratuito prima di iniziare.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

Per altre informazioni, vedere Impostare la sottoscrizione attiva oppure Accedere in modo interattivo

Eseguire lo script

# Throughput operations for a MongoDB API database and collection

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-cosmosdb-rg-$randomIdentifier"
tag="throughput-mongodb-cosmosdb"
account="msdocs-account-cosmos-$randomIdentifier" #needs to be lower case
database="msdocs-db-mongo-cosmos"
collection="collection1"
originalThroughput=400
updateThroughput=500

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create a Cosmos account for MongoDB API
echo "Creating $account"
az cosmosdb create --name $account --resource-group $resourceGroup --kind MongoDB

# Create a MongoDB API database
echo "Creating $database with $originalThroughput"
az cosmosdb mongodb database create --account-name $account --resource-group $resourceGroup --name $database --throughput $originalThroughput

# Define a minimal index policy for the collection
printf '[ {"key": {"keys": ["_id"]}} ]' > idxpolicy-$randomIdentifier.json

# Create a MongoDB API collection
az cosmosdb mongodb collection create --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --shard "user_id" --throughput $originalThroughput --idx @idxpolicy-$randomIdentifier.json

# Clean up temporary index policy file
rm -f "idxpolicy-$randomIdentifier.json"

# Throughput operations for MongoDB API database
#   Read the current throughput
#   Read the minimum throughput
#   Make sure the updated throughput is not less than the minimum
#   Update the throughput
#   Migrate between standard (manual) and autoscale throughput
#   Read the autoscale max throughput

# Retrieve the current provisioned database throughput
az cosmosdb mongodb database throughput show --resource-group $resourceGroup --account-name $account --name $database --query resource.throughput -o tsv

# Retrieve the minimum allowable database throughput
minimumThroughput=$(az cosmosdb mongodb database throughput show --resource-group $resourceGroup --account-name $account --name $database --query resource.minimumThroughput -o tsv)
echo $minimumThroughput

# Make sure the updated throughput is not less than the minimum allowed throughput
if [ $updateThroughput -lt $minimumThroughput ]; then
    updateThroughput=$minimumThroughput
fi

# Update database throughput
echo "Updating $database throughput to $updateThroughput"
az cosmosdb mongodb database throughput update --account-name $account --resource-group $resourceGroup --name $database --throughput $updateThroughput

# Migrate the database from standard (manual) throughput to autoscale throughput
az cosmosdb mongodb database throughput migrate --account-name $account --resource-group $resourceGroup --name $database --throughput-type 'autoscale'

# Retrieve current autoscale provisioned max database throughput
az cosmosdb mongodb database throughput show --account-name $account --resource-group $resourceGroup --name $database --query resource.autoscaleSettings.maxThroughput -o tsv

# Throughput operations for MongoDB API collection
#   Read the current throughput
#   Read the minimum throughput
#   Make sure the updated throughput is not less than the minimum
#   Update the throughput
#   Migrate between standard (manual) and autoscale throughput
#   Read the autoscale max throughput

# Retrieve the current provisioned collection throughput
az cosmosdb mongodb collection throughput show --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --query resource.throughput -o tsv

# Retrieve the minimum allowable collection throughput
minimumThroughput=$(az cosmosdb mongodb collection throughput show --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --query resource.minimumThroughput -o tsv)

echo $minimumThroughput

# Make sure the updated throughput is not less than the minimum allowed throughput
if [ $updateThroughput -lt $minimumThroughput ]; then
    updateThroughput=$minimumThroughput
fi

# Update collection throughput
echo "Updating collection throughput to $updateThroughput"
az cosmosdb mongodb collection throughput update --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --throughput $updateThroughput

# Migrate the collection from standard (manual) throughput to autoscale throughput
az cosmosdb mongodb collection throughput migrate --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --throughput 'autoscale'

# Retrieve the current autoscale provisioned max collection throughput
az cosmosdb mongodb collection throughput show --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --query resource.autoscaleSettings.maxThroughput -o tsv

Pulire le risorse

Usare il comando seguente per rimuovere il gruppo di risorse e tutte le risorse associate usando il comando az group delete, a meno che queste risorse non siano ancora necessarie. La creazione e l'eliminazione di alcune di queste risorse può richiedere tempo.

az group delete --name $resourceGroup

Informazioni di riferimento per l'esempio

Questo script usa i comandi seguenti. Ogni comando della tabella include collegamenti alla documentazione specifica del comando.

Comando Note
az group create Consente di creare un gruppo di risorse in cui sono archiviate tutte le risorse.
az cosmosdb create Crea un account Azure Cosmos DB.
az cosmosdb mongodb database create Crea un database dell'API per MongoDB di Azure Cosmos DB.
az cosmosdb mongodb collection create Crea una raccolta dell'API per MongoDB di Azure Cosmos DB.
az cosmosdb mongodb database throughput update Aggiorna il valore di RU/s per un database dell'API MongoDB di Azure Cosmos DB.
az cosmosdb mongodb collection throughput update Aggiorna il valore di RU/s per una raccolta dell'API MongoDB di Azure Cosmos DB.
az cosmosdb mongodb database throughput migrate Esegue la migrazione della velocità effettiva per un database.
az cosmosdb mongodb collection throughput migrate Esegue la migrazione della velocità effettiva per una raccolta.
az group delete Consente di eliminare un gruppo di risorse incluse tutte le risorse annidate.

Passaggi successivi

Per altre informazioni sull'interfaccia della riga di comando di Azure Cosmos DB, vedere la relativa documentazione.