使用 Powershell 扩展 CosmosDB 容器

使用 Powershell 扩展 CosmosDB 容器

我正在尝试使用 Powershell 扩展 CosmosDB 容器,但在文档中找不到任何内容。我尝试了以下脚本,但没有成功。

$resourceName = $CosmosDB + "/sql/" + $CosmosDatabase + "/" + $CosmosContainer
   $ContainerProperties = @{
       "resource"=@{
           "id"=$CosmosContainer;
           "partitionKey"=@{
               "paths"=@("/DefaultKey");
               "kind"="Hash"
           }
       };
       "options"=@{ "Throughput"=$CosmosScale }
   }

   Set-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroup -Name $resourceName -PropertyObject $ContainerProperties -Force

任何见解都值得赞赏。

答案1

以下是具体操作方法。此脚本将更新 SQL (Core) API 帐户的数据库或容器吞吐量为 500 RU/s

# Update RU for an Azure Cosmos DB SQL (Core) API database or container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container1"
$databaseResourceName = $accountName + "/sql/" + $databaseName + "/throughput"
$containerResourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName + "/throughput"
$throughput = 500
$updateResource = "database" # or "container"

$properties = @{
    "resource"=@{"throughput"=$throughput}
}

if($updateResource -eq "database"){
Set-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/settings" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $databaseResourceName -PropertyObject $properties
}
elseif($updateResource -eq "container"){
Set-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers/settings" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $containerResourceName -PropertyObject $properties
}
else {
    Write-Host("Must select database or container")
} 

答案2

这是另一种方法,将其视为 arm 模板部署:

$rg = "my-resource-group-name"

$armParams = @{
    accountName = "my-cosmosdb-sqlapi-account"
    databaseName = "myCosmosDbName"
    containerName = "myCosmosContainerName"
    throughput = 500
}

$armTemplateHashtable = @{
    '$schema'        = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#"
    contentVersion = "1.0.0.0"
    parameters     = @{
        accountName = @{
            type = "string"
        }
        databaseName = @{
            type = "string"
        }
        containerName = @{
            type = "string"
        }
        throughput = @{
            type = "int"
        }
    }
    variables = @{
        accountName = "[toLower(parameters('accountName'))]"
    }
    resources = @(
        @{
            type = "Microsoft.DocumentDB/databaseAccounts/apis/databases/containers/settings"
            name = "[concat(variables('accountName'), '/sql/', parameters('databaseName'), '/', parameters('containerName'), '/throughput')]"
            apiVersion = "2016-03-31"
            properties = @{
                resource = @{
                    throughput = "[parameters('throughput')]"
                }
            }
        }
    )
}

New-AzResourceGroupDeployment -Name "set-cosmos-throughput" `
    -ResourceGroupName $rg `
    -Mode Incremental `
    -TemplateObject $armTemplateHashtable `
    -TemplateParameterObject $armParams

您可以使用相同的方法使用 json arm 模板文件,但如果从自动化帐户运行(无法轻松访问文件系统),使用哈希表对象会更简单。

相关内容