azure-cosmosdb - 插入文档时响应状态码不表示成功
问题描述
我的容器上有以下分区 ID:/vesselId
我正在尝试添加此对象的集合:
public class CoachVessel
{
[JsonProperty("id")]
public string vesselId { get; set; }
[JsonProperty("imo")]
public long Imo { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
这是我批量插入文档的代码:
CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connStr, options);
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
List<Task> concurrentTasks = new List<Task>();
foreach (var vessel in vessels.Take(1))
{
concurrentTasks.Add(container.CreateItemAsync(vessel, new PartitionKey(vessel.vesselId)));
}
await Task.WhenAll(concurrentTasks);
我收到以下没有提供太多信息的错误?
Microsoft.Azure.Cosmos.CosmosException: '响应状态码不表示成功:BadRequest (400); 子状态:1001;活动ID:;原因: ();'
删除文档时遇到同样的问题:
CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true, MaxRetryAttemptsOnRateLimitedRequests=1000};
CosmosClient cosmosclient = new CosmosClient(connStr, options);
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
var allItemsQuery = container.GetItemQueryIterator<string>("SELECT * FROM c.id");
List<Task> concurrentDeleteTasks = new List<Task>();
while (allItemsQuery.HasMoreResults)
{
foreach (var item in await allItemsQuery.ReadNextAsync())
{
concurrentDeleteTasks.Add(container.DeleteItemAsync<string>(item, new PartitionKey("id")));
}
}
await Task.WhenAll(concurrentDeleteTasks.Take(3));
抛出以下错误:'响应状态码不表示成功:NotFound (404); 子状态:0;
解决方案
分区键必须与文档正文中的属性匹配。将容器的分区键更改为,/id
并修复删除代码以正确指定分区键。例如,
concurrentDeleteTasks.Add(container.DeleteItemAsync<string>(item, new PartitionKey(item.Id)));
推荐阅读
- java - Apache Storm 本地集群“无法规范化地址”
- java - r2dbc ReactiveCrudRepository 不断崩溃
- python - 捕获信号后如何终止 time.sleep()?
- mule - Mulesoft 批处理多个连接器
- javascript - 如何将变量分配给对象以使用 reactjs 提交数据?
- java - Gradle + AspectJ 日志跟踪在“检测”期间尝试重新编译项目并失败
- r - R中“arrayInd()”的逆是什么?
- flutter - 使用 fl_chart 包在线性图表中绘制底部标题时导致错误的原因
- node.js - 将 Azure Text to Speech 输出加载到 Azure Blob
- php - 如何检查 YouTube 上是否存在频道?