从 amazon dynamodb 中删除 [英] Deletion from amazon dynamodb

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

问题描述

是否有任何有效的方法可以一次删除亚马逊 dynamodb 表中的所有项目.我已经浏览了 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)提供一个简单的包装器,该包装器收集您的表中存在的一组 Hash/Range 键.然后,您不需要查询或扫描您在测试后插入的项目,因为您已经构建了 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
}

KeyDynamoDB 中的一个类SDK 在构造函数中接受零个、一个或两个 AttributeValue 对象来表示哈希键或哈希/范围键.假设它的 equalshashCode 方法有效,您可以在我描述的 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.

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

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