如何基于哈希键删除Amazon Dynamodb中的记录? [英] How to delete records in Amazon Dynamodb based on a hashkey?

查看:98
本文介绍了如何基于哈希键删除Amazon Dynamodb中的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to delete all items from an aws dynamodb table by hashkey. I see alot of discussion about it on the internet but no actual code samples and all my attempts have failed.

我在这里做错了什么,或者是否有更好的解决方案?

What am I doing wrong here or is there a better apporach altogether?

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);


推荐答案

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

Problem solved using the AWS batch write functionality. I chop mine into batches of 25 but I think the api can take up to 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);

这篇关于如何基于哈希键删除Amazon Dynamodb中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆