使用 PowerShell 管理 Azure Cosmos DB Core (SQL) API 资源Manage Azure Cosmos DB Core (SQL) API resources using PowerShell

适用于: SQL API

以下指南介绍了如何使用 PowerShell 通过脚本来自动管理 Azure Cosmos DB Core (SQL) API 资源,其中包括 Cosmos 帐户、数据库、容器和吞吐量。The following guide describes how to use PowerShell to script and automate management of Azure Cosmos DB Core (SQL) API resources, including the Cosmos account, database, container, and throughput. 对于其他 API 的 PowerShell cmdlet,请参阅适用于 Cassandra 的 PowerShell 示例适用于 MongoDB API 的 PowerShell 示例适用于 Gremlin 的 PowerShell 示例适用于 Table 的 PowerShell 示例For PowerShell cmdlets for other APIs see PowerShell Samples for Cassandra, PowerShell Samples for MongoDB API, PowerShell Samples for Gremlin, PowerShell Samples for Table

备注

本文中的示例使用 Az.CosmosDB 管理 cmdlet。Samples in this article use Az.CosmosDB management cmdlets. 有关最新更改,请参阅 Az.CosmosDB API 参考页。See the Az.CosmosDB API reference page for the latest changes.

若要跨平台管理 Azure Cosmos DB,可在使用 AzAz.CosmosDB cmdlet 时结合使用跨平台 PowerShell 以及 Azure CLIREST APIAzure 门户For cross-platform management of Azure Cosmos DB, you can use the Az and Az.CosmosDB cmdlets with cross-platform PowerShell, as well as the Azure CLI, the REST API, or the Azure portal.

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。This article has been updated to use the Azure Az PowerShell module. 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 AzTo learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

入门Getting Started

请按照如何安装和配置 Azure PowerShell 中的说明,安装 PowerShell 并在其中登录 Azure 帐户。Follow the instructions in How to install and configure Azure PowerShell to install and sign in to your Azure account in PowerShell.

重要

无法重命名 Azure Cosmos DB 资源,因为这违反了 Azure 资源管理器与资源 URI 的工作方式。Azure Cosmos DB resources cannot be renamed as this violates how Azure Resource Manager works with resource URIs.

Azure Cosmos 帐户Azure Cosmos accounts

以下部分演示如何管理 Azure Cosmos 帐户,包括:The following sections demonstrate how to manage the Azure Cosmos account, including:

创建 Azure Cosmos 帐户Create an Azure Cosmos account

此命令创建一个 Azure Cosmos DB 数据库帐户,该帐户使用多区域自动故障转移和有限过期一致性策略This command creates an Azure Cosmos DB database account with multiple regions, automatic failover and bounded-staleness consistency policy.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "China North" -FailoverPriority 1 -IsZoneRedundant 0

New-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -LocationObject $locations `
    -Name $accountName `
    -ApiKind $apiKind `
    -EnableAutomaticFailover:$true `
    -DefaultConsistencyLevel $consistencyLevel `
    -MaxStalenessIntervalInSeconds $maxStalenessInterval `
    -MaxStalenessPrefix $maxStalenessPrefix
  • $resourceGroupName:要在其中部署 Cosmos 帐户的 Azure 资源组。$resourceGroupName The Azure resource group into which to deploy the Cosmos account. 它必须已存在。It must already exist.
  • $locations:数据库帐户的区域,带有 FailoverPriority 0 的区域为写入区域。$locations The regions for the database account, the region with FailoverPriority 0 is the write region.
  • $accountName:Azure Cosmos 帐户的名称。$accountName The name for the Azure Cosmos account. 必须独一无二且必须为小写,仅包含字母数字和“-”字符,长度为 3 到 31 个字符。Must be unique, lowercase, include only alphanumeric and '-' characters, and between 3 and 31 characters in length.
  • $apiKind:要创建的 Cosmos 帐户的类型。$apiKind The type of Cosmos account to create. 有关详细信息,请参阅 Cosmos DB 中的 APIFor more information, see APIs in Cosmos DB.
  • $consistencyPolicy$maxStalenessInterval$maxStalenessPrefix:Azure Cosmos 帐户的默认一致性级别和设置。$consistencyPolicy, $maxStalenessInterval, and $maxStalenessPrefix The default consistency level and settings of the Azure Cosmos account. 有关详细信息,请参阅 Azure Cosmos DB 中的一致性级别For more information, see Consistency Levels in Azure Cosmos DB.

可以为 Azure Cosmos 帐户配置 IP 防火墙、虚拟网络服务终结点和专用终结点。Azure Cosmos accounts can be configured with IP Firewall, Virtual Network service endpoints, and private endpoints. 有关如何为 Azure Cosmos DB 配置 IP 防火墙的信息,请参阅配置 IP 防火墙For information on how to configure the IP Firewall for Azure Cosmos DB, see Configure IP Firewall. 若要了解如何为 Azure Cosmos DB 启用服务终结点,请参阅配置从虚拟网络进行访问的权限For information on how to enable service endpoints for Azure Cosmos DB, see Configure access from virtual Networks. 若要了解如何为 Azure Cosmos DB 启用专用终结点,请参阅配置从专用终结点进行访问的权限For information on how to enable private endpoints for Azure Cosmos DB, see Configure access from private endpoints.

列出资源组中的所有 Azure Cosmos 帐户List all Azure Cosmos accounts in a Resource Group

该命令列出资源组中的所有 Azure Cosmos 帐户。This command lists all Azure Cosmos accounts in a Resource Group.

$resourceGroupName = "myResourceGroup"

Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName

获取 Azure Cosmos 帐户的属性Get the properties of an Azure Cosmos account

使用此命令可以获取现有 Azure Cosmos 帐户的属性。This command allows you to get the properties of an existing Azure Cosmos account.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName

更新 Azure Cosmos 帐户Update an Azure Cosmos account

此命令可更新 Azure Cosmos DB 数据库帐户属性。This command allows you to update your Azure Cosmos DB database account properties. 可更新的属性包括:Properties that can be updated include the following:

  • 添加或删除区域Adding or removing regions
  • 更改默认的一致性策略Changing default consistency policy
  • 更改 IP 范围筛选器Changing IP Range Filter
  • 更改虚拟网络配置Changing Virtual Network configurations
  • 启用多区域写入Enabling multi-region writes

备注

不能同时添加或删除区域 (locations) 并更改 Azure Cosmos 帐户的其他属性。You cannot simultaneously add or remove regions (locations) and change other properties for an Azure Cosmos account. 修改区域的操作必须作为单独的操作与任何其他对帐户的更改操作分开执行。Modifying regions must be performed as a separate operation from any other change to the account.

备注

此命令可添加和删除区域,但不可修改故障转移优先级或触发手动故障转移。This command allows you to add and remove regions but does not allow you to modify failover priorities or trigger a manual failover. 请参阅修改故障转移优先级触发手动故障转移See Modify failover priority and Trigger manual failover.

# Create account with two regions
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "Session"
$enableAutomaticFailover = $true
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "China North" -FailoverPriority 1 -IsZoneRedundant 0

# Create the Cosmos DB account
New-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -LocationObject $locations `
    -Name $accountName `
    -ApiKind $apiKind `
    -EnableAutomaticFailover:$enableAutomaticFailover `
    -DefaultConsistencyLevel $consistencyLevel

# Add a region to the account
$locationObject2 = @()
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "China North" -FailoverPriority 1 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "China East 2" -FailoverPriority 2 -IsZoneRedundant 0

Update-AzCosmosDBAccountRegion `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -LocationObject $locationObject2

Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."
Write-Host "When region was added, press any key to continue."
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()

# Remove China North region from the account
$locationObject3 = @()
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "China East 2" -FailoverPriority 1 -IsZoneRedundant 0

Update-AzCosmosDBAccountRegion `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -LocationObject $locationObject3

Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."

为 Azure Cosmos 帐户启用多个写入区域Enable multiple write regions for an Azure Cosmos account

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $false
$enableMultiMaster = $true

# First disable automatic failover - cannot have both automatic
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableAutomaticFailover:$enableAutomaticFailover

# Now enable multi-region writes
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableMultipleWriteLocations:$enableMultiMaster

删除 Azure Cosmos 帐户Delete an Azure Cosmos account

此命令删除现有的 Azure Cosmos 帐户。This command deletes an existing Azure Cosmos account.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Remove-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -PassThru:$true

更新 Azure Cosmos 帐户的标记Update Tags of an Azure Cosmos account

此命令设置 Azure Cosmos 帐户的 Azure 资源标记This command sets the Azure resource tags for an Azure Cosmos account. 标记可以在创建帐户时使用 New-AzCosmosDBAccount 来设置,也可以在更新帐户时使用 Update-AzCosmosDBAccount 来设置。Tags can be set both at account creation using New-AzCosmosDBAccount as well as on account update using Update-AzCosmosDBAccount.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$tags = @{dept = "Finance"; environment = "Production";}

Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Tag $tags

列出帐户密钥List Account Keys

创建 Azure Cosmos 帐户时,服务生成两个主访问密钥,这两个密钥可用于访问 Azure Cosmos 帐户时进行的身份验证。When you create an Azure Cosmos account, the service generates two primary access keys that can be used for authentication when the Azure Cosmos account is accessed. 还会生成只读密钥,用于对只读操作进行身份验证。Read-only keys for authenticating read-only operations are also generated. 提供两个访问密钥后,Azure Cosmos DB 支持在不中断 Azure Cosmos 帐户的情况下重新生成密钥,并以轮换的方式一次使用一个密钥。By providing two access keys, Azure Cosmos DB enables you to regenerate and rotate one key at a time with no interruption to your Azure Cosmos account. Cosmos DB 帐户有两个读写密钥(主密钥和辅助密钥)和两个只读密钥(主密钥和辅助密钥)。Cosmos DB accounts have two read-write keys (primary and secondary) and two read-only keys (primary and secondary).

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Type "Keys"

列出连接字符串List Connection Strings

以下命令检索用于将应用程序连接到 Cosmos DB 帐户的连接字符串。The following command retrieves connection strings to connect apps to the Cosmos DB account.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Type "ConnectionStrings"

重新生成帐户密钥Regenerate Account Keys

应定期重新生成 Azure Cosmos 帐户访问密钥,确保连接安全。Access keys to an Azure Cosmos account should be periodically regenerated to help keep connections secure. 系统会向帐户分配主要和辅助访问密钥。A primary and secondary access keys are assigned to the account. 这样,在某个时刻重新生成某个密钥时,客户端仍可进行访问。This allows clients to maintain access while one key at a time is regenerated. Azure Cosmos 帐户有四种类型的密钥(Primary、Secondary、PrimaryReadonly 和 SecondaryReadonly)There are four types of keys for an Azure Cosmos account (Primary, Secondary, PrimaryReadonly, and SecondaryReadonly)

$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "mycosmosaccount" # Must be all lower case
$keyKind = "primary" # Other key kinds: secondary, primaryReadOnly, secondaryReadOnly

New-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -KeyKind $keyKind

启用自动故障转移Enable automatic failover

以下命令设置一个 Cosmos DB 帐户,使之能够在主要区域变得不可用时自动故障转移到次要区域。The following command sets a Cosmos DB account to fail over automatically to its secondary region should the primary region become unavailable.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $true
$enableMultiMaster = $false

# First disable multi-region writes - cannot have both automatic
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableMultipleWriteLocations:$enableMultiMaster

# Now enable automatic failover
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableAutomaticFailover:$enableAutomaticFailover

修改故障转移优先级Modify Failover Priority

对于配置了自动故障转移的帐户,可以更改在主要副本不可用时 Cosmos 将次要副本提升为主要副本的顺序。For accounts configured with Automatic Failover, you can change the order in which Cosmos will promote secondary replicas to primary should the primary become unavailable.

对于以下示例,假定当前的故障转移优先级为 China North 2 = 0China East 2 = 1China East = 2For the example below, assume the current failover priority is China North 2 = 0, China East 2 = 1, China East = 2. 此命令会将其更改为 China North 2 = 0China East = 1China East 2 = 2The command will change this to China North 2 = 0, China East = 1, China East 2 = 2.

注意

failoverPriority=0 的情况下更改位置会触发 Azure Cosmos 帐户的手动故障转移。Changing the location for failoverPriority=0 will trigger a manual failover for an Azure Cosmos account. 任何其他的优先级更改不会触发故障转移。Any other priority changes will not trigger a failover.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("China North 2", "China East", "China East 2") # Regions ordered by UPDATED failover priority

Update-AzCosmosDBAccountFailoverPriority `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -FailoverPolicy $locations

触发手动故障转移Trigger Manual Failover

对于配置了手动故障转移的帐户,可以通过将设置修改为 failoverPriority=0 来进行故障转移并将所有次要副本提升为主要副本。For accounts configured with Manual Failover, you can fail over and promote any secondary replica to primary by modifying to failoverPriority=0. 此操作可用于启动灾难恢复演练以测试灾难恢复规划。This operation can be used to initiate a disaster recovery drill to test disaster recovery planning.

在下面的示例中,假设帐户的当前故障转移优先级为 China North 2 = 0China East 2 = 1,然后将区域互换。For the example below, assume the account has a current failover priority of China North 2 = 0 and China East 2 = 1 and flip the regions.

注意

failoverPriority=0 的情况下更改 locationName 会触发 Azure Cosmos 帐户的手动故障转移。Changing locationName for failoverPriority=0 will trigger a manual failover for an Azure Cosmos account. 任何其他优先级更改都不会触发故障转移。Any other priority change will not trigger a failover.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("China East 2", "China North 2") # Regions ordered by UPDATED failover priority

Update-AzCosmosDBAccountFailoverPriority `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -FailoverPolicy $locations

列出 Azure Cosmos DB 帐户上的资源锁List resource locks on an Azure Cosmos DB account

资源锁可放置在数据库和集合等 Azure Cosmos DB 资源上。Resource locks can be placed on Azure Cosmos DB resources including databases and collections. 以下示例演示了如何在 Azure Cosmos DB 帐户上列出所有的 Azure 资源锁。The example below shows how to list all Azure resource locks on an Azure Cosmos DB account.

$resourceGroupName = "myResourceGroup"
$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$accountName = "mycosmosaccount"

Get-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceTypeAccount `
    -ResourceName $accountName

Azure Cosmos DB 数据库Azure Cosmos DB Database

以下部分演示了如何管理 Azure Cosmos DB 数据库,具体包括:The following sections demonstrate how to manage the Azure Cosmos DB database, including:

创建 Azure Cosmos DB 数据库Create an Azure Cosmos DB database

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

New-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

创建共享吞吐量的 Azure Cosmos DB 数据库Create an Azure Cosmos DB database with shared throughput

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$databaseRUs = 400

New-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName `
    -Throughput $databaseRUs

获取 Azure Cosmos DB 数据库的吞吐量Get the throughput of an Azure Cosmos DB database

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlDatabaseThroughput `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

将数据库吞吐量迁移到自动缩放Migrate database throughput to autoscale

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Invoke-AzCosmosDBSqlDatabaseThroughputMigration `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName `
    -ThroughputType Autoscale

获取帐户中的所有 Azure Cosmos DB 数据库Get all Azure Cosmos DB databases in an account

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName

获取单个 Azure Cosmos DB 数据库Get a single Azure Cosmos DB database

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

删除 Azure Cosmos DB 数据库Delete an Azure Cosmos DB database

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Remove-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

在 Azure Cosmos DB 数据库上创建资源锁以防止删除Create a resource lock on an Azure Cosmos DB database to prevent delete

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"

New-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName `
    -LockLevel $lockLevel

删除 Azure Cosmos DB 数据库上的资源锁Remove a resource lock on an Azure Cosmos DB database

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"

Remove-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName

Azure Cosmos DB 容器Azure Cosmos DB Container

以下部分演示了如何管理 Azure Cosmos DB 容器,具体包括:The following sections demonstrate how to manage the Azure Cosmos DB container, including:

创建 Azure Cosmos DB 容器Create an Azure Cosmos DB container

# Create an Azure Cosmos DB container with default indexes and throughput at 400 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$throughput = 400 #minimum = 400

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -Throughput $throughput

使用自动缩放功能创建 Azure Cosmos DB 容器Create an Azure Cosmos DB container with autoscale

# Create an Azure Cosmos DB container with default indexes and autoscale throughput at 4000 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$autoscaleMaxThroughput = 4000 #minimum = 4000

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -AutoscaleMaxThroughput $autoscaleMaxThroughput

使用大分区键大小创建 Azure Cosmos DB 容器Create an Azure Cosmos DB container with a large partition key size

# Create an Azure Cosmos DB container with a large partition key value (version = 2)
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -PartitionKeyVersion 2

获取 Azure Cosmos DB 容器的吞吐量Get the throughput of an Azure Cosmos DB container

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Get-AzCosmosDBSqlContainerThroughput `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

将容器吞吐量迁移到自动缩放Migrate container throughput to autoscale

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Invoke-AzCosmosDBSqlContainerThroughputMigration `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -ThroughputType Autoscale

使用自定义索引策略创建 Azure Cosmos DB 容器Create an Azure Cosmos DB container with custom index policy

# Create a container with a custom indexing policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$indexPathIncluded = "/*"
$indexPathExcluded = "/myExcludedPath/*"

$includedPathIndex = New-AzCosmosDBSqlIncludedPathIndex -DataType String -Kind Range
$includedPath = New-AzCosmosDBSqlIncludedPath -Path $indexPathIncluded -Index $includedPathIndex

$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
    -IncludedPath $includedPath `
    -ExcludedPath $indexPathExcluded `
    -IndexingMode Consistent `
    -Automatic $true

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -IndexingPolicy $indexingPolicy

在索引关闭的情况下创建 Azure Cosmos DB 容器Create an Azure Cosmos DB container with indexing turned off

# Create an Azure Cosmos DB container with no indexing
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"

$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
    -IndexingMode None

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -IndexingPolicy $indexingPolicy

创建键策略和 TTL 都独一无二的 Azure Cosmos DB 容器Create an Azure Cosmos DB container with unique key policy and TTL

# Create a container with a unique key policy and TTL of one day
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$uniqueKeyPath = "/myUniqueKeyPath"
$ttlInSeconds = 86400 # Set this to -1 (or don't use it at all) to never expire

$uniqueKey = New-AzCosmosDBSqlUniqueKey `
    -Path $uniqueKeyPath

$uniqueKeyPolicy = New-AzCosmosDBSqlUniqueKeyPolicy `
    -UniqueKey $uniqueKey

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -UniqueKeyPolicy $uniqueKeyPolicy `
    -TtlInSeconds $ttlInSeconds

创建带冲突解决方案的 Azure Cosmos DB 容器Create an Azure Cosmos DB container with conflict resolution

若要将所有冲突都写入 ConflictsFeed,然后分别进行处理,请传递 -Type "Custom" -Path ""To write all conflicts to the ConflictsFeed and handle separately, pass -Type "Custom" -Path "".

# Create container with last-writer-wins conflict resolution policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionPath = "/myResolutionPath"

$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
    -Type LastWriterWins `
    -Path $conflictResolutionPath

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -ConflictResolutionPolicy $conflictResolutionPolicy

若要创建冲突解决策略以使用存储过程,请调用 New-AzCosmosDBSqlConflictResolutionPolicy 并传递参数 -Type-ConflictResolutionProcedureTo create a conflict resolution policy to use a stored procedure, call New-AzCosmosDBSqlConflictResolutionPolicy and pass parameters -Type and -ConflictResolutionProcedure.

# Create container with custom conflict resolution policy using a stored procedure
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionSprocName = "mysproc"

$conflictResolutionSproc = "/dbs/$databaseName/colls/$containerName/sprocs/$conflictResolutionSprocName"

$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
    -Type Custom `
    -ConflictResolutionProcedure $conflictResolutionSproc

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -ConflictResolutionPolicy $conflictResolutionPolicy

列出数据库中的所有 Azure Cosmos DB 容器List all Azure Cosmos DB containers in a database

# List all Azure Cosmos DB containers in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName

获取数据库中的单个 Azure Cosmos DB 容器Get a single Azure Cosmos DB container in a database

# Get a single Azure Cosmos DB container in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Get-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

删除 Azure Cosmos DB 容器Delete an Azure Cosmos DB container

# Delete an Azure Cosmos DB container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Remove-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

在 Azure Cosmos DB 容器上创建资源锁以防止删除Create a resource lock on an Azure Cosmos DB container to prevent delete

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"

New-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName `
    -LockLevel $lockLevel

删除 Azure Cosmos DB 容器上的资源锁Remove a resource lock on an Azure Cosmos DB container

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"

Remove-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName

后续步骤Next steps