简体   繁体   English

java删除dynamodb中的所有项目

[英]java delete all items in dynamodb

Im trying to delete all items in my table in dynamodb but it does not work.我试图在 dynamodb 中删除我表中的所有项目,但它不起作用。

    try {
        ScanRequest scanRequest = new ScanRequest().withTableName(table);
        ScanResult scanResult = null;

        do {
            if (Check.nonNull(scanResult)) {
                scanRequest.setExclusiveStartKey(scanResult.getLastEvaluatedKey());
            }

            scanResult = client.scan(scanRequest);

            scanResult.getItems().forEach((Item) -> {
                String n1 = Item.get("n1").toString();
                String n2 = tem.get("n2").toString();
                DeleteItemSpec spec = new DeleteItemSpec().withPrimaryKey("n1", n1, "n2", n2);
                dynamodb.getTable(table).deleteItem(spec);
            });
        } while (Check.nonNull(scanResult.getLastEvaluatedKey()));
    } catch (Exception e) {
        throw new BadRequestException(e);
    }

n1 is my Primary partition key n1 是我的主分区键

n2 is my Primary sort key n2 是我的主要排序键

The best approach to delete all the items from DynamoDB is to drop the table and recreate it.从 DynamoDB 中删除所有项目的最佳方法是删除表并重新创建它。

Otherwise, there are lot of read capacity and write capacity units being used which will cost you.否则,将使用大量读取容量和写入容量单位,这将花费您。

Dropping and recreating the table is the best approach.删除并重新创建表是最好的方法。

PREAMBLE : While a scan operation is expensive, I was needing this answer for initialising a table for a test scenario (low volume).序言:虽然扫描操作很昂贵,但我需要这个答案来初始化测试场景的表(低容量)。 The table was being created by another process and I needed the test scenario on that table, I could therefore not delete and recreate the table.该表是由另一个进程创建的,我需要该表上的测试场景,因此我无法删除和重新创建该表。

ANSWER : given:答案:给定:

  • DynamoDbClient db DynamoDbClient 数据库

  • static String TABLE_NAME静态字符串 TABLE_NAME

  • static String HASH_KEY静态字符串 HASH_KEY

  • static String SORT_KEY静态字符串 SORT_KEY

     ScanIterable scanIterable = db.scanPaginator(ScanRequest.builder() .tableName(TABLE_NAME) .build()); for(ScanResponse scanResponse:scanIterable){ for( Map<String, AttributeValue> item: scanResponse.items()){ Map<String,AttributeValue> deleteKey = new HashMap<>(); deleteKey.put(HASH_KEY,item.get(HASH_KEY)); deleteKey.put(SORT_KEY,item.get(SORT_KEY)); db.deleteItem(DeleteItemRequest.builder() .tableName(TRANSACTION_TABLE_NAME) .key(deleteKey).build()); } }

To delete all the items from the table first you need to perform scan operation over the table which will results you an scanoutcome.要首先从表中删除所有项目,您需要对表执行扫描操作,这将导致扫描结果。 Using the iterator loop over the sacnoutcome with the primary key and it's primary key value.This will be one of the approach to delete all the items from the table.使用迭代器循环遍历 sacnoutcome 和主键及其主键值。这将是从表中删除所有项目的方法之一。 Hope that this code will work you.希望这段代码对你有用。 Thanks谢谢

Table table = dynamoDB.getTable(your_table);
ItemCollection<ScanOutcome> deleteoutcome = table.scan();
Iterator<Item> iterator = deleteoutcome.iterator();

while (iterator.hasNext()) {
    your_table.deleteItem("PrimaryKey", iterator.next().get("primary key value"));
}

//May be we can make it look generic by reading key schema first as below
String strPartitionKey = null;
String strSortKey = null;
TableDescription description = table.describe();
List<KeySchemaElement> schema = description.getKeySchema();
for (KeySchemaElement element : schema) {
    if (element.getKeyType().equalsIgnoreCase("HASH"))
        strPartitionKey = element.getAttributeName();
    if (element.getKeyType().equalsIgnoreCase("RANGE"))
        strSortKey = element.getAttributeName();
}

ItemCollection<ScanOutcome> deleteoutcome = table.scan();
Iterator<Item> iterator = deleteoutcome.iterator();
while (iterator.hasNext()) {
    Item next = iterator.next();
    if (strSortKey == null && strPartitionKey != null)
        table.deleteItem(strPartitionKey, next.get(strPartitionKey));
    else if (strPartitionKey != null && strSortKey != null)
        table.deleteItem(strPartitionKey, next.get(strPartitionKey), strSortKey, next.get(strSortKey));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DynamoDb:删除具有相同哈希键的所有项目 - DynamoDb: Delete all items having same Hash Key 使用 DynamoDbMapper 从 dynamodb java 检索所有项目 ID - Retrieve all items ids from dynamodb java using DynamoDbMapper 如何在不使用java指定主键的情况下从DynamoDB表中获取所有项目? - How can I fetch all items from a DynamoDB table without specifying the primary key with java? 使用 Java 高级 API 从 DynamoDB 表中获取所有表项 - Get all the table items from DynamoDB table using Java High Level API 使用java sdk从具有散列范围模式的给定散列键中查询DynamoDB中的所有项 - Query all items in DynamoDB from a given hash key with a hash-range schema using java sdk AWS Serverless Lambda Java 从没有变量类型的 Amazon DynamoDB 检索所有项目 - AWS Serverless Lambda Java retrieve all items from Amazon DynamoDB without variable types 有没有办法在 DynamoDB 中检索没有属性的所有项目? - Is there a way to retrieve all items without an attribute in DynamoDB? 在 DynamoDB 中获取最近 15 分钟的所有项目 - Get all items of the last 15 minutes in DynamoDB 通过Java代码将多个项目放入DynamoDB中 - Put multiple items into DynamoDB by Java code Java:无法删除 dynamodb 项目值 - Java: unable to delete dynamodb item value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM