是否可以在ARM模板中创建documentDB数据库和帐户

时间:2017-02-16 07:15:50

标签: azure azure-cosmosdb azure-stream-analytics arm-template

在Azure门户中,可以在创建Stream Analytics作业输出的步骤中创建documentDB数据库和集合。使用ARM模板创建流作业和输出时是否可以执行相同的操作?

我发现只能将documentDB帐户创建为具有ARM模板的资源,但是可以在设置作业输出时创建数据库和集合,就像在门户中一样吗? / p>

2 个答案:

答案 0 :(得分:2)

根据我的经验,目前不支持在ARM模板中创建documentDB数据库和集合。我将与Azure团队确认。如果有任何反馈,我会在这里发布。

我的解决方法是我们可以使用REST API来做到这一点。如果documentDB数据库和集合已经存在,那么我们可以通过ARM模板创建带有documentDb数据库和集合的作业输出。

 {
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.1.0.0",
  "parameters": {
    "databaseAccountName": {

      "type": "string",

      "metadata": {

        "description": "The DocumentDB database account name."

      }

    },
      "dbname": {

        "type": "string",

        "metadata": {

          "description": "The  database name"

        }
        },
        "collectionname": {

          "type": "string",

          "metadata": {

            "description": "collectionname"

          }

        },
          "streamAnalyticsJobName": {
            "type": "string",
            "minLength": 3,
            "maxLength": 63,
            "metadata": {
              "description": "Stream Analytics Job Name, can contain alphanumeric characters and hypen and must be 3-63 characters long"
            }
          },
          "numberOfStreamingUnits": {
            "type": "int",
            "minValue": 1,
            "maxValue": 48,
            "allowedValues": [
              1,
              3,
              6,
              12,
              18,
              24,
              30,
              36,
              42,
              48
            ],
            "metadata": {
              "description": "Number of Streaming Units"
            }
          }
        },
   "variables": {

          "offerType": "Standard"

        },
        "resources": [
          {
            "type": "Microsoft.StreamAnalytics/StreamingJobs",
            "apiVersion": "2016-03-01",
            "name": "[parameters('streamAnalyticsJobName')]",
            "location": "[resourceGroup().location]",
            "properties": {
              "sku": {
                "name": "Standard"
              },
              "outputErrorPolicy": "stop",
              "eventsOutOfOrderPolicy": "adjust",
              "eventsOutOfOrderMaxDelayInSeconds": 0,
              "eventsLateArrivalMaxDelayInSeconds": 5,
              "dataLocale": "en-US",
              "inputs": [],
              "Outputs": [
                {
                  "Name": "relateddb",
                  "Properties": {
                    "DataSource": {
                      "Properties": {
                        "AccountId": "[parameters('databaseAccountName')]",
                        "AccountKey": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('databaseAccountName')), '2015-11-06').primaryMasterKey]",
                        "CollectionNamePattern": "[parameters('collectionname')]",
                        "Database": "[parameters('dbname')]",
                        "DocumentId": null,
                        "PartitionKey": null
                      },
                      "Type": "Microsoft.Storage/DocumentDB"
                    },
                    "Diagnostics": null,
                    "Etag": null,
                    "Serialization": null
                  }
                }
              ],
              "transformation": {
                "name": "Transformation",
                "properties": {
                  "streamingUnits": "[parameters('numberOfStreamingUnits')]",
                  "query": "SELECT\r\n    *\r\nINTO\r\n    [YourOutputAlias]\r\nFROM\r\n    [YourInputAlias]"
                }
              }
            }
          }
        ]
      }

答案 1 :(得分:1)

万一有人偶然发现:现在可以通过ARM创建数据库和容器。

  

Azure资源管理器中对数据库,容器和其他资源的ARM支持

     

Azure Cosmos DB现在在Azure资源管理器中提供对数据库,容器和商品的支持。用户现在可以置备数据库和容器,并使用Azure资源管理器模板或PowerShell设置吞吐量。该支持可用于所有API,包括SQL(核心),MongoDB,Cassandra,Gremlin和Table。此功能还允许客户创建自定义RBAC角色,以创建,删除或修改Azure Cosmos DB中数据库和容器上的设置。若要了解更多信息并开始使用,请参阅Azure Cosmos DB Azure资源管理器模板。

https://docs.microsoft.com/en-us/azure/cosmos-db/manage-sql-with-resource-manager

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "accountName": {
            "type": "string",
            "defaultValue": "[concat('sql-', 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": "Session",
            "allowedValues": [ "Eventual", "ConsistentPrefix", "Session", "BoundedStaleness", "Strong" ],
            "metadata": {
                "description": "The default consistency level of the Cosmos DB account."
            }
        },
        "maxStalenessPrefix": {
            "type": "int",
            "minValue": 10,
            "defaultValue": 100000,
            "maxValue": 2147483647,
            "metadata": {
                "description": "Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 1000000. Multi Region: 100000 to 1000000."
            }
        },
        "maxIntervalInSeconds": {
            "type": "int",
            "minValue": 5,
            "defaultValue": 300,
            "maxValue": 86400,
            "metadata": {
                "description": "Max lag time (seconds). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400."
            }
        },  
        "multipleWriteLocations": {
            "type": "bool",
            "defaultValue": true,
            "allowedValues": [ true, false ],
            "metadata": {
                "description": "Enable multi-master to make all regions writable."
            }
        },
        "automaticFailover": {
            "type": "bool",
            "defaultValue": false,
            "allowedValues": [ true, false ],
            "metadata": {
                "description": "Enable automatic failover for regions. Ignored when Multi-Master is enabled"
            }
        },
        "databaseName": {
            "type": "string",
            "metadata": {
                "description": "The name for the SQL database"
            }
        },
        "throughput": {
            "type": "int",
            "defaultValue": 400,
            "minValue": 400,
            "maxValue": 1000000,
            "metadata": {
                "description": "The throughput for the database"
            }           
        },
        "container1Name": {
            "type": "string",
            "defaultValue": "container1",
            "metadata": {
                "description": "The name for the first SQL container"
            }
        },
        "container2Name": {
            "type": "string",
            "defaultValue": "container2",
            "metadata": {
                "description": "The name for the second SQL container"
            }
        }
    },
    "variables": {
        "accountName": "[toLower(parameters('accountName'))]",
        "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
            }, 
            {
                "locationName": "[parameters('secondaryRegion')]",
                "failoverPriority": 1
            }
        ]
    },
    "resources": 
    [
        {
            "type": "Microsoft.DocumentDB/databaseAccounts",
            "name": "[variables('accountName')]",
            "apiVersion": "2016-03-31",
            "location": "[parameters('location')]",
            "kind": "GlobalDocumentDB",
            "properties": {
                "consistencyPolicy": "[variables('consistencyPolicy')[parameters('defaultConsistencyLevel')]]",
                "locations": "[variables('locations')]",
                "databaseAccountOfferType": "Standard",
                "enableAutomaticFailover": "[parameters('automaticFailover')]",
                "enableMultipleWriteLocations": "[parameters('multipleWriteLocations')]"
            }
        },
        {
            "type": "Microsoft.DocumentDB/databaseAccounts/apis/databases",
            "name": "[concat(variables('accountName'), '/sql/', parameters('databaseName'))]",
            "apiVersion": "2016-03-31",
            "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts/', variables('accountName'))]" ],
            "properties":{
                "resource":{
                    "id": "[parameters('databaseName')]"
                },
                "options": { "throughput": "[parameters('throughput')]" }
            }
        },
        {
            "type": "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers",
            "name": "[concat(variables('accountName'), '/sql/', parameters('databaseName'), '/', parameters('container1Name'))]",
            "apiVersion": "2016-03-31",
            "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts/apis/databases', variables('accountName'), 'sql', parameters('databaseName'))]" ],
            "properties":
            {
                "resource":{
                    "id":  "[parameters('container1Name')]",
                    "partitionKey": {
                        "paths": [
                        "/MyPartitionKey1"
                        ],
                        "kind": "Hash"
                    },
                    "indexingPolicy": {
                        "indexingMode": "consistent",
                        "includedPaths": [{
                                "path": "/*",
                                "indexes": [
                                    {
                                        "kind": "Range",
                                        "dataType": "number",
                                        "precision": -1
                                    },
                                    {
                                        "kind": "Range",
                                        "dataType": "string",
                                        "precision": -1
                                    }
                                ]
                            }
                        ],
                        "excludedPaths": [{
                                "path": "/MyPathToNotIndex/*"
                            }
                        ]
                    }
                }
            }
        },
        {
            "type": "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers",
            "name": "[concat(variables('accountName'), '/sql/', parameters('databaseName'), '/', parameters('container2Name'))]",
            "apiVersion": "2016-03-31",
            "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts/apis/databases', variables('accountName'), 'sql', parameters('databaseName'))]" ],
            "properties":
            {
                "resource":{
                    "id":  "[parameters('container2Name')]",
                    "partitionKey": {
                        "paths": [
                        "/MyPartitionKey2"
                        ],
                        "kind": "Hash"
                    },
                    "indexingPolicy": {
                        "indexingMode": "consistent",
                        "includedPaths": [{
                                "path": "/*",
                                "indexes": [
                                    {
                                        "kind": "Range",
                                        "dataType": "number",
                                        "precision": -1
                                    },
                                    {
                                        "kind": "Range",
                                        "dataType": "string",
                                        "precision": -1
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        }
    ]
}