2

我正在尝试通过 hashkey 从 aws dynamodb 表中删除所有项目。我在互联网上看到很多关于它的讨论,但没有实际的代码示例,我所有的尝试都失败了。

我在这里做错了什么还是有更好的方法?

List<Document> results = null;
var client = new AmazonDynamoDBClient("myamazonkey", "myamazonsecret");
var table = Table.LoadTable(client, "mytable");

var search = table.Query(new Primitive("hashkey"), new RangeFilter());
do {
results = search.GetNextSet();
search.Matches.Clear();
foreach (var r in results)
                    {
                        client.DeleteItem(new DeleteItemRequest()
                            {
                                Key = new Key() { HashKeyElement = new AttributeValue() { S = "hashkey"}, RangeKeyElement = new AttributeValue() { N = r["range-key"].AsString() } }
                            });
                    }
} while(results.Count > 0);
4

1 回答 1

2

使用 AWS 批量写入功能解决了问题。我将我的分成 25 个批次,但我认为 api 最多可以占用 100 个。

List<Document> results = null;
var client = new AmazonDynamoDBClient("myamazonkey", "myamazonsecret");
var table = Table.LoadTable(client, "mytable");
var batchWrite = table.CreateBatchWrite();
var batchCount = 0;

var search = table.Query(new Primitive("hashkey"), new RangeFilter());
do {
  results = search.GetNextSet();
  search.Matches.Clear();
  foreach (var document in results)
  {
    batchWrite.AddItemToDelete(document);
    batchCount++;
    if (batchCount%25 == 0)
    {
      batchCount = 0;
      try
      {
        batchWrite.Execute();
      }
      catch (Exception exception)
      {
       Console.WriteLine("Encountered an Amazon Exception {0}", exception);
      }

      batchWrite = table.CreateBatchWrite();
     }
   }
   if (batchCount > 0) batchWrite.Execute();
  }
} while(results.Count > 0);
于 2013-05-14T20:52:26.967 回答