简体   繁体   English

使用 powershell 获取 Cosmosdb 容器集合项

[英]Get Cosmosdb Container Collection items using powershell

Team, I had created new CosmosDB account in Azure portal with a container contains list of collection items.团队,我在 Azure 门户中创建了新的 CosmosDB 帐户,其中包含一个包含集合项列表的容器。 I am able to access Container details in power shell script.我能够在 power shell 脚本中访问容器详细信息。

How to list collection items or show specific collection item using partition key using power shell script如何使用 power shell 脚本使用分区键列出集合项或显示特定集合项

Power shell Script:电源 shell 脚本:

  Get-AzResource -ResourceType "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers" -ApiVersion "2020-03-01" -ResourceGroupName "testRG" -Name "cosmosaccount1/database1/containercollection1"

You would need to use something like a thirdparty module for this.为此,您需要使用第三方模块之类的东西。 Azure Resource Manager doesnt support that, hence you need to talk to Cosmos DB directly. Azure 资源管理器不支持该功能,因此您需要直接与 Cosmos DB 联系。

https://github.com/PlagueHO/CosmosDB https://github.com/PlagueHO/CosmosDB

The Cosmos DB repo has a set of examples to use Powershell: https://github.com/Azure/azure-cosmos-do.net-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/PowerShellRestApi Cosmos DB 存储库有一组使用示例 Powershell: https://github.com/Azure/azure-cosmos-do.net-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/PowerShellRestApi

Particularly to read Items: https://github.com/Azure/azure-cosmos-do.net-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/PowerShellRestApi/PowerShellScripts/ReadItem.ps1特别要阅读的项目: https://github.com/Azure/azure-cosmos-do.net-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/PowerShellRestApi/PowerShellScripts/ReadItem.ps1

They are all using the REST API to do REST request, in this case, it is an authenticated GET to https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs/{id} (where databaseaccount is your account name, db-id is the id of your database, coll-id is the id of your collection/container, and id is your document id).他们都使用 REST API 来执行 REST 请求,在这种情况下,它是一个经过身份验证的GEThttps://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs/{id} (其中databaseaccount是您的帐户名, db-id是您的数据库的 ID, coll-id是您的集合/容器的 ID, id是您的文档 ID)。 It is also setting the x-ms-documentdb-partitionkey header for the partition key.它还为分区键设置x-ms-documentdb-partitionkey header。

Like @4c74356b41 has indicated, you can use the CosmosDB module, which is now part of the official Az module .正如 @4c74356b41 所指出的,您可以使用 CosmosDB 模块,它现在是官方 Az 模块的一部分。

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

You can see the available commands with Get-Commands:您可以使用 Get-Commands 查看可用的命令:

Import-Module Az
Import-Module -Name CosmosDB
Get-Command -Module CosmosDB

Get all items in a collection获取集合中的所有项目

In order to get all entries inside a container, we use the Get-CosmosDbDocument command:为了获取容器内的所有条目,我们使用Get-CosmosDbDocument命令:

$subscription = "SubscriptionName"
$resourceGroupName = "ResourceGroupName"
$accountName = "AzureCosmosDBAccount"
$databaseName = "DatabaseName"
$cosmosContainer = "TargetCosmosDBContainer"

Set-AzContext $subscription
$backOffPolicy = New-CosmosDbBackoffPolicy -MaxRetries 5 -Method Additive -Delay 1000
$cosmosDbContext = New-CosmosDbContext -Account $accountName -Database
$databaseName -ResourceGroup $resourceGroupName -BackoffPolicy $backOffPolicy

$documentsPerRequest = 100
$continuationToken = $null
$documents = $null
do {
    $responseHeader = $null
    $getCosmosDbDocumentParameters = @{
        Context = $cosmosDbContext
        CollectionId = $cosmosContainer
        MaxItemCount = $documentsPerRequest
        ResponseHeader = ([ref] $responseHeader)
    }

    if ($continuationToken) {
        $getCosmosDbDocumentParameters.ContinuationToken = $continuationToken
    }

    $documents += Get-CosmosDbDocument @getCosmosDbDocumentParameters
    $continuationToken = Get-CosmosDbContinuationToken -ResponseHeader $responseHeader
} while (-not [System.String]::IsNullOrEmpty($continuationToken))

Note: There is no apparent limitation on the number of documents that can be retrieved with this command, but it stands to reason that the command will have the API limitation, and this is 4 MB (as documented here ).注意:对于使用此命令可以检索的文档数量没有明显限制,但按理说该命令将具有 API 限制,这是 4 MB(如此处记录)。 The value here ($documentsPerRequest = 100) could prove to be either too big or too small, depending on the size of each document.此处的值 ($documentsPerRequest = 100) 可能太大或太小,具体取决于每个文档的大小。 I usually don't use this parameter, but I've mentioned it here in case someone needs it.我通常不使用这个参数,但我在这里提到它以防有人需要它。

List specific collection item列出特定的收集项目

To get a specific entry or group of entries from a container, we use the same Get-CosmosDbDocument command, in a slightly different way:要从容器中获取特定条目或条目组,我们使用相同的Get-CosmosDbDocument命令,但方式略有不同:

$query = "SELECT * FROM c WHERE c.property = 'propertyValue'"
$documents = Get-CosmosDbDocument -Context $cosmosDbContext -CollectionId $cosmosContainer -Query $query -QueryEnableCrossPartition $true

Note: For brevity I haven't went to the process of getting a continuation token, but if the query will return a result that is larger than 4 MB, then we will only receive the first part of the response.注意:为简洁起见,我没有去获取延续令牌的过程,但如果查询将返回大于 4 MB 的结果,那么我们将只收到响应的第一部分。 To make sure this does not happen we should add "Query" and "QueryEnableCrossPartition" in the $getCosmosDbDocumentParameters dictionary.为确保不会发生这种情况,我们应该在 $getCosmosDbDocumentParameters 字典中添加“Query”和“QueryEnableCrossPartition”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTPS 连接到 Linux Docker 容器中的 CosmosDb 模拟器的问题 - Issue with HTTPS connection to CosmosDb Emulator in Linux Docker Container 使用 flutter 从 firebase 获取集合列表 - Get list of collection from firebase using flutter 使用 PowerShell 从 AzureAD 获取信息 - Using PowerShell to get info from AzureAD 使用 CosmosDB 作为 Golang 的键值对存储 - Using CosmosDB as a key-value store with Golang 如何使用 Pyspark 删除 CosmosDB 顶点 - How to Delete a CosmosDB Vertex using Pyspark 如何使用 AzCopy 工具将数据从 Azure CosmosDb 复制到本地? - How Copy the Data from Azure CosmosDb to local using AzCopy tool? 使用 ReactJS 通过 UID 从用户的“数据”集合中仅获取 1 个文档 - Get only 1 document from 'data' collection of the user by UID using ReactJS 使用 web 应用程序获取容器中所有 blob 的列表 Etag 值 - Get list Etag Values for all the blobs in container using web app 集合以排除 azure 数据工厂中的数组项 - Collection to exclude array items in azure data factory 使用 dt.exe 命令行迁移 Cosmosdb - Cosmosdb migration using dt.exe command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM