Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB - putItem - Map with nested data types (NodeJS, aws-sdk)

All - I am working on a specific business requirement and with the lack of info on Google I thought I would stop here for some info:

I am basically ingesting a CSV, converting it to a JSON Object and stuffing it into Dynamo. The interesting part is, the data types of the row values jump between strings and numbers but I am unable to get this to work properly.

I am using Node and the aws-sdk and literally used the Amazon Docs to test this straight up and it still did not work, see below:

var params = {
    TableName: foo,
    Item: {
        masterReportsUuid: uuidv4(),
        reportDate: _eventDate,
        "testAttribute": {
            "Name": {
                "S": "Joe"
            },
            "Age": {
                "N": "35"
            }
        },
    }
};



dbDocClient.put(params, (err, data) => {
    if (err) {
        //log to CloudWatch
        console.log(err);
        reject(err);
    } else {
        resolve(data);
    }
});

The testAttribute obviously is a Map with Name and Age, string and number. This is straight from the documentation -

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#putItem-property

An attribute of type Map. For example:

"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}

But this outputs like this in Dynamo -

Dynamo Output

So my question is - why is this not working?

EDIT: Typos.

like image 430
Justin Twarog Avatar asked Mar 14 '19 14:03

Justin Twarog


1 Answers

Ugh - I fixed the issue. I will leave this here in case anyone runs into this.

Two Issues - I was using the DynamoDB.DoucmentClient().put API Call and not the DynamoDB.putItem call AND my params object was close but not correct. Please see below for a working example of nested Map AttributeTypes -

const dbDocClient = new aws.DynamoDB.DocumentClient();
const dbDynamo = new aws.DynamoDB();

var params = {
    TableName: _ReportsTable,
    Item: {
        testUuid: {
            "S": uuidv4()
        },
        testDate: {
            "S": _eventDate
        },
        testAttribute: {
            "M": {
                "Name": {
                    "S": "Joe"
                },
                "Age": {
                    "N": "35"
                }
            }
        },
    }
};


dbDynamo.putItem(params, (err, data) => {
    if (err) {
        //log to CloudWatch
        console.log(err);
        reject(err);
    } else {
        resolve(data);
    }
});
like image 100
Justin Twarog Avatar answered Oct 26 '22 08:10

Justin Twarog