最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using batchWriteItem in dynamodb - Stack Overflow

programmeradmin2浏览0评论

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.bodyode,
        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

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
Share Improve this question edited Aug 6, 2018 at 7:19 Arun VM asked Aug 6, 2018 at 7:14 Arun VMArun VM 4471 gold badge6 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

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

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 +""
                  }
               }
             }
        }]
    }
  }
发布评论

评论列表(0)

  1. 暂无评论