Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PutItem with DynamoDB using lists / arrays

I'm trying to achieve something that I assumed would be pretty standard, but I've hit a wall with it. I have a bunch of data coming at me that I want to store in DynamoDB

    const putDataParams = {
        TableName: "sensorNodeData2",
        Item: {
            "deviceId":   {"S": event.deviceId}, 
            "timeStamp":   {"N": event.time},
            "rssi": {"S": event.rssi},
            "seq":  {"S": event.seqNum},
            "payload": {"L": payloadArrayInt16.map(x => x.N)}
        }
    };
    console.log('Data PutItem Params = ' + JSON.stringify(putDataParams));

    dynamodb.putItem(putDataParams, function(err, data) {
        if (err) {
            console.log(err);
            context.fail('ERROR: Dynamo failed: ' + err);
        }
        else {
            console.log(data);
            context.succeed('SUCCESS');
        }
    });

The problem is I cannot for the life of me figure out how to get the list part to work. I've defined it first as :

var payloadArrayInt16=  new Uint16Array(dataArrayMaxIndex); 

and the error is:

"errorMessage": "ERROR: Dynamo failed: InvalidParameterType: Expected params.Item[payload].L to be an Array"

Then I tried :

var payloadArrayInt16= [dataArrayMaxIndex];

Which went through, but obviously doesn't do what I want... when I print out the params, it's not pulled out the contents of the array. It sees:

"Temp":{"L":[null,null,null,null,null,null,null,null,null,null]}

I'm pulling my hair out. There isn't a single example anywhere on how to do this. Can someone please put me out of my misery?!

like image 407
monkey Avatar asked Nov 07 '18 07:11

monkey


People also ask

Does DynamoDB support array?

A DynamoDB Set deserializes into an object with its array of items stored under the values property. If we want a Set to deserialize into an array, we'll need to add an unmarshalling step where we assign the values property instead of the deserialized set object itself.

Does DynamoDB support list?

DynamoDB supports the Java Set , List , and Map collection types. The following table summarizes how these Java types map to the DynamoDB types.

What is PutItem in DynamoDB?

The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data and there will be no Item element in the response. GetItem provides an eventually consistent read by default.

Which one of the following data types does Amazon DynamoDB not support?

Unlike conventional relational databases, DynamoDB does not natively support a date and time data type.


1 Answers

DynamoDB array of numeric values is represented like this:

"payload": {
    "L": [
      {
        "N": "1"
      },
      {
        "N": "2"
      },
      {
        "N": "3"
      }
    ]
  }

Now if your input array payloadArrayInt16 is like this:

[1,2,3]

You have to transform it to dynamo db format:

payloadArrayInt16.map(x => { return { "N": x.toString() }});

But you can make your life much easier by using this official aws dymanodb js library It handles all transformations between native js object structures to dynamo db data structures and viceversa.

Also if you are using AWS IoT Core you can write your data to dynamodb directly without any lambda.

like image 67
kolodi Avatar answered Oct 25 '22 09:10

kolodi