Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CLI: JSON load into DynamoDB

Hi I am trying to load the following JSON file structure (> 8k transactions) into DynamoDB through the AWS CLI command.

{
    "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"
        },
    ...

Could anyone help with the command that I should use for this? Also do I need to transform this JSON file for it to work?

like image 685
Estrobelai Avatar asked Sep 21 '26 16:09

Estrobelai


1 Answers

You can do it by using batch-write-item. It can take a maximum of 25 puts or delete.

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

Set your request-items.json as,

{
    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

like image 65
Shiyas Avatar answered Sep 24 '26 15:09

Shiyas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!