使用 Azure Resource Manager 範本管理數據表資源的 Azure Cosmos DB

適用於:

在本文中,您會了解如何利用 Azure Resource Manager 範本來部署和管理 Azure Cosmos DB 帳戶、資料庫和容器。

本文提供僅適用於數據表帳戶的 API 範例,若要尋找其他 API 類型帳戶的範例,請參閱:搭配 Azure Cosmos DB 適用於 Cassandra、GremlinMongoDBSQL 的 API 使用 Azure Resource Manager 範本文章。

重要

  • 帳戶名稱限制為 44 個字元,全部小寫。
  • 若要變更輸送量值,請使用已更新的 RU/秒重新部署範本。
  • 您在新增或移除 Azure Cosmos DB 帳戶的位置時,無法同時修改其他屬性。 這些作業必須個別執行。

若要建立下列任何 Azure Cosmos DB 資源,請將下列範例範本複製到新的 JSON 檔案中。 當您使用不同的名稱和值來部署相同資源的多個執行個體時,可以選擇建立參數 JSON 檔案以供使用。 部署 Azure Resource Manager 範本的方式有很多種,包括 Azure 入口網站Azure CLIAzure PowerShellGitHub

提示

若要在使用 API for Table 時啟用共用輸送量,請在 Azure 入口網站 中啟用帳戶層級輸送量。

具有自動調整輸送量的數據表 Azure Cosmos DB 帳戶

此範本會建立適用於數據表的 Azure Cosmos DB 帳戶,其中包含一個具有自動調整輸送量的數據表。 此範本也適用於從 Azure 快速入門範本資源庫執行的單鍵式部署。

Button to deploy the Resource Manager template to Azure.

{
  "$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": "1598935088646118897"
    }
  },
  "parameters": {
    "accountName": {
      "type": "string",
      "defaultValue": "[format('table-{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 region for the Cosmos DB account."
      }
    },
    "secondaryRegion": {
      "type": "string",
      "metadata": {
        "description": "The secondary region for the Cosmos DB account."
      }
    },
    "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 2147483647. Multi Region: 100000 to 2147483647."
      }
    },
    "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."
      }
    },
    "systemManagedFailover": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
        "description": "Enable system managed failover for regions"
      }
    },
    "tableName": {
      "type": "string",
      "metadata": {
        "description": "The name for the table"
      }
    },
    "autoscaleMaxThroughput": {
      "type": "int",
      "defaultValue": 4000,
      "maxValue": 1000000,
      "minValue": 1000,
      "metadata": {
        "description": "Maximum autoscale throughput for the table"
      }
    }
  },
  "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": "GlobalDocumentDB",
      "properties": {
        "capabilities": [
          {
            "name": "EnableTable"
          }
        ],
        "consistencyPolicy": "[variables('consistencyPolicy')[parameters('defaultConsistencyLevel')]]",
        "locations": "[variables('locations')]",
        "databaseAccountOfferType": "Standard",
        "enableAutomaticFailover": "[parameters('systemManagedFailover')]"
      }
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/tables",
      "apiVersion": "2022-05-15",
      "name": "[format('{0}/{1}', toLower(parameters('accountName')), parameters('tableName'))]",
      "properties": {
        "resource": {
          "id": "[parameters('tableName')]"
        },
        "options": {
          "autoscaleSettings": {
            "maxThroughput": "[parameters('autoscaleMaxThroughput')]"
          }
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts', toLower(parameters('accountName')))]"
      ]
    }
  ]
}

具有標準布建輸送量的數據表 Azure Cosmos DB 帳戶

此範本會針對具有標準輸送量之數據表的 API 建立適用於數據表的 Azure Cosmos DB 帳戶。 此範本也適用於從 Azure 快速入門範本資源庫執行的單鍵式部署。

Button to deploy the Resource Manager template to Azure.

{
  "$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": "11114398738530243204"
    }
  },
  "parameters": {
    "accountName": {
      "type": "string",
      "defaultValue": "[format('table-{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 region for the Cosmos DB account."
      }
    },
    "secondaryRegion": {
      "type": "string",
      "metadata": {
        "description": "The secondary region for the Cosmos DB account."
      }
    },
    "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 2147483647. Multi Region: 100000 to 2147483647."
      }
    },
    "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."
      }
    },
    "systemManagedFailover": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
        "description": "Enable system managed failover for regions"
      }
    },
    "tableName": {
      "type": "string",
      "metadata": {
        "description": "The name for the table"
      }
    },
    "throughput": {
      "type": "int",
      "defaultValue": 400,
      "metadata": {
        "description": "The throughput for the table"
      },
      "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": "GlobalDocumentDB",
      "properties": {
        "capabilities": [
          {
            "name": "EnableTable"
          }
        ],
        "consistencyPolicy": "[variables('consistencyPolicy')[parameters('defaultConsistencyLevel')]]",
        "locations": "[variables('locations')]",
        "databaseAccountOfferType": "Standard",
        "enableAutomaticFailover": "[parameters('systemManagedFailover')]"
      }
    },
    {
      "type": "Microsoft.DocumentDB/databaseAccounts/tables",
      "apiVersion": "2022-05-15",
      "name": "[format('{0}/{1}', toLower(parameters('accountName')), parameters('tableName'))]",
      "properties": {
        "resource": {
          "id": "[parameters('tableName')]"
        },
        "options": {
          "throughput": "[parameters('throughput')]"
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts', toLower(parameters('accountName')))]"
      ]
    }
  ]
}

下一步

以下是一些額外資源: