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

以下指南介绍了如何使用 Powershell 通过脚本来自动管理 Azure Cosmos DB 资源,其中包括帐户、数据库、容器和吞吐量。The following guide describes how to use PowerShell to script and automate management of Azure Cosmos DB resources, including account, database, container, and throughput.

备注

本文中的示例使用 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 PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

入门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 帐户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"
$locations = @("China North 2", "China East 2")
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000

New-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Location $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:数据库帐户的区域,从写入区域开始,按故障转移优先级排序。$locations The regions for the database account, starting with the write region and ordered by failover priority.
  • $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. 有关如何为 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 帐户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-master

备注

不能同时添加或删除区域 (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"
$locations = @("China North 2", "China East 2")
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "Session"
$enableAutomaticFailover = $true

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

# Add a region to the account
$locations2 = @("China North 2", "China East 2", "China East")
$locationObjects2 = @()
$i = 0
ForEach ($location in $locations2) {
    $locationObjects2 += @{ locationName = "$location"; failoverPriority = $i++ }
}

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

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 a region from the account
$locations3 = @("China North 2", "China East")
$locationObjects3 = @()
$i = 0
ForEach ($location in $locations3) {
    $locationObjects3 += @{ locationName = "$location"; failoverPriority = $i++ }
}

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

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-master on an account
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableAutomaticFailover:$enableAutomaticFailover

# Now enable multi-master
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 master 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-master - cannot have both automatic
# failover and multi-master 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

获取帐户中的所有 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

使用自定义索引策略创建 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