繁体   English   中英

Windows Azure 表存储中的批量删除

[英]Batch delete in Windows Azure Table Storage

我正在尝试删除WADLogsTable 2 个日期(即 2 个分区键)之间的所有实体。 到目前为止,我找到的最好的代码是这个(来自https://www.wintellect.com/deleting-entities-in-windows-azure-table-storage ):

private static void DeleteAllEntitiesInBatches(CloudTable table, Expression<Func<DynamicTableEntity, bool>> filters)
        {
            Action<IEnumerable<DynamicTableEntity>> processor = entities =>
            {
                var batches = new Dictionary<string, TableBatchOperation>();

                foreach (var entity in entities)
                {
                    TableBatchOperation batch = null;

                    if (batches.TryGetValue(entity.PartitionKey, out batch) == false)
                    {
                        batches[entity.PartitionKey] = batch = new TableBatchOperation();
                    }

                    batch.Add(TableOperation.Delete(entity));

                    if (batch.Count == 100)
                    {
                        table.ExecuteBatch(batch);
                        batches[entity.PartitionKey] = new TableBatchOperation();
                    }
                }

                foreach (var batch in batches.Values)
                {
                    if (batch.Count > 0)
                    {
                        table.ExecuteBatch(batch);
                    }
                }
            };

            ProcessEntities(table, processor, filters);
        }

        private static void ProcessEntities(CloudTable table, Action<IEnumerable<DynamicTableEntity>> processor, Expression<Func<DynamicTableEntity, bool>> filters)
        {
            TableQuerySegment<DynamicTableEntity> segment = null;

            while (segment == null || segment.ContinuationToken != null)
            {
                if (filters == null)
                {
                    segment = table.ExecuteQuerySegmented(new TableQuery().Take(100), segment == null ? null : segment.ContinuationToken);
                }
                else
                {
                    var query = table.CreateQuery<DynamicTableEntity>().Where(filters).Take(100).AsTableQuery();
                    segment = query.ExecuteSegmented(segment == null ? null : segment.ContinuationToken);
                }

                processor(segment.Results);
            }
        }

但我不知道我应该传递什么作为“过滤器”参数。 我想出了这个相当幼稚的尝试:

Expression<Func<DynamicTableEntity, bool>> filters = e => long.Parse(e.PartitionKey) >= startTicks && long.Parse(e.PartitionKey) <= endTicks;

但这不起作用。 在运行时,我收到以下错误:

不支持表达式 ((Parse([10007].PartitionKey) >= 635109048000000000) And (Parse([10007].PartitionKey) <= 635115960000000000))。

我不知道我的问题是否与 Azure 表或表达式有关,但任何帮助将不胜感激。

编辑:如果需要,我很乐意提供更多信息。

我在另一个论坛上得到了答案:

var startTicks = string.Format("{0:0000000000000000000}", start);
var endTicks = string.Format("{0:0000000000000000000}", end);
Expression<Func<DynamicTableEntity, bool>> filters = e => e.PartitionKey.CompareTo(startTicks) >= 0 && e.PartitionKey.CompareTo(endTicks) <= 0;

暂无
暂无

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

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