AWS CLI:JSON加载到DynamoDB中

时间:2020-09-04 22:15:01

标签: amazon-web-services amazon-ec2 amazon-dynamodb aws-cli

嗨,我正在尝试通过AWS CLI命令将以下JSON文件结构(> 8k事务)加载到DynamoDB中。

{
    "transactions": [
        {
            "customerId": "abc",
            "transactionId": "123",
            "transactionDate": "2020-09-01",
            "merchantId": "1234",
            "categoryId": "3",
            "amount": "5",
            "description": "McDonalds"
        },
        {
            "customerId": "def",
            "transactionId": "456",
            "transactionDate": "2020-09-01",
            "merchantId": "45678",
            "categoryId": "2",
            "amount": "-11.70",
            "description": "Tescos"
        },
        {
            "customerId": "jkl",
            "transactionId": "gah",
            "transactionDate": "2020-09-01",
            "merchantId": "9081",
            "categoryId": "3",
            "amount": "-139.00",
            "description": "Amazon"
        },
    ...

有人可以帮助我使用该命令吗?还需要转换此JSON文件才能使其正常工作吗?

1 个答案:

答案 0 :(得分:0)

您可以使用批处理写入项来完成。最多可能需要25个放置或删除操作。

aws dynamodb batch-write-item  --request-items file://request-items.json 

request-items.json 设置为

{
    your-table-name: [
        {
            "PutRequest": {
                "Item": {
                    "customerId": {"S": "abc"},
                    "transactionId": {"S": "5651"}
                    ....
                }
            }
        },
        {
            "PutRequest": {
                "Item": {
                    "customerId": {"S": "def"},
                    "transactionId": {"S": "125"},
                    ....
                }
            }
        },
        {
            "PutRequest": {
                "Item": {
                    "customerId": {"S": "jkl"},
                    "transactionId": {"S": "4466"},
                    ....
                }
            }
        },
        .....
        .....
    ]
}

https://docs.aws.amazon.com/cli/latest/reference/dynamodb/batch-write-item.html