Udostępnij za pośrednictwem


Konwertowanie każdego zasobu usługi Azure Cosmos DB ze standardowej na przepływność autoskalowania

DOTYCZY: NoSQL MongoDB Kasandra Gremlin Stół

Skrypt w tym artykule pokazuje, jak przekonwertować każdy zasób przy użyciu standardowej aprowizowanej przepływności na skalowanie automatyczne w ramach subskrypcji.

Wielu klientów rozpoczyna się od standardowej aprowizowanej przepływności podczas tworzenia nowych aplikacji. Jednak standardowa przepływność jest najlepiej używana w obciążeniach, które mają stałe wymagania dotyczące przepływności. Większość obciążeń jest zmienna. Oznacza to, że skalowanie automatyczne jest często tańsze w użyciu. W scenariuszach, w których migracja może zawierać dziesiątki lub setki zasobów, może to być żmudne i czasochłonne. Ten skrypt jest przeznaczony do migrowania wszystkich zasobów w jednym kroku.

Jeśli nie masz subskrypcji platformy Azure, przed rozpoczęciem utwórz bezpłatne konto platformy Azure.

Wymagania wstępne

  • Ten artykuł wymaga wersji 2.9.1 lub nowszej interfejsu wiersza polecenia platformy Azure. W przypadku korzystania z usługi Azure Cloud Shell najnowsza wersja jest już zainstalowana.

Przykładowy skrypt

Uruchamianie usługi Azure Cloud Shell

Usługa Azure Cloud Shell to bezpłatna interaktywna powłoka, której możesz używać do wykonywania kroków opisanych w tym artykule. Udostępnia ona wstępnie zainstalowane i najczęściej używane narzędzia platformy Azure, które są skonfigurowane do użycia na koncie.

Aby otworzyć usługę Cloud Shell, wybierz pozycję Wypróbuj w prawym górnym rogu bloku kodu. Możesz również uruchomić usługę Cloud Shell w oddzielnej karcie przeglądarki, przechodząc do strony https://shell.azure.com.

Po otwarciu usługi Cloud Shell sprawdź, czy dla danego środowiska wybrano powłokę Bash . Kolejne sesje będą używać interfejsu wiersza polecenia platformy Azure w środowisku powłoki Bash, wybierz pozycję Kopiuj , aby skopiować bloki kodu, wkleić go do usługi Cloud Shell i nacisnąć Enter , aby go uruchomić.

Logowanie się do platformy Azure

Usługa Cloud Shell jest automatycznie uwierzytelniana na początkowym koncie zalogowanym. Użyj następującego skryptu, aby zalogować się przy użyciu innej subskrypcji, zastępując <Subscription ID> element identyfikatorem subskrypcji platformy Azure. Jeśli nie masz subskrypcji platformy Azure, przed rozpoczęciem utwórz bezpłatne konto platformy Azure.

subscription="<subscriptionId>" # add subscription here

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

Aby uzyskać więcej informacji, zobacz set active subscription or log in interactively (Ustawianie aktywnej subskrypcji lub logowanie się interaktywnie)

Uruchamianie skryptu

#!/bin/bash

# Passed validation in Cloud Shell on 7/25/2024

# <FullScript>
# Azure Cosmos DB users can migrate from standard provisioned to autoscale
# throughput and back again. Most users can benefit from autoscale throughput
# to save on costs and avoid over-provisioning. This script migrates all
# provisioned throughput to autoscale throughput for all Cosmos DB accounts
# in the current subscription for NoSQL, MongoDB, Cassandra, Gremlin, and Table
# database and container level resources in the accounts.


# These can remain commented out if running in Azure Cloud Shell
#az login
#az account set -s {your subscription id}

throughtput=0

# Get the list of resource groups in the current subscription
resourceGroups=$(az group list --query "[].name" -o tsv)

# Loop through every resource group in the subscription
for resourceGroup in $resourceGroups; do
    echo "Processing resource group: $resourceGroup"

    # Get the list of Cosmos DB accounts in this resource group
    accounts=$(az cosmosdb list -g $resourceGroup --query "[].name" -o tsv)

    # Loop through every Cosmos DB account in the resource group
    for account in $accounts; do

        echo "Processing account: $account"

        # Get the list of SQL databases in this account
        databases=$(az cosmosdb sql database list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through SQL databases in the account
        for database in $databases; do
            throughput=$(az cosmosdb sql database throughput show -g $resourceGroup -a $account -n $database --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$database has throughput, attempting to migrate to autoscale"
                # Migrate the database to autoscale throughput
                az cosmosdb sql database throughput migrate -g $resourceGroup -a $account -n $database -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for database: $database in Cosmos DB account $account"
                fi
            else
                echo "$database does not have throughput"
            fi

            # Loop through SQL containers in the database
            containers=$(az cosmosdb sql container list -g $resourceGroup -a $account -d $database --query "[].name" -o tsv)

            for container in $containers; do
                throughput=$(az cosmosdb sql container throughput show -g $resourceGroup -a $account -d $database -n $container --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$container has throughput, attempting to migrate to autoscale"
                    # Migrate the container to autoscale throughput
                    az cosmosdb sql container throughput migrate -g $resourceGroup -a $account -d $database -n $container -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for container: $container in Cosmos DB account $account and database $database"
                    fi
                else
                    echo "$container does not have throughput"
                fi
            done
        done

        # Get the list of MongoDB databases in this account
        databases=$(az cosmosdb mongodb database list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through MongoDB databases in the account
        for database in $databases; do
            throughput=$(az cosmosdb mongodb database throughput show -g $resourceGroup -a $account -n $database --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$database has throughput, attempting to migrate to autoscale"
                # Migrate the database to autoscale throughput
                az cosmosdb mongodb database throughput migrate -g $resourceGroup -a $account -n $database -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for database: $database in Cosmos DB account $account"
                fi
            else
                echo "$database does not have throughput"
            fi

            # Loop through MongoDB collections in the database
            collections=$(az cosmosdb mongodb collection list -g $resourceGroup -a $account -d $database --query "[].name" -o tsv)

            for collection in $collections; do
                throughput=$(az cosmosdb mongodb collection throughput show -g $resourceGroup -a $account -d $database -n $collection --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$collection has throughput, attempting to migrate to autoscale"
                    # Migrate the collection to autoscale throughput
                    az cosmosdb mongodb collection throughput migrate -g $resourceGroup -a $account -d $database -n $collection -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for collection: $collection in Cosmos DB account $account and database $database"
                    fi
                else
                    echo "$collection does not have throughput"
                fi
            done
        done

        # Get the list of Cassandra keyspaces in this account
        keyspaces=$(az cosmosdb cassandra keyspace list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through Cassandra keyspaces in the account
        for keyspace in $keyspaces; do
            throughput=$(az cosmosdb cassandra keyspace throughput show -g $resourceGroup -a $account -n $keyspace --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$keyspace has throughput, attempting to migrate to autoscale"
                # Migrate the keyspace to autoscale throughput
                az cosmosdb cassandra keyspace throughput migrate -g $resourceGroup -a $account -n $keyspace -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for keyspace: $keyspace in Cosmos DB account $account"
                fi
            else
                echo "$keyspace does not have throughput"
            fi

            # Loop through Cassandra tables in the keyspace
            tables=$(az cosmosdb cassandra table list -g $resourceGroup -a $account -k $keyspace --query "[].name" -o tsv)

            for table in $tables; do
                throughput=$(az cosmosdb cassandra table throughput show -g $resourceGroup -a $account -k $keyspace -n $table --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$table has throughput, attempting to migrate to autoscale"
                    # Migrate the table to autoscale throughput
                    az cosmosdb cassandra table throughput migrate -g $resourceGroup -a $account -k $keyspace -n $table -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for table: $table in Cosmos DB account $account and keyspace $keyspace"
                    fi
                else
                    echo "$table does not have throughput"
                fi
            done
        done

        # Get the list of Gremlin databases in this account
        databases=$(az cosmosdb gremlin database list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through Gremlin databases in the account
        for database in $databases; do
            throughput=$(az cosmosdb gremlin database throughput show -g $resourceGroup -a $account -n $database --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$database has throughput, attempting to migrate to autoscale"
                # Migrate the database to autoscale throughput
                az cosmosdb gremlin database throughput migrate -g $resourceGroup -a $account -n $database -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for database: $database in Cosmos DB account $account"
                fi
            else
                echo "$database does not have throughput"
            fi

            # Loop through Gremlin graphs in the database
            graphs=$(az cosmosdb gremlin graph list -g $resourceGroup -a $account -d $database --query "[].name" -o tsv)

            for graph in $graphs; do
                throughput=$(az cosmosdb gremlin graph throughput show -g $resourceGroup -a $account -d $database -n $graph --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$graph has throughput, attempting to migrate to autoscale"
                    # Migrate the graph to autoscale throughput
                    az cosmosdb gremlin graph throughput migrate -g $resourceGroup -a $account -d $database -n $graph -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for graph: $graph in Cosmos DB account $account and database $database"
                    fi
                else
                    echo "$graph does not have throughput"
                fi
            done
        done

        # Get the list of Table databases in this account
        tables=$(az cosmosdb table list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through Table databases in the account
        for table in $tables; do
            throughput=$(az cosmosdb table throughput show -g $resourceGroup -a $account -n $table --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$table has throughput, attempting to migrate to autoscale"
                # Migrate the table to autoscale throughput
                az cosmosdb table throughput migrate -g $resourceGroup -a $account -n $table -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for table: $table in Cosmos DB account $account"
                fi
            else
                echo "$table does not have throughput"
            fi
        done
    done
done

echo "All Done! Enjoy your new autoscale throughput Cosmos DB accounts!"

# </FullScript>

Przykładowa dokumentacja

W tym skrypcie użyto następujących poleceń. Każde polecenie w tabeli stanowi link do dokumentacji polecenia.

Polecenie Uwagi
az group list Wyświetla listę wszystkich grup zasobów w subskrypcji platformy Azure.
az cosmosdb list Wyświetla listę wszystkich kont usługi Azure Cosmos DB w grupie zasobów.
az cosmosdb sql database list Wyświetla listę wszystkich baz danych NoSQL na koncie.
az cosmosdb sql database throughput show Przeczytaj wartość przepływności bazy danych NoSQL na koncie.
az cosmosdb sql database throughput migrate Migrowanie przepływności zasobu bazy danych NoSQL.
az cosmosdb sql container list Wyświetla listę wszystkich kontenerów NoSQL w bazie danych.
az cosmosdb sql container throughput show Przeczytaj wartość przepływności kontenera NoSQL na koncie.
az cosmosdb sql container throughput migrate Migrowanie przepływności dla kontenera NoSQL na koncie.
az cosmosdb mongodb database list Wyświetla listę wszystkich baz danych MongoDB na koncie.
az cosmosdb mongodb database throughput show Odczytaj wartość przepływności bazy danych MongoDB na koncie.
az cosmosdb mongodb database throughput migrate Migrowanie przepływności zasobu bazy danych na koncie bazy danych MongoDB.
az cosmosdb mongodb collection list Wyświetla listę wszystkich kolekcji bazy danych MongoDB.
az cosmosdb mongodb collection throughput show Przeczytaj wartość przepływności dla kolekcji bazy danych MongoDB na koncie.
az cosmosdb mongodb collection throughput migrate Migrowanie przepływności dla zasobu kolekcji na koncie bazy danych MongoDB.
az cosmosdb cassandra keyspace list Wyświetla listę wszystkich przestrzeni kluczy Cassandra na koncie.
az cosmosdb cassandra keyspace throughput show Przeczytaj wartość przepływności dla przestrzeni kluczy Cassandra na koncie.
az cosmosdb cassandra keyspace throughput migrate Migrowanie przepływności dla przestrzeni kluczy Cassandra na koncie.
az cosmosdb cassandra table list Wyświetla listę wszystkich tabel Cassandra w przestrzeni kluczy.
az cosmosdb cassandra table throughput show Przeczytaj wartość przepływności dla tabeli Cassandra na koncie.
az cosmosdb cassandra table throughput migrate Migrowanie przepływności dla tabeli cassandra na koncie.
az cosmosdb gremlin database list Wyświetla listę wszystkich baz danych języka Gremlin na koncie.
az cosmosdb gremlin database throughput show Przeczytaj wartość przepływności bazy danych Gremlin na koncie.
az cosmosdb gremlin database throughput migrate Migrowanie przepływności zasobu bazy danych Gremlin.
az cosmosdb gremlin container list Wyświetla listę wszystkich grafów języka Gremlin w bazie danych.
az cosmosdb gremlin container throughput show Przeczytaj wartość przepływności grafu Gremlin na koncie.
az cosmosdb gremlin graph throughput migrate Migrowanie przepływności dla grafu Gremlin na koncie.
az cosmosdb table list Wyświetla listę wszystkich tabel na koncie.
az cosmosdb table throughput show Odczytaj wartość przepływności dla tabeli na koncie.
az cosmosdb table throughput migrate Migrowanie przepływności dla tabeli na koncie.

Następne kroki

Aby uzyskać więcej informacji na temat interfejsu wiersza polecenia usługi Azure Cosmos DB, zobacz dokumentację interfejsu wiersza polecenia usługi Azure Cosmos DB.

Aby zapoznać się z przykładami interfejsu wiersza polecenia platformy Azure dla określonych interfejsów API, zobacz: