问题
I've been searching and searching for a way to delete items with the same ID by a range condition. None of the overloads for Table.DeleteItem seem to take a range condition, only a range.
Is there some other approach to deleting all entries older than an hour?
I'd prefer to not not have to retrieve all the matching hashkey rows and manually delete each row by the specific range key.
I'm primarily using the .NET SDK but any hints appreciated.
I see some batch write examples, but nothing that takes a whole range.
private static void SingleTableBatchWrite()
{
Table productCatalog = Table.LoadTable(client, "ProductCatalog");
var batchWrite = productCatalog.CreateBatchWrite();
var book1 = new Document();
book1["Id"] = 902;
book1["Title"] = "My book1 in batch write using .NET Helper API";
book1["ISBN"] = "902-11-11-1111";
book1["Price"] = 10;
book1["ProductCategory"] = "Book";
book1["Authors"] = new List<string> { "Author 1", "Author 2", "Author 3" };
book1["Dimensions"] = "8.5x11x.5";
batchWrite.AddDocumentToPut(book1);
// Specify delete item using overload that takes PK.
batchWrite.AddKeyToDelete(12345);
Console.WriteLine("Performing batch write in SingleTableBatchWrite()");
batchWrite.Execute();
}
回答1:
This functionality doesn't exist in DynamoDB.
The best you can do is Query for the appropriate items and manually delete each retrieved key. You can save on the bandwidth by asking for only your hash and range key attributes when querying, but note that this doesn't save on read capacity; DynamoDB will still charge you for the full size of the item when returning a subset of its attributes.
Some people have tackled this temporal problem by making a separate table for each time period (more generally a day than an hour). That way you can do some interesting things:
- Delete old data "for free" by dropping the table
- Scale today's table higher than the rest
This "data segregation" isn't as bad as it seems due to DynamoDB's model. Need to get an item? You already have the temporal range-key, so you know which table to ask. Need to query? Your Range condition must be time-based, so you know which tables to ask.
来源:https://stackoverflow.com/questions/15167441/delete-rows-by-id-and-range-condition