从亚马逊dynamodb中删除 [英] Deletion from amazon dynamodb

查看:285
本文介绍了从亚马逊dynamodb中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种有效的方法可以一次删除来自亚马逊dynamodb tabe的所有项目。我已经查看了aws文档,但是显示出删除了一个项目。

Is there any efficient way to delete all the items from a amazon dynamodb tabe at once.I have gone through the aws docs but there it's shown deletion of a single item.

推荐答案

您将要使用 BatchWriteItem (如果您不能删除表格)。如果所有条目都在单个HashKey中,则可以使用Query API检索记录,然后一次删除25个项目。否则,您可能需要扫描。

You will want to use BatchWriteItem if you can't drop the table. If all your entries are within a single HashKey, you can use the Query API to retrieve the records, and then delete them 25 items at a time. If not, you'll probably have to Scan.

或者,您可以在 AmazonDynamoDBClient ( (来自官方SDK)收集表中存在的一组哈希/范围键。然后,您将不需要查询或扫描在测试后插入的项目,因为您已经建立了Set。看起来像这样:

Alternatively, you could provide a simple wrapper around AmazonDynamoDBClient (from the official SDK) that collects a Set of Hash/Range keys that exist in your table. Then you wouldn't need to Query or Scan for the items you inserted after the test, since you would already have the Set built. That would look something like this:

public class KeyCollectingAmazonDynamoDB implements AmazonDynamoDB
{
    private final AmazonDynamoDB delegate;
    // HashRangePair is something you have to define
    private final Set<Key> contents;

    public InsertGatheringAmazonDynamoDB( AmazonDynamoDB delegate )
    {
        this.delegate = delegate;
        this.contents = new HashSet<>();
    }

    @Override
    public PutItemResult putItem( PutItemRequest putItemRequest )
            throws AmazonServiceException, AmazonClientException
    {
        contents.add( extractKey( putItemRequest.getItem() ) );
        return delegate.putItem( putItemRequest );
    }

    private Key extractKey( Map<String, AttributeValue> item )
    {
        // TODO Define your hash/range key extraction here
        // Create a Key object
        return new Key( hashKey, rangeKey );
    }

    @Override
    public DeleteItemResult deleteItem( DeleteItemRequest deleteItemRequest )
            throws AmazonServiceException, AmazonClientException
    {
        contents.remove( deleteItemRequest.getKey() );
        return delegate.deleteItem( deleteItemRequest );
    }

    @Override
    public BatchWriteItemResult batchWriteItem( BatchWriteItemRequest batchWriteItemRequest )
            throws AmazonServiceException, AmazonClientException
    {
        // Similar extraction, but in bulk.
        for ( Map.Entry<String, List<WriteRequest>> entry : batchWriteItemRequest.getRequestItems().entrySet() )
        {
            String tableName = entry.getKey();
            List<WriteRequest> writeRequests = entry.getValue();
            for ( WriteRequest writeRequest : writeRequests )
            {
                PutRequest putRequest = writeRequest.getPutRequest();
                if ( putRequest != null )
                {
                    // Add to Set just like putItem
                }
                DeleteRequest deleteRequest = writeRequest.getDeleteRequest();
                if ( deleteRequest != null )
                {
                    // Remove from Set just like deleteItem
                }
            }
        }

        // Write through to DynamoDB
        return delegate.batchWriteItem( batchWriteItemRequest );
    }

    // remaining methods elided, since they're direct delegation
}

DynamoDB SDK ,该构造函数在构造函数中接受零个,一个或两个 AttributeValue 对象,以表示哈希键或哈希/范围键。假设它的等于 hashCode 方法有效,则可以在 Set Key 类。

Key is a class within the DynamoDB SDK that accepts zero, one, or two AttributeValue objects in the constructor to represent a hash key or a hash/range key. Assuming it's equals and hashCode methods work, you can use in within the Set I described. If they don't, you'll have to write your own Key class.

这应该会让您保持在测试中使用的Set。它不是特定于一个表的,因此如果您使用多个表,则可能需要添加另一层集合。这会将 Set< Key> 更改为 Map< TableName,Set< Key>> 之类的东西。您需要查看 getTableName()属性以选择正确的 Set 进行更新。

This should get you a maintained Set for use within your tests. It's not specific to a table, so you might need to add another layer of collection if you're using multiple tables. That would change Set<Key> to something like Map<TableName, Set<Key>>. You would need to look at the getTableName() property to pick the correct Set to update.

测试完成后,抓取表中的内容并删除应该很简单。

Once your test finishes, grabbing the contents of the table and deleting should be straightforward.

最后一条建议:使用其他表比您的应用程序进行测试。创建一个相同的架构,但是为表指定一个不同的名称。您甚至可能希望其他IAM用户阻止您的测试代码访问生产表。如果您对此有疑问,请随时为该情况打开一个单独的问题。

One final suggestion: use a different table for testing than you do for your application. Create an identical schema, but give the table a different name. You probably even want a different IAM user to prevent your test code from accessing your production table. If you have questions about that, feel free to open a separate question for that scenario.

这篇关于从亚马逊dynamodb中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆