Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS - import JSON file to load Dynamo table

I have a json file that I want to use to load my Dynamo table in AWS. In the AWS console, there is only an option to create one record at a time. Not good: )

Essentially my .JSON file is an array of objects which hold the data for each column in the table ie:

{
    "Column1": "Column1 Value",
    "Column2": "Column2 Value",
    "Column3": "Column3 Value",
    "Column4": "Column4 Value",
  },

Is there any way to do this via AWS console and importing my json file, or do I have to use AWS JS SDK to programmatically do this ??

like image 476
29er Avatar asked Apr 28 '18 17:04

29er


People also ask

Can we store JSON data in DynamoDB?

You can store a JSON document as an attribute in a DynamoDB table. To do this, use the withJSON method of Item . This method parses the JSON document and maps each element to a native DynamoDB data type.


2 Answers

The answer from E.J. Brennan looks correct, for a single record, but it doesn't answer the original question (which needs to add an array of records).

For this, the command is

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

But, you'll need to make a modified JSON file, like so (note the DynamoDB JSON that specifies data types):

{
    "YourTableName": [
        {   
            "PutRequest": {
                "Item": { 
                    "Column1": { "S": "Column1 Value" },
                    "Column2": { "S": "Column2 Value" },
                    "Column3": { "S": "Column3 Value" },
                    "Column4": { "S": "Column4 Value" },
                }
            }
        },
        {
            "PutRequest": {
                "Item": { 
                    "Column1": { "S": "Column1 Value" },
                    "Column2": { "S": "Column2 Value" },
                    "Column3": { "S": "Column3 Value" },
                    "Column4": { "S": "Column4 Value" },
                }
            }
        }
    ]
}
like image 170
carpiediem Avatar answered Oct 12 '22 15:10

carpiediem


You don't need to use the API. You could use the AWS-CLI instead, i.e:

aws dynamodb put-item --table-name MusicCollection --item file://item.json --return-consumed-capacity TOTAL

but you may need to tweak your JSON format a bit.

More examples and documentation here:

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

like image 24
E.J. Brennan Avatar answered Oct 12 '22 15:10

E.J. Brennan