Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert json in dynamodb

Here's my code.
I want to insert asset_data json into asset_data column. i am using aws sdk. It says aws sdk now has support for json. http://aws.amazon.com/releasenotes/SDK/JavaScript/1691866671551861

var asset_data = {
    "name": "name" + i,
    "contentUrl": "http://www.hdwallpapersimages.com/nature-beauty-desktop-images/94892/",
    "size": 300,
    "headline": "headline",
    "description": "assetUrl reference for the creator",
    "encodingFormat": 'jpeg'
  };

  var params = {
    TableName: 'xyz',
    Item: { // a map of attribute name to AttributeValue
      "asset_id": {S: "asset" + i},
      "hit_id": {S: "0"},
      "created_date": {"S": Date.now().toString()},
      "status": {N: "0"},
      "operation": {S: "image_tagging"},
      "asset_data": {L: asset_data},
      "source": {S: "DAM"},
      "completed_date": {S: Date.now().toString()},
      "response_data": {S: "taged value"}
      // more attributes...
    },

    ReturnValues: 'NONE', // optional (NONE | ALL_OLD)
    ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
    ReturnItemCollectionMetrics: 'NONE' // optional (NONE | SIZE)
  };

  db.putItem(params, function (err, data) {
    if (err) console.log(err); // an error occurred
    else console.log("inserted..."); // successful response
  });
like image 549
mks Avatar asked Oct 05 '15 09:10

mks


People also ask

Can you store JSON 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.

Does DynamoDB contain JSON data type?

DynamoDB data types are a superset of JSON data types. This means that all JSON data can be represented as DynamoDB data, while the opposite isn't true.

How do I load data into AWS DynamoDB?

To import data from S3 to DynamoDBLog in to the console and navigate to the DynamoDB service. Choose Imports from S3 in the navigation pane. The Imports from S3 page lists information about any existing or recent import jobs that have been created. Choose Import from S3 to move to the Import options page.


1 Answers

You can use the DynamoDB Document Client SDK:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.

For your case specifically look at the Map Attribute value (M) being passed as MapAttribute in the example below extracted from the official documentation. The Document Client API takes care of the proper marshalling/unmarshalling between the Javascript and DynamoDB types (meaning that you don't have to specify the Attribute Values (S,N,L,...) as it is required when using the non-document based SDK ):

var params = {
  TableName: 'Table',
  Item: {
     HashKey: 'haskey',
     NumAttribute: 1,
     BoolAttribute: true,
     ListAttribute: [1, 'two', false],
     MapAttribute: { foo: 'bar'},
     NullAttribute: null
  }
};

var docClient = new AWS.DynamoDB.DocumentClient();

docClient.put(params, function(err, data) {
  if (err) console.log(err);
  else console.log(data);
});
like image 72
b-s-d Avatar answered Sep 28 '22 16:09

b-s-d