[英]DynamoDb: Delete all items having same Hash Key
Consider the following table: 请考虑下表:
Table (documentId : Hash Key, userId: Range Key)
How can I write a code to delete all the items having the same documentId
and preferably without retrieving the items. 如何编写代码来删除具有相同
documentId
所有项目,最好不检索项目。
Currently, You cannot delete all the items just by passing the Hash key, to delete an item it requires Hash + Range because that's what makes it unique. 目前,您不能仅通过传递哈希键来删除所有项目,以删除它需要哈希+范围的项目,因为这是使其独特的原因。
You have to know both your (hash + range) to delete the item.
Edit: Here is the reference link from DynamoDB documentation http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html#API_DeleteItem_RequestSyntax 编辑:以下是DynamoDB文档的参考链接http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html#API_DeleteItem_RequestSyntax
Please read the explanation of the "KEY" which clearly says that we must pass both Hash (Partition Key) and Range (Sort Key) to delete the item. 请阅读“KEY”的说明,清楚地说我们必须通过Hash(分区键)和Range(排序键)来删除项目。
If you want to delete only by hash key, you need to query records first and then use batchDelete
to delete all the records. 如果只想通过散列键删除,则需要先查询记录,然后使用
batchDelete
删除所有记录。
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 call out here that deleteItem
deletes only one item at a time and both hash key and range key needs to be specified for this. 我想在这里呼吁
deleteItem
只删除一个项目,并且需要为此指定散列键和范围键。
I have a similar requirement where I need to delete more than 10 million of rows from DynamoDB table. 我有类似的要求,我需要从DynamoDB表中删除超过1000万行。 I was hoping that there would be a way to delete all the items based on a specific partition key but unfortunately there is no way (atleast I couldn't find).
我希望有一种方法可以根据特定的分区键删除所有项目,但不幸的是没有办法(至少我找不到)。
It's painful to specify the specific values of hashkey and sortKey. 指定hashkey和sortKey的具体值是很痛苦的。 The only option is to scan the table to retrieve primary key (or composite key) and then iterate over it to delete a single item using deleteItem API.
唯一的选择是扫描表以检索主键(或复合键),然后迭代它以使用deleteItem API删除单个项。
You can delete upto 25 items ( as large as 400KB) in a single call using BatchWriteItem
API. 您可以使用
BatchWriteItem
API在一次调用中删除最多25个项目(最大为400KB) 。
You can refer to this API from AWS for more information : https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html 您可以从AWS引用此API以获取更多信息: https : //docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.