عمليات معدل النقل (RU/s) باستخدام Azure CLI لقاعدة بيانات أو رسم بياني ل Azure Cosmos DB ل MongoDB

ينطبق على: MongoDB

يقوم البرنامج النصي في هذه المقالة بإنشاء قاعدة بيانات MongoDB ذات معدل نقل مشترك وتجميع مع معدل نقل مخصص، ثم يقوم بتحديث معدل النقل لكليهما. ثم ينتقل البرنامج النصي من القياسي إلى معدل النقل التلقائي ثم يقرأ قيمة معدل النقل التلقائي بعد ترحيله.

إذا لم يكن لديك اشتراك في Azure، فأنشئ حساب Azure مجاني قبل أن تبدأ.

المتطلبات الأساسية

  • تتطلب هذه المقالة الإصدار 2.30 أو إصدار أحدث. قم بتشغيل az --version للعثور على الإصدار. إذا كنت بحاجة إلى التثبيت أو الترقية، فراجع تثبيت Azure CLI. إذا كنت تستخدم Azure Cloud Shell، يتم تثبيت أحدث إصدار بالفعل.

نموذج البرنامج النصي

إطلاق Azure Cloud Shell

Azure Cloud Shell هو shell تفاعلية مجانية التي يمكنك استخدامها لتشغيل الخطوات في هذه المقالة. يحتوي على أدوات Azure الشائعة المثبتة مسبقًا والمهيئة للاستخدام مع حسابك.

لفتح Cloud Shell، ما عليك سوى تحديد جربه من الزاوية اليمنى العليا من مجموعة التعليمات البرمجية. يمكنك أيضًا تشغيل Cloud Shell في علامة تبويب مستعرض منفصلة بالانتقال إلى https://shell.azure.com.

عند فتح Cloud Shell، تحقق من تحديد Bash لبيئتك. ستستخدم الجلسات اللاحقة Azure CLI في بيئة Bash، حدد نسخ لنسخ كتل التعليمات البرمجية، وألصقها في Cloud Shell، واضغط على Enter لتشغيلها.

تسجيل الدخول إلى Azure

يُصادق Cloud Shell تلقائياً بموجب الحساب الأولي الذي سُجل الدخول به. استخدم البرنامج النصي التالي لتسجيل الدخول باستخدام اشتراك مختلف، واستبدل <Subscription ID> بمعرّف اشتراك Azure الخاص بك. إذا لم يكن لديك اشتراك في Azure، فأنشئ حساب Azure مجاني قبل أن تبدأ.

subscription="<subscriptionId>" # add subscription here

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

لمزيد من المعلومات، راجع تعيين اشتراك نشط أو تسجيل الدخول بشكل تفاعلي

تشغيل البرنامج النصي

# 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

تنظيف الموارد

استخدم الأمر التالي لإزالة مجموعة الموارد وجميع الموارد المقترنة بها باستخدام الأمر az group delete - إلا إذا وُجدت حاجة مستمرة لهذه الموارد. قد يستغرق إنشاء بعض هذه الموارد بعض الوقت، وكذلك حذفها.

az group delete --name $resourceGroup

نموذج مرجع

يستخدم هذا البرنامج النصي الأوامر التالية. يرتبط كل أمر في الجدول بأمر وثائق معينة.

الأمر الملاحظات
az group create إنشاء مجموعة موارد يتم تخزين كل الموارد فيها.
إنشاء az cosmosdb ينشئ حساب Azure Cosmos DB.
az cosmosdb mongodb database create إنشاء قاعدة بيانات Azure Cosmos DB MongoDB API.
az cosmosdb mongodb collection create إنشاء مجموعة Azure Cosmos DB MongoDB API.
az cosmosdb mongodb database throughput update تحديث وحدات الطلب لقاعدة بيانات Azure Cosmos DB MongoDB API.
az cosmosdb mongodb collection throughput update تحديث وحدات الطلب لمجموعة Azure Cosmos DB MongoDB API.
az cosmosdb mongodb database throughput migrate ترحيل سرعة النقل لقاعدة بيانات.
az cosmosdb mongodb collection throughput migrate ترحيل سرعة النقل لمجموعة.
حذف مجموعة az يحذف مجموعة الموارد بما في ذلك جميع الموارد المتداخلة.

الخطوات التالية

لمزيد من المعلومات حول Azure Cosmos DB CLI، راجع وثائق Azure Cosmos DB CLI.