DynamoDb: delete all items that have the same hash key

Consider the following table:

Table (documentId : Hash Key, userId: Range Key) 

How to write code to delete all elements with the same documentId and, preferably, without extracting elements.

+8
source share
3 answers

Currently, you cannot delete all elements by simply passing the Hash key; to delete an element, Hash + Range is required because this makes it unique.

 You have to know both your (hash + range) to delete the item. 

Edit: here is the link to the documentation from DynamoDB http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html#API_DeleteItem_RequestSyntax

Please read the β€œKEY” explanation, which clearly states that we must pass both a Hash (section key) and Range (sort key) to remove the item.

+11
source

If you want to delete only the hash key, you need to first query the records and then use batchDelete to delete all the records.

 HashMap<String, AttributeValue> eav = new HashMap<String, AttributeValue>(); eav.put(":v1", new AttributeValue().withS(value)); DynamoDBQueryExpression<DocumentTable> queryExpression = new DynamoDBQueryExpression<DocumentTable>() .withKeyConditionExpression("documentId = :v1") .withExpressionAttributeValues(eav); List<DocumentTable> ddbResults = dynamoDBMapper.query(DocumentTable.class, queryExpression); dynamoDBMapper.batchDelete(ddbResults); 

I would like to say here that deleteItem deletes only one element at a time, and for this you need to specify both a hash key and a range key.

+1
source

I have a similar requirement when I need to delete more than 10 million rows from a DynamoDB table. I was hoping there would be a way to remove all elements based on a specific section key, but unfortunately there is no way (at least I could not find).

It hurts to specify specific hashkey and sortKey values. The only option is to scan the table to get the primary key (or composite key), and then iterate over it to delete one item using the deleteItem API.

You can remove up to 25 elements (up to 400 KB in size) in a single call with the BatchWriteItem API.

You can refer to this API from AWS for more information: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

0
source

Source: https://habr.com/ru/post/1238051/


All Articles