简体   繁体   English

从Azure表存储中删除批处理时如何避免404

[英]How to avoid a 404 when deleting batches from Azure Table Storage

Problem 问题

I am trying to delete a lot of rows from table storage that may or may not exist. 我正在尝试从表存储中删除可能不存在的很多行。 The deal is I need to minimise I/O and maximise bandwidth so 1 hit to rule them all would be awesome. 要做的是我需要最小化I / O和最大化带宽,因此只要一按即可将它们全部都很棒。 Problem is that if any of the batch entities doesn't exist the whole batch fails. 问题是,如果不存在任何批处理实体,则整个批处理都会失败。

Why 为什么

This also brings me to a design question - why doesn't the request simply return a deletion result with indication of which objects were not deleted due to 404. Why does it throw an exception? 这也带给我一个设计问题-为什么请求不仅仅返回删除结果并指出由于404而没有删除哪些对象,为什么会引发异常? What is the reason for it. 这是什么原因。

More info 更多信息

The batch size is within Table Storage constraint of 100 and they are all within the same partition. 批处理大小在100个表存储约束内,并且它们都在同一分区内。

To answer your question, there's no way to avoid this kind of situation. 要回答您的问题,没有办法避免这种情况。 If an entity in the batch fails, the whole batch will fail. 如果批处理中的实体发生故障,则整个批处理将失败。

However there's one thing you could do: 但是,您可以做一件事:

When the batch fails, it returns the index of the failed entity. 批处理失败时,它将返回失败实体的索引。 What you could do is take that batch and create 3 separate batches out of that. 您可以做的就是取出该批次,然后从中创建3个单独的批次。 1st batch will be from first entity (0th index) to the index of the failed entity (minus one), 2nd will be the failed entity (so just one entity) and the last one would be from the failed entity index to the last entity. 第一批将从第一个实体(第0个索引)到失败实体的索引(减去一个),第二个将是失败实体(因此只有一个实体),最后一个将从失败实体索引到最后一个实体。 For the failed entity, you could simply try DeleteIfExists . 对于失败的实体,您可以简单地尝试DeleteIfExists So assuming you have 100 entities in a batch and let's say 30th entity fails, you would create 3 batches: 因此,假设您一个批次中有100个实体,并且假设第30个实体失败,那么您将创建3个批次:

Batch 1: 0th to 29th entity (Index 0 - 28) 批次1:第0至第29个实体(索引0-28)

Batch 2: 30th entity (single entity) (Index 29) 批次2:第30个实体(单个实体)(索引29)

Batch 3: 31st to 100th entity (Index 30 - 99) 批次3:第31至第100个实体(索引30-99)

This also brings me to a design question - why doesn't the request simply return a deletion result with indication of which objects were not deleted due to 404. Why does it throw an exception? 这也带给我一个设计问题-为什么请求不仅仅返回删除结果并指出由于404而没有删除哪些对象,为什么会引发异常? What is the reason for it. 这是什么原因。

One possible reason I could think of is because of Storage API's adherence to REST. 我想到的一个可能原因是因为Storage API坚持使用REST。 You try to delete a resource, it's not there so API would throw the error. 您尝试删除资源,因为它不存在,所以API会抛出错误。 Furthermore, an entity could fail to delete not only because the entity is not present but also because the if-match conditional header specified in the request does not match. 此外,实体可能无法删除,不仅因为该实体不存在,还因为请求中指定的if-match条件标头不匹配。 To elaborate, you may want to delete an entity only if eTag matches. 详细地说,您可能只想在eTag匹配时才删除实体。 In this case, even though the entity is present your delete operation would fail. 在这种情况下,即使存在实体,您的删除操作也会失败。 To deal with 404 errors on single entity delete operation, all client SDKs have implemented DeleteIfExists kind of functionality which will eat 404 error. 为了处理单个实体删除操作上的404错误,所有客户端SDK都实现了DeleteIfExists类型的功能,它将吞噬404错误。

You could PUT empty entities with the same PartitionKey and EntityKey before DELETEing them. 您可以先删除具有相同PartitionKey和EntityKey的空实体,然后再删除它们。 This way you'll be sure you won't have 404 errors. 这样,您将确保不会出现404错误。 It's two consistent calls for every batch instead of retrying many times and complicate the logic of your app. 每个批次有两个一致的调用,而无需重试多次,从而使应用程序的逻辑复杂化。 Not an ideal answer but we don't live in an ideal world :) 不是理想的答案,但我们没有生活在理想的世界中:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM