使用PowerShell(SQL API)在Cosmos DB中插入文档

时间:2018-08-16 11:09:50

标签: powershell azure-cosmosdb

尝试在具有分区密钥的Cosmos DB集合中插入数据时出现错误。

没有分区键,可以正常工作

$resourceGroupName = "myrg"
$cosmosDbAccountName = "mydb"
$databaseName = "test"

$cosmosDbContext = New-CosmosDbContext -Account $cosmosDbAccountName -        
Database $databaseName -ResourceGroup $resourceGroupName    

New-CosmosDbCollection -Context $cosmosDbContext -Id 'events' -OfferThroughput 1000 -PartitionKey 'RuleType' -DefaultTimeToLive 604800

$document = @"
{
        "id": "$([Guid]::NewGuid().ToString())",
        "createTime": "2018-05-21T22:59:59.999Z",
        "RuleType": "FTOD"
    }

"@
New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'events' - 
DocumentBody $document  -PartitionKey 'RuleType'
  

“ RuleType”:这是我的分区键

enter image description here

Invoke-WebRequest:远程服务器返回错误:(400)错误的请求。 在C:\ Program Files \ WindowsPowerShell \ Modules \ CosmosDB \ 2.1.4.536 \ lib \ utils.ps1:554 char:30 + ... estResult =调用WebRequest -UseBasicParsing @invokeWebRequestParam ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~     + CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-WebRequest],WebException     + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

有人知道吗?

1 个答案:

答案 0 :(得分:0)

我转载了您的问题。

enter image description here

基于Get-Help New-CosmosDbDocument命令显示的参数列表:

enter image description here

您需要添加partitionkey,才能成功插入文档。

$resourceGroupName = "***"
$cosmosDbAccountName = "***"
$databaseName = "db"

$cosmosDbContext = New-CosmosDbContext -Account $cosmosDbAccountName -Database $databaseName -ResourceGroup $resourceGroupName    

$document = @"
{
        "id": "6fa9b3d5-ce1a-4b38-9068-9d17de5b1c69",
        "createTime": "2018-05-21T22:59:59.999Z",
        "RuleType": "FTOD"           
    }

"@
$partitionkey = "FTOD"
New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'part' -DocumentBody $document -PartitionKey $partitionkey

希望它对您有帮助。