简体   繁体   English

DynamoDb:删除具有相同哈希键的所有项目

[英]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.

相关问题 使用java sdk从具有散列范围模式的给定散列键中查询DynamoDB中的所有项 - Query all items in DynamoDB from a given hash key with a hash-range schema using java sdk java删除dynamodb中的所有项目 - java delete all items in dynamodb DynamoDB:仅获取所有唯一哈希键的最新项目 - DynamoDB: Getting only most recent items of all unique hash keys DynamoDB:在给定一组哈希键的情况下,批量查询具有最高范围键的项目 - DynamoDB: Batch query items with highest range key given a set of hash key 仅使用 Dynamodb 中的分区键获取所有项目 - Get all items using only the Partition key from Dynamodb 在DynamoDB范围键中插入项目 - Inserting items in DynamoDB Range key 删除所有条目,但从哈希映射中删除指定的密钥集 - Delete all the entries but the specified Key set from Hash Map 如何在不使用java指定主键的情况下从DynamoDB表中获取所有项目? - How can I fetch all items from a DynamoDB table without specifying the primary key with java? 将不同的 dynamodb 项(来自同一个 dynamodb 表)解组到多个 POJO - Unmarshalling different dynamodb items (from the same dynamodb table) to multiple POJOs 在 DynamoDB 中获取最近 15 分钟的所有项目 - Get all items of the last 15 minutes in DynamoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM