Share via


Doorvoerbewerkingen (RU/s) met Azure CLI voor een database of grafiek voor Azure Cosmos DB voor MongoDB

VAN TOEPASSING OP: MongoDB

Met het script in dit artikel maakt u een MongoDB-database met gedeelde doorvoer en verzameling met toegewezen doorvoer, waarna de doorvoer voor beide wordt bijgewerkt. Met het script wordt de migratie van standaarddoorvoer naar doorvoer met automatische schaalaanpassing uitgevoerd en de waarde van doorvoer met automatische schaalaanpassing gelezen nadat de migratie is voltooid.

Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.

Vereisten

  • Voor dit artikel is versie 2.30 of hoger vereist. Voer az --version uit om de versie te bekijken. Als u Azure CLI 2.0 wilt installeren of upgraden, raadpleegt u Azure CLI 2.0 installeren. Als u Azure Cloud Shell gebruikt, is de nieuwste versie al geïnstalleerd.

Voorbeeldscript

Azure Cloud Shell starten

Azure Cloud Shell is een gratis interactieve shell waarmee u de stappen in dit artikel kunt uitvoeren. In deze shell zijn algemene Azure-hulpprogramma's vooraf geïnstalleerd en geconfigureerd voor gebruik met uw account.

Als u Cloud Shell wilt openen, selecteert u Proberen in de rechterbovenhoek van een codeblok. U kunt Cloud Shell ook openen in een afzonderlijk browsertabblad door naar https://shell.azure.com te gaan.

Wanneer Cloud Shell wordt geopend, controleert u of Bash is geselecteerd voor uw omgeving. Volgende sessies gebruiken Azure CLI in een Bash-omgeving, selecteer Kopiëren om de codeblokken te kopiëren, plak deze in Cloud Shell en druk op Enter om deze uit te voeren.

Aanmelden bij Azure

Cloud Shell wordt automatisch geverifieerd onder het eerste account waarmee is aangemeld. Gebruik het volgende script om u aan te melden met een ander abonnement, waarbij u <Subscription ID> uw Azure-abonnements-id vervangt. Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.

subscription="<subscriptionId>" # add subscription here

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

Zie Voor meer informatie het instellen van een actief abonnement of het interactief aanmelden

Het script uitvoeren

# 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

Resources opschonen

Gebruik de volgende opdracht om de resourcegroep en alle bijbehorende resources te verwijderen met behulp van de opdracht az group delete - tenzij u deze resources voortdurend nodig hebt. Het kan even duren voordat sommige van deze resources zijn gemaakt en dat deze kunnen worden verwijderd.

az group delete --name $resourceGroup

Voorbeeldverwijzing

In dit script worden de volgende opdrachten gebruikt. Elke opdracht in de tabel is een koppeling naar specifieke documentatie over de opdracht.

Command Aantekeningen
az group create Hiermee wordt een resourcegroep gemaakt waarin alle resources worden opgeslagen.
az cosmosdb create Hiermee wordt een Azure Cosmos DB-account gemaakt.
az cosmosdb mongodb database create Hiermee maakt u een Azure Cosmos DB MongoDB API-database.
az cosmosdb mongodb collection create Hiermee maakt u een Azure Cosmos DB MongoDB API-verzameling.
az cosmosdb mongodb database throughput update RU's bijwerken voor een Azure Cosmos DB MongoDB API-database.
az cosmosdb mongodb collection throughput update RU's bijwerken voor een MongoDB-API-verzameling van Azure Cosmos DB.
az cosmosdb mongodb database throughput migrate Migratie doorvoer voor een database.
az cosmosdb mongodb collection throughput migrate Migratie doorvoer voor een verzameling.
az group delete Hiermee verwijdert u een resourcegroep met inbegrip van alle ingesloten resources.

Volgende stappen

Raadpleeg de documentatie van Azure Cosmos DB CLI voor meer informatie over de Azure Cosmos DB CLI.