[英]Azure storage table delete row by row key
I am trying to delete row from azure storage filter by only rowkey value.我正在尝试仅通过 rowkey 值从 azure 存储过滤器中删除行。 But I dont see any overload for delete operation where we can filter with only rowkey.但是我没有看到删除操作有任何过载,我们只能使用 rowkey 进行过滤。 Is there any alternative option to delete row from azure storage table for records with specific rowkey?对于具有特定 rowkey 的记录,是否有任何替代选项可以从 azure 存储表中删除行?
RemoveEntityByRowKey('123456');
public static void RemoveEntityByRowKey(string myRowKey)
{
try
{
CloudTable table = _tableClient.GetTableReference("MyAzureTable");
TableOperation delteOperation = TableOperation.Delete(myRowKey);
table.Execute(delteOperation);
}
catch (Exception ex)
{
LogError.LogErrorToAzure(ex);
throw;
}
}
In order to delete an entity, you would need both PartitionKey
and RowKey
( Delete Entity REST API
).为了删除实体,您需要同时使用PartitionKey
和RowKey
( Delete Entity REST API
)。 So what you would need to do is first fetch the entity with matching RowKey
.因此,您需要做的是首先获取具有匹配RowKey
的实体。 Once you have fetched this entity, you should be able to call TableOperation.Delete
as mentioned in the answers.获取此实体后,您应该能够调用TableOperation.Delete
,如答案中所述。
However, fetching entity by RowKey
is not recommended because it will do a Full Table Scan .但是,不推荐通过RowKey
获取实体,因为它会进行全表扫描。 It may not be a problem if your table size is small but would be an issue where your table contains large number of entities.如果您的表很小,这可能不是问题,但如果您的表包含大量实体,这将是一个问题。 Furthermore, a RowKey
is unique in a Partition
ie in a table there can be only one entity with a PartitionKey
/ RowKey
combination.此外, RowKey
在Partition
中是唯一的,即在一张表中只能有一个具有PartitionKey
/ RowKey
组合的实体。 In other words, you can potentially have entities with same RowKey
in different Partitions
.换句话说,您可能在不同的Partitions
中拥有具有相同RowKey
的实体。 So when you fetch entities by RowKey
only, you may get more than one entity back.因此,当您仅通过RowKey
获取实体时,您可能会返回多个实体。 You need to ensure that you're deleting correct entity.您需要确保删除的是正确的实体。
If you know the PartitionKey as well as the RowKey, you don't need to retrieve the entire entity to delete it.如果您知道 PartitionKey 和 RowKey,则无需检索整个实体即可将其删除。 You could adapt your code as follows:您可以按如下方式调整代码:
public static void RemoveEntityByRowKey(string myRowKey)
{
try
{
var entity = new YourEntity
{
PartitionKey = "partition",
RowKey = myRowKey,
ETag = "*"
}
CloudTable table = _tableClient.GetTableReference("MyAzureTable");
TableOperation delteOperation = TableOperation.Delete(entity);
table.Execute(delteOperation);
}
catch (Exception ex)
{
LogError.LogErrorToAzure(ex);
throw;
}
}
If you're targeting .NET Core, you'll need to use the ExecuteQuerySegmentedAsync
method to execute the filter condition query.如果您的目标是 .NET Core,则需要使用ExecuteQuerySegmentedAsync
方法来执行过滤条件查询。 ExecuteQuery
is deprecated.不推荐使用ExecuteQuery
。
var cloudTableClient = _cloudStorageAccount.CreateCloudTableClient();
var myTable = cloudTableClient.GetTableReference("MyTable");
var query = new TableQuery<MyEntity>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "myRowKey"));
var segment = await myTable.ExecuteQuerySegmentedAsync(query, null);
var myEntities = segment.Results;
By row do you mean a record?行是指记录吗?
TableOperation.Delete accepts a Table Entity. TableOperation.Delete 接受表实体。 See here: https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tableoperation.delete.aspx请参阅此处: https ://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tableoperation.delete.aspx
In order to delete that entity, you must first retrieve it by specifying its Partition
key and/or Row
key.为了删除该实体,您必须首先通过指定其Partition
键和/或Row
键来检索它。
Look into TableQuery class here https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tablequery_methods.aspx在此处查看 TableQuery 类https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tablequery_methods.aspx
Once you retrieve it, pass it to Delete method.检索它后,将其传递给 Delete 方法。
There is no method没有方法
TableOperation.Delete(String rowKey)
, TableOperation.Delete(String rowKey)
,
only method唯一的方法
public static TableOperation delete(final TableEntity entity)
in TableOperation.在表操作中。 For details, see Get started with Azure Table storage using .NET有关详细信息,请参阅使用 .NET 开始使用 Azure 表存储
With reference to Franks pointers, I am posting answer so that it would be useful to others who face the similar issue.参考弗兰克斯的指针,我发布了答案,以便对面临类似问题的其他人有用。
RemoveEntityByRowKey('123456');
public static void RemoveEntityByRowKey(string myRowKey)
{
try
{
CloudTable table = _tableClient.GetTableReference("MyAzureTable");
TableQuery<myEntity> query = new TableQuery<myEntity>()
.Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, myRowKey));
foreach (var item in table.ExecuteQuery(query))
{
var oper = TableOperation.Delete(item);
table.Execute(oper);
}
}
catch (Exception ex)
{
LogErrorToAzure(ex);
throw;
}
}
mpl is my table row entity and is required to delete the record from the db. mpl 是我的表行实体,需要从数据库中删除记录。 I have added this answer to show an async (with result check)我添加了这个答案以显示异步(带有结果检查)
if (result)
{
//delete the lead from the storage table
TableOperation delRow = TableOperation.Delete(mpl);
TableResult tr = await table.ExecuteAsync(delRow);
if (((int)tr.HttpStatusCode) < 400)
log.LogInformation($"Table Record: {mpl.RowKey} deleted");
else
log.LogError($"Table Record: {mpl.RowKey} NOT deleted");
}
The Windows.Azure.Storage
namespace is now deprecated in favour of the Azure.Data.Tables
namespace. Windows.Azure.Storage
命名空间现已弃用,取而代之的是Azure.Data.Tables
命名空间。 As such the TableOperation.Delete
method is also deprecated .因此TableOperation.Delete
方法也已弃用。 You should now use a TableClient
and it's DeleteEntity
method:您现在应该使用TableClient
及其DeleteEntity
方法:
TableClient tableClient = new TableClient(connectionString, Table);
tableClient.DeleteEntity(PartitionKey, RowKey);
There is also the async version DeleteEntityAsync
if you wish too.如果您愿意,也可以使用异步版本DeleteEntityAsync
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.