通过数据管道截断 DynamoDb 或重写数据

Vla*_*ich 7 truncate amazon-dynamodb amazon-data-pipeline data-pipeline

可以通过数据管道转储 DynamoDb,也可以在 DynamoDb 中导入数据。导入进展顺利,但数据一直附加到 DynamoDb 中已经存在的数据。

现在我找到了扫描 DynamoDb 并一个一个或通过 Batch 删除项目的工作示例。但无论如何,对于大量数据来说,这不是一个好的变体。

也可以完全删除表并创建它。但是随着该变体索引将丢失。

因此,最好的方法是通过数据管道导入或以某种方式截断来覆盖 DynamoDb 数据。有可能吗?如果是的话,怎么可能?

LuF*_*FFy 9

Truncate Table 功能在 DynamoDB 中不可用,所以请考虑删除表并重新创建

原因:DynamoDB 根据您使用的ReadCapacityUnits&WriteCapacityUnits向您收费。如果您使用BatchWriteItemfunction删除所有项目,它将使用WriteCapacityUnits. 所以,为了保存这些WriteCapacityUnits用于删除项目,如果您截断表并重新创建它会更好。

删除和创建 DynamoDB 表的步骤如下:

通过 AWS CLI 删除表

aws dynamodb delete-table --table-name *tableName*
Run Code Online (Sandbox Code Playgroud)

通过 AmazonDynamoDB API 删除表

样品申请

POST / HTTP/1.1
Host: dynamodb.<region>.<domain>;
Accept-Encoding: identity
Content-Length: <PayloadSizeBytes>     
User-Agent: <UserAgentString>
Content-Type: application/x-amz-json-1.0
Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=<Headers>, Signature=<Signature>
X-Amz-Date: <Date> 
X-Amz-Target: DynamoDB_20120810.DeleteTable 

{
    "TableName": "Reply"
}
Run Code Online (Sandbox Code Playgroud)

通过 AmazonDynamoDB API 创建 DynamoDB 表

POST / HTTP/1.1
Host: dynamodb.<region>.<domain>;
Accept-Encoding: identity
Content-Length: <PayloadSizeBytes>     
User-Agent: <UserAgentString>
Content-Type: application/x-amz-json-1.0
Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=<Headers>, Signature=<Signature>
X-Amz-Date: <Date> 
X-Amz-Target: DynamoDB_20120810.CreateTable 

{
    "AttributeDefinitions": [
        {
            "AttributeName": "ForumName",
            "AttributeType": "S"
        },
        {
            "AttributeName": "Subject",
            "AttributeType": "S"
        },
        {
            "AttributeName": "LastPostDateTime",
            "AttributeType": "S"
        }
    ],
    "TableName": "Thread",
    "KeySchema": [
        {
            "AttributeName": "ForumName",
            "KeyType": "HASH"
        },
        {
            "AttributeName": "Subject",
            "KeyType": "RANGE"
        }
    ],
    "LocalSecondaryIndexes": [
        {
            "IndexName": "LastPostIndex",
            "KeySchema": [
                {
                    "AttributeName": "ForumName",
                    "KeyType": "HASH"
                },
                {
                    "AttributeName": "LastPostDateTime",
                    "KeyType": "RANGE"
                }
            ],
            "Projection": {
                "ProjectionType": "KEYS_ONLY"
            }
        }
    ],
    "ProvisionedThroughput": {
        "ReadCapacityUnits": 5,
        "WriteCapacityUnits": 5
    }
}
Run Code Online (Sandbox Code Playgroud)

总结删除表并重新创建它是最好的解决方案。