Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB - AWS CLI - batch-write-item only inserts one row

I created a table using this AWS CLI command:

aws dynamodb create-table --table-name test_table --attribute-definitions AttributeName=time_stamp,AttributeType=N AttributeName=watch_uuid,AttributeType=S --key-schema AttributeName=watch_uuid,KeyType=HASH AttributeName=time_stamp,KeyType=RANGE --billing-mode PROVISIONED --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1

I then tried inserting two rows using this command:

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

and this file contents:

{
    "test_table": [
        {
            "PutRequest": {
                "Item": {
                    "owner_name": {
                        "S": "Test watch 3"
                    },
                    "time_stamp": {
                        "N": "1541524533453"
                    },
                    "watch_uuid": {
                        "S": "A9A0E8B2-CD8D-464A-8787-383A85919A06_1541524533453_Test watch 3"
                    },
                    "y_user_accel": {
                        "S": "0.07286"
                    }
                }
            },
            "PutRequest": {
                "Item": {
                    "owner_name": {
                        "S": "Test watch 4"
                    },
                    "time_stamp": {
                        "N": "1541524533765"
                    },
                    "watch_uuid": {
                        "S": "A9A0E8B2-CD8D-464A-8787-383A85919A06_1541524533453_Test watch 4"
                    },
                    "y_user_accel": {
                        "S": "0.07286"
                    }
                }
            }
        }
    ]
}

I got no errors or indications that it would only insert 1 row, but the DB now only has 1 row. I have tried playing around with the structure of the file to no avail. I either get an error (because the structure was wrong) or only one row of data inserted.

I have also tried changing the provisioning numbers from 1 to 25. This also hasn't helped.

I am pretty sure the problem is in the file structure, but the documentation is inconsistent with what the structure should be.

Any thoughts would be much appreciated. Thanks.

like image 734
Garet Jax Avatar asked Dec 13 '18 14:12

Garet Jax


1 Answers

Looking at the example in the official documentation, you need to change your format to this:

{
    "test_table": [
        {
            "PutRequest": {
                "Item": {
                    "owner_name": {
                        "S": "Test watch 3"
                    },
                    "time_stamp": {
                        "N": "1541524533453"
                    },
                    "watch_uuid": {
                        "S": "A9A0E8B2-CD8D-464A-8787-383A85919A06_1541524533453_Test watch 3"
                    },
                    "y_user_accel": {
                        "S": "0.07286"
                    }
                }
            }
         },
         {
            "PutRequest": {
                "Item": {
                    "owner_name": {
                        "S": "Test watch 4"
                    },
                    "time_stamp": {
                        "N": "1541524533765"
                    },
                    "watch_uuid": {
                        "S": "A9A0E8B2-CD8D-464A-8787-383A85919A06_1541524533453_Test watch 4"
                    },
                    "y_user_accel": {
                        "S": "0.07286"
                    }
                }
            }
        }
    ]
}

Note how "test_table" is an array. You need to provide multiple items in that array. You were providing a single item in that array, with two parameters, both named "PutRequest", so when the JSON was parsed it ended up with a single "PutRequest" because you can't have multiple properties on a JSON object with the same name.

like image 67
Mark B Avatar answered Oct 11 '22 15:10

Mark B