Using batchWriteItem in dynamodb

I have two tables in my dynamo db one is candidate table and the other one is user table I want to use batchWriteItem in dynamo db in order to add the data in the table.

The query which I have formatted is as follows

var user = {
        userid: usrid,
        role: 'candidate',
        password: vucrypt.encryptpass(pass)
      };

      var canduser = {
        fname: req.body.fname,
        lname: req.body.lname,
        location: req.body.location,
        phone: req.body.phone,
        ccode: req.body.ccode,
        grad: req.body.grad,
        pgrad: req.body.pgrad,
        ograd: req.body.ograd,
        experience: exp,
        linkedin: req.body.linkedin,
        terms: tandc
      };
      canduser = vutools.fixcanduser(canduser);
      canduser.userid = usrid;

      var writes = {
        'users': [{put: user}],
        'candidate': [{put: canduser}],
      };

But if i use
dynamodb.batchWriteItem(writes, function(err, regdata) {
}

Its ending up as error.
How can I write the right query? The error I am getting is this.

MultipleValidationErrors: There were 3 validation errors:
* MissingRequiredParameter: Missing required key 'RequestItems' in params
* UnexpectedParameter: Unexpected key 'users' found in params
* UnexpectedParameter: Unexpected key 'candidate' found in params

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

To batchwrite in DynamoDB, the data must be formated in the dynamodb way.
if you want do it in standard json, go for the documentclient.
you have an example below, have in mind that dynamobb batchwrite only accept mawimum of 25 element by request.

so according to the doc you must have :

1. Attributes

“ATTRIBUTE_1”: { “S”: “ATTRIBUTE_1_VALUE” }

According to your example :

“role”: {“S”:”candidate”}

2. Items

Each item must have this format

      PutRequest: {
        Item: {
            ...,
            "ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" },
            ...
        }
      }

3. Array of items to add

Create an array of items, which doesn’t exceed 25 elements, (it’s a dynamodb limit for batchwrite)

4. Your request params

put it together

var params = {
  RequestItems: {
    "TABLE_NAME": [
        //the array you just created in step 3
     ]
   }
}

5. The request

ddb.batchWriteItem(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});

UPDATE

Your example will be something like this :

var params = {
  "RequestItems": {
    "TABLE_NAME": [
      {
        "PutRequest": {
          Item: {
            "userid": { "N": "usrid" },
            "role": { "S": 'candidate' },
            "password": { "S": vucrypt.encryptpass(pass) }
          }
        }
      }
    ],
    "TABLE_NAME2": [
      {
        "PutRequest": {
          Item: {
            "fname": {
              "S": req.body.fname
            },
            "lname": {
              "S": req.body.lname
            },
            "location": {
              "S": req.body.location
            },
            "phone": {
              "S": req.body.phone
            },
            "ccode": {
              "S": req.body.ccode
            },
            "grad": {
              "S": req.body.grad
            },
            "pgrad": {
              "S": req.body.pgrad
            },
            "ograd": {
              "S": req.body.ograd
            },
            "experience": {
              "S": exp
            },
            "linkedin": {
              "S": req.body.linkedin
            },
            "terms": {
              "S": tandc
            }
          }
        }
      }
    ]
  }
}

Solution 2

This is the right answer there are some type problems.

  var createuser = {
    "RequestItems": {
      "users": [{
           "PutRequest": {
               Item: {
                    "userid": {
                        "S": usrid +""
                    },
                    "password": {
                        "S": vucrypt.encryptpass(pass) +""
                    },
                    "role": {
                      "S": 'candidate' +""
                    }
                }
             }
        }],
      "candidate": [{
           "PutRequest": {
             Item: {
                  "ccode": {
                      "S": req.body.ccode +""
                  },
                  "fname": {
                      "S": req.body.fname +""
                  },
                  "lname": {
                      "S": req.body.lname +""
                  },
                  "pgrad": {
                      "S": req.body.pgrad +""
                  },
                  "videoresumeurl": {
                      "S": "-"
                  },
                  "phone": {
                      "S": req.body.phone +""
                  },
                  "terms": {
                      "S": tandc +""
                  },
                  "location": {
                      "S": req.body.location +""
                  },
                  "experience": {
                      "N": req.body.experience +""
                  },
                  "userid": {
                      "S": usrid +""
                  },
                  "grad": {
                      "S": req.body.grad +""
                  }
               }
             }
        }]
    }
  }

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply