批量删除cosmos DB中的所有项目:404 not found
Deleting all items from cosmos DB in batch: 404 not found
我有以下代码:
CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connStr, options);
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
await VesselsContainer.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("name"));
or
await VesselsContainer.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("XXXa Soya"));
当 运行 代码我得到:
code does not indicate success: NotFound (404); Substatus: 0;
ActivityId: ; Reason: ();'
任何指示如何解决这个问题?
Partion key: /name
这是我的文档:
代码已亲自测试,您可以直接将其复制到您的项目中并运行它。
item的格式如图所示
using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Cosmos;
using System.Linq;
namespace CosmosGettingStartedTutorial
{
class Program
{
// <Main>
public static async Task Main(string[] args)
{
try
{
Console.WriteLine("Beginning operations...\n");
CosmosClient client = new CosmosClient("https://localhost:8081/", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
Database database = await client.CreateDatabaseIfNotExistsAsync("Items");
Container container = database.GetContainer("test");
// Query for an item
FeedIterator <response> feedIterator = container.GetItemQueryIterator<response>("SELECT * FROM c");
List<Task> concurrentDeleteTasks = new List<Task>();
while (feedIterator.HasMoreResults)
{
FeedResponse<response> res = await feedIterator.ReadNextAsync();
foreach (var item in res)
{
concurrentDeleteTasks.Add(container.DeleteItemAsync<response>(item.id, new PartitionKey(item.adress)));
}
}
await Task.WhenAll(concurrentDeleteTasks.Take(3));
}
catch (CosmosException de)
{
Exception baseException = de.GetBaseException();
Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
}
finally
{
Console.WriteLine("End of demo, press any key to exit.");
Console.ReadKey();
}
}
public class response
{
public string id { get; set; }
public string adress { get; set; }
public string _rid { get; set; }
public string _self { get; set; }
public string _etag { get; set; }
public string _attachments { get; set; }
public long _ts { get; set; }
}
}
}
确保您使用了正确的容器。
你的情况
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
await container.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("XXXa Soya"));
而不是
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
await VesselsContainer.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("XXXa Soya"));
我有以下代码:
CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connStr, options);
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
await VesselsContainer.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("name"));
or
await VesselsContainer.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("XXXa Soya"));
当 运行 代码我得到:
code does not indicate success: NotFound (404); Substatus: 0; ActivityId: ; Reason: ();'
任何指示如何解决这个问题?
Partion key: /name
这是我的文档:
代码已亲自测试,您可以直接将其复制到您的项目中并运行它。
item的格式如图所示
using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Cosmos;
using System.Linq;
namespace CosmosGettingStartedTutorial
{
class Program
{
// <Main>
public static async Task Main(string[] args)
{
try
{
Console.WriteLine("Beginning operations...\n");
CosmosClient client = new CosmosClient("https://localhost:8081/", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
Database database = await client.CreateDatabaseIfNotExistsAsync("Items");
Container container = database.GetContainer("test");
// Query for an item
FeedIterator <response> feedIterator = container.GetItemQueryIterator<response>("SELECT * FROM c");
List<Task> concurrentDeleteTasks = new List<Task>();
while (feedIterator.HasMoreResults)
{
FeedResponse<response> res = await feedIterator.ReadNextAsync();
foreach (var item in res)
{
concurrentDeleteTasks.Add(container.DeleteItemAsync<response>(item.id, new PartitionKey(item.adress)));
}
}
await Task.WhenAll(concurrentDeleteTasks.Take(3));
}
catch (CosmosException de)
{
Exception baseException = de.GetBaseException();
Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
}
finally
{
Console.WriteLine("End of demo, press any key to exit.");
Console.ReadKey();
}
}
public class response
{
public string id { get; set; }
public string adress { get; set; }
public string _rid { get; set; }
public string _self { get; set; }
public string _etag { get; set; }
public string _attachments { get; set; }
public long _ts { get; set; }
}
}
}
确保您使用了正确的容器。 你的情况
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
await container.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("XXXa Soya"));
而不是
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
await VesselsContainer.DeleteItemAsync<CoachVessel>("81c91c3a4f4841529bf36badfbb4a2ba", new PartitionKey("XXXa Soya"));