如何基于哈希键删除Amazon Dynamodb中的记录
本文关键字:记录 Dynamodb Amazon 何基于 哈希键 删除 | 更新日期: 2023-09-27 18:12:59
我试图通过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);
使用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);