29er
29er

Reputation: 9005

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 ??

Upvotes: 21

Views: 36890

Answers (4)

T.UK
T.UK

Reputation: 71

The code above doesn't read in individual JSON objects, if you wanted to do that from a JSON file with multiple objects:

import boto3
import json

dynamodbclient=boto3.resource('dynamodb')
sample_table = dynamodbclient.Table('ec2metadata')

with open('/samplepath/spotec2interruptionevent.json', 'r') as myfile:
    data=myfile.read()

# parse file
objects = json.loads(data)

#instance_id and cluster_id is the Key in dynamodb table 

   for object in objects:
       instance_id = object["instance_id"]
       cluster_id =  object["cluster_id"]
       sample_table.put_item=(item=object)

Upvotes: 0

carpiediem
carpiediem

Reputation: 2028

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" },
                }
            }
        }
    ]
}

Upvotes: 19

Anandkumar
Anandkumar

Reputation: 1502

I used boto3 in python to load the data

import boto3
import json

dynamodbclient=boto3.resource('dynamodb')
sample_table = dynamodbclient.Table('ec2metadata')

with open('/samplepath/spotec2interruptionevent.json', 'r') as myfile:
    data=myfile.read()

# parse file
obj = json.loads(data)

#instance_id and cluster_id is the Key in dynamodb table 

    response=sample_table.put_item(
                              Item={
                                  'instance_id': instanceId,
                                  'cluster_id': clusterId,
                                  'event':obj

                              }
                              )

Here is a sample for javascript:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.02.html#GettingStarted.Js.02.02

Upvotes: 1

E.J. Brennan
E.J. Brennan

Reputation: 46839

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

Upvotes: 13

Related Questions