java删除dynamodb中的所有项

java删除dynamodb中的所有项,java,amazon-dynamodb,Java,Amazon Dynamodb,我正在尝试删除dynamodb中我的表中的所有项,但它不起作用 try { ScanRequest scanRequest = new ScanRequest().withTableName(table); ScanResult scanResult = null; do { if (Check.nonNull(scanResult)) { scanRequest.setExclus

我正在尝试删除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是我的主分区密钥


n2是我的主要排序键

从DynamoDB中删除所有项的最佳方法是删除表并重新创建它

否则,将使用大量的读容量和写容量单元,这将花费您的成本


删除并重新创建表格是最好的方法。

要首先从表格中删除所有项目,您需要对表格执行扫描操作,这将生成扫描结果。使用主键及其主键值对sacnoutcome进行迭代器循环。这将是从表中删除所有项的方法之一。希望这段代码能对你有用。谢谢

Table Table=dynamoDB.getTable(您的表);
ItemCollection deleteOutput=table.scan();
迭代器迭代器=deleteOutput.Iterator();
while(iterator.hasNext()){
您的_table.deleteItem(“PrimaryKey”,iterator.next().get(“主键值”));
}
//也许我们可以通过阅读键模式使它看起来更通用,如下所示
字符串strPartitionKey=null;
字符串strSortKey=null;
TableDescription=table.description();
列表模式=description.getKeySchema();
for(KeySchemaElement元素:架构){
if(element.getKeyType().equalsIgnoreCase(“哈希”))
strPartitionKey=element.getAttributeName();
if(element.getKeyType().equalsIgnoreCase(“范围”))
strSortKey=element.getAttributeName();
}
ItemCollection deleteOutput=table.scan();
迭代器迭代器=deleteOutput.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));
}

序言:虽然扫描操作很昂贵,但我需要这个答案来初始化测试场景(低容量)的表。该表是由另一个进程创建的,我需要该表上的测试场景,因此无法删除并重新创建该表

回答: 鉴于:

  • dynamodb客户端数据库

  • 静态字符串表名称

  • 静态字符串哈希键

  • 静态字符串排序键

     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());
         }
     }
    
    ScanIterable ScanIterable=db.scanPaginator(ScanRequest.builder())
    .tableName(表名称)
    .build());
    for(扫描响应扫描响应:可扫描){
    对于(映射项:scanResponse.items()){
    Map deleteKey=new HashMap();
    deleteKey.put(散列键,item.get(散列键));
    deleteKey.put(排序键,item.get(排序键));
    db.deleteItem(DeleteItemRequest.builder()
    .tableName(事务表名称)
    .key(deleteKey.build());
    }
    }
    

您是否有任何例外?如果是,您能否共享异常stacktrace?请具体说明。“它不工作”信息不足。dynamoDb是根据为表设置的读写容量收费的。即使您不在表上执行任何操作,您也将被收费。