【问题标题】:how to delete records out of table storage within a certain date range?如何在某个日期范围内从表存储中删除记录?
【发布时间】:2022-01-18 17:51:50
【问题描述】:

我希望能够从表存储中删除在 2020 年 11 月 15 日到 2020 年 12 月 15 日之间具有TimeStamp 的记录。

例如:

我们如何使用powershell删除指定日期范围内的表存储中的记录?

这是我尝试过的:

$storageAccountName = "mystorageaccount"
$resourceGroup = "myresourcegroup"
$tableName = "FilesReceived"
$columnName = "Timestamp"
$value = "datetime'2021-09-15T00:00:00Z'"

$storageAccount = Get-AzStorageAccount `
  -ResourceGroupName $resourceGroup `
  -Name $storageAccountName
$ctx = $storageAccount.Context 

$storageTable = Get-AzStorageTable –Name $tableName –Context $ctx
$cloudTable = ($storageTable).CloudTable

[string]$filter = `
  [Microsoft.Azure.Cosmos.Table.TableQuery]::GenerateFilterCondition($columnName,`
  [Microsoft.Azure.Cosmos.Table.QueryComparisons]::LessThan,$value)

# Get entity
$entityToDelete = Get-AzTableRow `
    -table $cloudTable `
    -customFilter $filter

$entityToDelete | Remove-AzTableRow -table $cloudTable

但是,当我检查$entityToDelete 的内容时,它完全是空的。

【问题讨论】:

    标签: powershell azure-table-storage azure-runbook


    【解决方案1】:

    我们如何在指定日期内从表存储中删除记录 范围?

    与只执行删除查询的关系数据库不同,在表存储中,您首先需要获取要删除的实体,然后发送删除这些实体的请求。

    要获取实体,您需要执行类似Timestamp ge datetime'2021-11-15T00:00:00Z' and Timestamp le '2021-12-15T00:00:00Z' 的查询。您可以进行查询投影,只获取PartitionKeyRowKey 属性,因为删除时只需要这两个属性。

    获取实体后,您可以分别为每个实体发送删除请求,或执行实体批量事务以在单个请求中删除最多 100 个实体。

    【讨论】:

    • 很想知道使用哪个删除命令。我已经下载了最新的 powershell 版本
    • 我已经更新了问题并添加了我尝试运行的代码
    • gaurav,你在吗?
    • @AlexGordon - 我运行了您的代码并成功删除了数据。你能分享$filter的输出吗?我在我的代码中对查询进行了硬编码。这是我用来获取实体的代码:$data = Get-AzTableRow -table $cloudTable -customFilter "Timestamp ge datetime'2010-09-15T00:00:00Z'" -Top 1 -SelectColumn "PartitionKey,RowKey"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多