Aracılığıyla paylaş


Azure Resource Manager şablonlarını kullanarak MongoDB için Azure Cosmos DB kaynaklarını yönetme

ŞUNLAR IÇIN GEÇERLIDIR: MongoDB

Bu makalede, MongoDB, veritabanları ve koleksiyonlar için API için Azure Cosmos DB hesaplarınızı dağıtmaya ve yönetmeye yardımcı olmak üzere Azure Resource Manager şablonlarını kullanmayı öğreneceksiniz.

Bu makalede yalnızca MongoDB için Azure Cosmos DB API'sine örnekler verilmiştir. Diğer API türü hesaplarına örnek bulmak için bkz. Azure Resource Manager şablonlarını Azure Cosmos DB'nin Cassandra, Gremlin, SQL, Tablo makaleleri için API'siyle kullanma.

Önemli

  • Hesap adları, tümü küçük harfle olmak üzere 44 karakterle sınırlıdır.
  • Aktarım hızı değerlerini değiştirmek için şablonu güncelleştirilmiş RU/sn ile yeniden dağıtın.
  • Azure Cosmos DB hesabına konum eklediğinizde veya kaldırdığınızda, diğer özellikleri aynı anda değiştiremezsiniz. Bu işlemler ayrı olarak yapılmalıdır.

Aşağıdaki Azure Cosmos DB kaynaklarından herhangi birini oluşturmak için aşağıdaki örnek şablonu yeni bir json dosyasına kopyalayın. İsteğe bağlı olarak, aynı kaynağın farklı ad ve değerlerle birden çok örneğini dağıtırken kullanılacak bir parametre json dosyası oluşturabilirsiniz. Azure portalı, Azure CLI, Azure PowerShell ve GitHub gibi Azure Resource Manager şablonlarını dağıtmanın birçok yolu vardır.

Otomatik ölçeklendirme sağlanan aktarım hızıyla MongoDB için Azure Cosmos DB hesabı

Bu şablon, veritabanı düzeyinde otomatik ölçeklendirme aktarım hızını paylaşan iki koleksiyonla MongoDB (3.2, 3.6, 4.0 ve 4.2) api'si için bir Azure Cosmos DB hesabı oluşturur. Bu şablon, Azure Hızlı Başlangıç Şablonları Galerisi'nden tek tıklamayla dağıtım için de kullanılabilir.

Resource Manager şablonunu Azure'a dağıtma düğmesi.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.6.18.56646",
      "templateHash": "3095519724929260960"
    }
  },
  "parameters": {
    "accountName": {
      "type": "string",
      "defaultValue": "[toLower(format('mongodb-{0}', uniqueString(resourceGroup().id)))]",
      "metadata": {
        "description": "Cosmos DB account name"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for the Cosmos DB account."
      }
    },
    "primaryRegion": {
      "type": "string",
      "metadata": {
        "description": "The primary replica region for the Cosmos DB account."
      }
    },
    "secondaryRegion": {
      "type": "string",
      "metadata": {
        "description": "The secondary replica region for the Cosmos DB account."
      }
    },
    "serverVersion": {
      "type": "string",
      "defaultValue": "4.2",
      "allowedValues": [
        "3.2",
        "3.6",
        "4.0",
        "4.2"
      ],
      "metadata": {
        "description": "Specifies the MongoDB server version to use."
      }
    },
    "defaultConsistencyLevel": {
      "type": "string",
      "defaultValue": "Session",
      "allowedValues": [
        "Eventual",
        "ConsistentPrefix",
        "Session",
        "BoundedStaleness",
        "Strong"
      ],
      "metadata": {
        "description": "The default consistency level of the Cosmos DB account."
      }
    },
    "maxStalenessPrefix": {
      "type": "int",
      "defaultValue": 100000,
      "maxValue": 2147483647,
      "minValue": 10,
      "metadata": {
        "description": "Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 1000000. Multi Region: 100000 to 1000000."
      }
    },
    "maxIntervalInSeconds": {
      "type": "int",
      "defaultValue": 300,
      "maxValue": 86400,
      "minValue": 5,
      "metadata": {
        "description": "Max lag time (seconds). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400."
      }
    },
    "databaseName": {
      "type": "string",
      "metadata": {
        "description": "The name for the Mongo DB database"
      }
    },
    "collection1Name": {
      "type": "string",
      "metadata": {
        "description": "The name for the first Mongo DB collection"
      }
    },
    "collection2Name": {
      "type": "string",
      "metadata": {
        "description": "The name for the second Mongo DB collection"
      }
    },
    "autoscaleMaxThroughput": {
      "type": "int",
      "defaultValue": 1000,
      "maxValue": 1000000,
      "minValue": 1000,
      "metadata": {
        "description": "Maximum throughput when using Autoscale Throughput Policy for the Database"
      }
    }
  },
  "variables": {
    "consistencyPolicy": {
      "Eventual": {
        "defaultConsistencyLevel": "Eventual"
      },
      "ConsistentPrefix": {
        "defaultConsistencyLevel": "ConsistentPrefix"
      },
      "Session": {
        "defaultConsistencyLevel": "Session"
      },
      "BoundedStaleness": {
        "defaultConsistencyLevel": "BoundedStaleness",
        "maxStalenessPrefix": "[parameters('maxStalenessPrefix')]",
        "maxIntervalInSeconds": "[parameters('maxIntervalInSeconds')]"
      },
      "Strong": {
        "defaultConsistencyLevel": "Strong"
      }
    },
    "locations": [
      {
        "locationName": "[parameters('primaryRegion')]",
        "failoverPriority": 0,
        "isZoneRedundant": false
      },
      {
        "locationName": "[parameters('secondaryRegion')]",
        "failoverPriority": 1,
        "isZoneRedundant": false
      }
    ]
  },
  "resources": [
    {
      "type": "Microsoft.DocumentDB/databaseAccounts",
      "apiVersion": "2021-10-15",
      "name": "[parameters('accountName')]",
      "location": "[parameters('location')]",
      "kind": "MongoDB",
      "properties": {
        "consistencyPolicy": "[variables('consistencyPolicy')[parameters('defaultConsistencyLevel')]]",
        "locations": "[variables('locations')]",
        "databaseAccountOfferType": "Standard",
        "apiProperties": {
          "serverVersion": "[parameters('serverVersion')]"
        }
      }
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/mongodbDatabases",
      "apiVersion": "2021-10-15",
      "name": "[format('{0}/{1}', parameters('accountName'), parameters('databaseName'))]",
      "properties": {
        "resource": {
          "id": "[parameters('databaseName')]"
        },
        "options": {
          "autoscaleSettings": {
            "maxThroughput": "[parameters('autoscaleMaxThroughput')]"
          }
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('accountName'))]"
      ]
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections",
      "apiVersion": "2021-10-15",
      "name": "[format('{0}/{1}/{2}', parameters('accountName'), parameters('databaseName'), parameters('collection1Name'))]",
      "properties": {
        "resource": {
          "id": "[parameters('collection1Name')]",
          "shardKey": {
            "user_id": "Hash"
          },
          "indexes": [
            {
              "key": {
                "keys": [
                  "_id"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "$**"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "user_id",
                  "user_address"
                ]
              },
              "options": {
                "unique": true
              }
            },
            {
              "key": {
                "keys": [
                  "_ts"
                ],
                "options": {
                  "expireAfterSeconds": 2629746
                }
              }
            }
          ],
          "options": {
            "If-Match": "<ETag>"
          }
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts/mongodbDatabases', parameters('accountName'), parameters('databaseName'))]"
      ]
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections",
      "apiVersion": "2021-10-15",
      "name": "[format('{0}/{1}/{2}', parameters('accountName'), parameters('databaseName'), parameters('collection2Name'))]",
      "properties": {
        "resource": {
          "id": "[parameters('collection2Name')]",
          "shardKey": {
            "company_id": "Hash"
          },
          "indexes": [
            {
              "key": {
                "keys": [
                  "_id"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "$**"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "company_id",
                  "company_address"
                ]
              },
              "options": {
                "unique": true
              }
            },
            {
              "key": {
                "keys": [
                  "_ts"
                ],
                "options": {
                  "expireAfterSeconds": 2629746
                }
              }
            }
          ],
          "options": {
            "If-Match": "<ETag>"
          }
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts/mongodbDatabases', parameters('accountName'), parameters('databaseName'))]"
      ]
    }
  ]
}

Standart sağlanan aktarım hızına sahip MongoDB için Azure Cosmos DB hesabı

Bu şablon MongoDB (3.2, 3.6, 4.0 ve 4.2) api'si için veritabanı düzeyinde 400 RU/sn standart (el ile) aktarım hızını paylaşan iki koleksiyona sahip bir Azure Cosmos DB hesabı oluşturur. Bu şablon, Azure Hızlı Başlangıç Şablonları Galerisi'nden tek tıklamayla dağıtım için de kullanılabilir.

Resource Manager şablonunu Azure'a dağıtma düğmesi.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.9.1.41621",
      "templateHash": "6120132423365709425"
    }
  },
  "parameters": {
    "accountName": {
      "type": "string",
      "defaultValue": "[format('mongodb-{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Cosmos DB account name"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for the Cosmos DB account."
      }
    },
    "primaryRegion": {
      "type": "string",
      "metadata": {
        "description": "The primary replica region for the Cosmos DB account."
      }
    },
    "secondaryRegion": {
      "type": "string",
      "metadata": {
        "description": "The secondary replica region for the Cosmos DB account."
      }
    },
    "defaultConsistencyLevel": {
      "type": "string",
      "defaultValue": "Eventual",
      "metadata": {
        "description": "The default consistency level of the Cosmos DB account."
      },
      "allowedValues": [
        "Eventual",
        "ConsistentPrefix",
        "Session",
        "BoundedStaleness",
        "Strong"
      ]
    },
    "serverVersion": {
      "type": "string",
      "defaultValue": "4.2",
      "metadata": {
        "description": "Specifies the MongoDB server version to use."
      },
      "allowedValues": [
        "3.2",
        "3.6",
        "4.0",
        "4.2"
      ]
    },
    "maxStalenessPrefix": {
      "type": "int",
      "defaultValue": 100000,
      "metadata": {
        "description": "Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 2147483647. Multi Region: 100000 to 2147483647."
      },
      "maxValue": 2147483647,
      "minValue": 10
    },
    "maxIntervalInSeconds": {
      "type": "int",
      "defaultValue": 300,
      "metadata": {
        "description": "Max lag time (seconds). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400."
      },
      "maxValue": 86400,
      "minValue": 5
    },
    "databaseName": {
      "type": "string",
      "metadata": {
        "description": "The name for the Mongo DB database"
      }
    },
    "sharedThroughput": {
      "type": "int",
      "defaultValue": 400,
      "metadata": {
        "description": "The shared throughput for the Mongo DB database, up to 25 collections"
      },
      "maxValue": 1000000,
      "minValue": 400
    },
    "collection1Name": {
      "type": "string",
      "metadata": {
        "description": "The name for the first Mongo DB collection"
      }
    },
    "collection2Name": {
      "type": "string",
      "metadata": {
        "description": "The name for the second Mongo DB collection"
      }
    },
    "dedicatedThroughput": {
      "type": "int",
      "defaultValue": 400,
      "metadata": {
        "description": "The dedicated throughput for the orders collection"
      },
      "maxValue": 1000000,
      "minValue": 400
    }
  },
  "variables": {
    "consistencyPolicy": {
      "Eventual": {
        "defaultConsistencyLevel": "Eventual"
      },
      "ConsistentPrefix": {
        "defaultConsistencyLevel": "ConsistentPrefix"
      },
      "Session": {
        "defaultConsistencyLevel": "Session"
      },
      "BoundedStaleness": {
        "defaultConsistencyLevel": "BoundedStaleness",
        "maxStalenessPrefix": "[parameters('maxStalenessPrefix')]",
        "maxIntervalInSeconds": "[parameters('maxIntervalInSeconds')]"
      },
      "Strong": {
        "defaultConsistencyLevel": "Strong"
      }
    },
    "locations": [
      {
        "locationName": "[parameters('primaryRegion')]",
        "failoverPriority": 0,
        "isZoneRedundant": false
      },
      {
        "locationName": "[parameters('secondaryRegion')]",
        "failoverPriority": 1,
        "isZoneRedundant": false
      }
    ]
  },
  "resources": [
    {
      "type": "Microsoft.DocumentDB/databaseAccounts",
      "apiVersion": "2022-05-15",
      "name": "[toLower(parameters('accountName'))]",
      "location": "[parameters('location')]",
      "kind": "MongoDB",
      "properties": {
        "consistencyPolicy": "[variables('consistencyPolicy')[parameters('defaultConsistencyLevel')]]",
        "locations": "[variables('locations')]",
        "databaseAccountOfferType": "Standard",
        "enableAutomaticFailover": true,
        "apiProperties": {
          "serverVersion": "[parameters('serverVersion')]"
        },
        "capabilities": [
          {
            "name": "DisableRateLimitingResponses"
          }
        ]
      }
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/mongodbDatabases",
      "apiVersion": "2022-05-15",
      "name": "[format('{0}/{1}', toLower(parameters('accountName')), parameters('databaseName'))]",
      "properties": {
        "resource": {
          "id": "[parameters('databaseName')]"
        },
        "options": {
          "throughput": "[parameters('sharedThroughput')]"
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts', toLower(parameters('accountName')))]"
      ]
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections",
      "apiVersion": "2022-05-15",
      "name": "[format('{0}/{1}/{2}', toLower(parameters('accountName')), parameters('databaseName'), parameters('collection1Name'))]",
      "properties": {
        "resource": {
          "id": "[parameters('collection1Name')]",
          "shardKey": {
            "user_id": "Hash"
          },
          "indexes": [
            {
              "key": {
                "keys": [
                  "_id"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "$**"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "product_name",
                  "product_category_name"
                ]
              }
            }
          ]
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts/mongodbDatabases', toLower(parameters('accountName')), parameters('databaseName'))]"
      ]
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections",
      "apiVersion": "2022-05-15",
      "name": "[format('{0}/{1}/{2}', toLower(parameters('accountName')), parameters('databaseName'), parameters('collection2Name'))]",
      "properties": {
        "resource": {
          "id": "[parameters('collection2Name')]",
          "shardKey": {
            "company_id": "Hash"
          },
          "indexes": [
            {
              "key": {
                "keys": [
                  "_id"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "$**"
                ]
              }
            },
            {
              "key": {
                "keys": [
                  "customer_id",
                  "order_id"
                ]
              }
            }
          ]
        },
        "options": {
          "throughput": "[parameters('dedicatedThroughput')]"
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts/mongodbDatabases', toLower(parameters('accountName')), parameters('databaseName'))]"
      ]
    }
  ]
}

Sonraki adımlar

Bazı ek kaynaklar şunlardır: