Share via


Operaciones de rendimiento (RU/s) con la CLI de Azure para una base de datos o un grafo de Azure Cosmos DB for MongoDB

SE APLICA A: MongoDB

El script de este artículo crea una base de datos de MongoDB con rendimiento compartido y una colección con rendimiento dedicado y, luego, actualiza el rendimiento de ambas. A continuación, el script migra del rendimiento estándar a escalado automático y, a continuación, lee el valor del rendimiento de escalado automático una vez que se ha migrado.

Si no tiene una suscripción a Azure, cree una cuenta gratuita de Azure antes de empezar.

Requisitos previos

  • En este artículo se necesita la versión 2.30 o posterior. Ejecute az --version para encontrar la versión. Si necesita instalarla o actualizarla, vea Instalación de la CLI de Azure. Si usa Azure Cloud Shell, ya está instalada la versión más reciente.

Script de ejemplo

Inicio de Azure Cloud Shell

Azure Cloud Shell es un shell interactivo gratuito que puede usar para ejecutar los pasos de este artículo. Tiene las herramientas comunes de Azure preinstaladas y configuradas para usarlas en la cuenta.

Para abrir Cloud Shell, seleccione Pruébelo en la esquina superior derecha de un bloque de código. También puede ir a https://shell.azure.com para iniciar Cloud Shell en una pestaña independiente del explorador.

Cuando se abra Cloud Shell, compruebe que Bash está seleccionado para el entorno. En las sesiones siguientes se usará la CLI de Azure en un entorno de Bash, seleccione Copiar para copiar los bloques de código, péguelos en Cloud Shell y, luego, presione Entrar para ejecutarlos.

Inicio de sesión en Azure

Cloud Shell se autentica de forma automática en la cuenta inicial con la que ha iniciado sesión. Use el script siguiente para iniciar sesión con otra suscripción, y reemplace <Subscription ID> con el id. de la suscripción de Azure. Si no tiene una suscripción a Azure, cree una cuenta gratuita de Azure antes de empezar.

subscription="<subscriptionId>" # add subscription here

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

Para más información, vea Establecimiento de la suscripción activa o Inicio de sesión de forma interactiva

Ejecute el 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

Limpieza de recursos

Use el comando siguiente para quitar el grupo de recursos y todos los recursos asociados con él mediante el comando az group delete, a menos que tenga una necesidad constante de estos recursos. Algunos de estos recursos pueden tardar un tiempo en crearse, así como en eliminarse.

az group delete --name $resourceGroup

Referencia de ejemplo

Este script usa los siguientes comandos. Cada comando de la tabla crea un vínculo a documentación específica del comando.

Get-Help Notas
az group create Crea un grupo de recursos en el que se almacenan todos los recursos.
az cosmosdb create Crea una cuenta de Azure Cosmos DB.
az cosmosdb mongodb database create Crea una base de datos de la API para MongoDB de Azure Cosmos DB.
az cosmosdb mongodb collection create Crea una colección de la API para MongoDB de Azure Cosmos DB.
az cosmosdb mongodb database throughput update Actualiza las unidades de solicitud (RU) de una base de datos de la API para MongoDB de Azure Cosmos DB.
az cosmosdb mongodb collection throughput update Actualiza las unidades de solicitud (RU) de una colección de la API para MongoDB de Azure Cosmos DB.
az cosmosdb mongodb database throughput migrate Migra el rendimiento de una base de datos.
az cosmosdb mongodb collection throughput migrate Migra el rendimiento de una colección.
az group delete Elimina un grupo de recursos, incluidos todos los recursos anidados.

Pasos siguientes

Para más información, consulte la documentación de la CLI de Azure Cosmos DB.