java DynamoDb:删除所有具有相同哈希键的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34259358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DynamoDb: Delete all items having same Hash Key
提问by Hans
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
且最好不检索项目的项目。
回答by Harshal Bulsara
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.
目前,您不能仅通过传递 Hash 键来删除所有项目,要删除一个项目,它需要 Hash + Range,因为这是使其唯一的原因。
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”的解释,它清楚地说明我们必须同时传递哈希(分区键)和范围(排序键)才能删除项目。
回答by alok698
If you want to delete only by hash key, you need to query records first and then use batchDelete
to delete all the records.
如果只想通过hash key删除,需要先查询记录,再使用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
只删除一个项目,并且需要为此指定散列键和范围键。
回答by Sagar Jani
I have a similar requirement where I need to delete more than 10 million of rows from DynamoDB table. 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).
我有一个类似的需求,我需要从 DynamoDB 表中删除超过 1000 万行。我希望有一种方法可以删除基于特定分区键的所有项目,但不幸的是没有办法(至少我找不到)。
It's painful to specify the specific values of hashkey and 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.
指定 hashkey 和 sortKey 的具体值很痛苦。唯一的选择是扫描表以检索主键(或复合键),然后使用 deleteItem API 对其进行迭代以删除单个项目。
You can delete upto 25 items ( as large as 400KB)in a single call using BatchWriteItem
API.
您可以使用API在一次调用中删除多达25 个项目(大至 400KB)BatchWriteItem
。
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