Partager via


Opérations de débit (RU/s) avec Azure CLI sur une base de données ou un graphe d’Azure Cosmos DB pour MongoDB

S’APPLIQUE À : MongoDB

Le script dans cet article crée une base de données MongoDB avec un débit partagé et une collection avec un débit dédié, puis met à jour le débit pour les deux ressources. Le script migre ensuite du débit standard à un débit avec mise à l’échelle automatique, puis lit la valeur de ce débit après sa migration.

Si vous n’avez pas d’abonnement Azure, créez un compte gratuit Azure avant de commencer.

Prérequis

  • Cet article nécessite la version 2.30 ou une version ultérieure. Exécutez az --version pour trouver la version. Si vous devez installer ou mettre à niveau, voir Installer Azure CLI. Si vous utilisez Azure Cloud Shell, la version la plus récente est déjà installée.

Exemple de script

Lancement d’Azure Cloud Shell

Azure Cloud Shell est un interpréteur de commandes interactif et gratuit que vous pouvez utiliser pour exécuter les étapes de cet article. Il contient des outils Azure courants préinstallés et configurés pour être utilisés avec votre compte.

Pour ouvrir Cloud Shell, sélectionnez simplement Essayer en haut à droite d’un bloc de code. Vous pouvez aussi lancer Cloud Shell dans un onglet distinct du navigateur en accédant à https://shell.azure.com.

Quand Cloud Shell s’ouvre, vérifiez que Bash est sélectionné pour votre environnement. Les sessions ultérieures utiliseront Azure CLI dans un environnement Bash. Sélectionnez Copier pour copier les blocs de code, collez-les dans Cloud Shell, puis appuyez sur Entrée pour les exécuter.

Connexion à Azure

Cloud Shell est automatiquement authentifié sous le compte initial utilisé pour la connexion. Utilisez le script suivant pour vous connecter avec un autre abonnement, en remplaçant <Subscription ID> par votre ID d’abonnement Azure. Si vous n’avez pas d’abonnement Azure, créez un compte gratuit Azure avant de commencer.

subscription="<subscriptionId>" # add subscription here

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

Pour plus d’informations, consultez Définir l’abonnement actif ou Se connecter de manière interactive

Exécuter le 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

Nettoyer les ressources

Utilisez la commande suivante pour supprimer le groupe de ressources et toutes les ressources associées à celui-ci à l’aide de la commande az group delete, sauf si vous avez toujours besoin de ces ressources. La création, ainsi que la suppression, de certaines de ces ressources peut prendre du temps.

az group delete --name $resourceGroup

Informations de référence sur l’exemple

Ce script utilise les commandes suivantes. Chaque commande du tableau renvoie à une documentation spécifique.

Commande Notes
az group create Crée un groupe de ressources dans lequel toutes les ressources sont stockées.
az cosmosdb create Crée un compte Azure Cosmos DB.
az cosmosdb mongodb database create Crée une base de données d’API MongoDB Azure Cosmos DB.
az cosmosdb mongodb collection create Crée une collection d’API MongoDB Azure Cosmos DB.
az cosmosdb mongodb database throughput update Met à jour les unités de requête/s pour une base de données d’API MongoDB Azure Cosmos DB.
az cosmosdb mongodb collection throughput update Met à jour les unités de requête/s pour une collection d’API MongoDB Azure Cosmos DB.
az cosmosdb mongodb database throughput migrate Migrez le débit d’une base de données.
az cosmosdb mongodb collection throughput migrate Migrez le débit d’une collection.
az group delete Supprime un groupe de ressources, y compris toutes les ressources imbriquées.

Étapes suivantes

Pour plus d’informations sur l’interface CLI Azure Cosmos DB, consultez la documentation sur l’interface CLI Azure Cosmos DB.