在dynamodb中使用batchWriteItem

2024-04-28

我的 dynamo 数据库中有两个表,一个是候选表,另一个是用户表我想在 dynamo 数据库中使用 batchWriteItem 以便在表中添加数据。

我格式化的查询如下

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

但如果我使用
dynamodb.batchWriteItem(writes, function(err, regdata) { }

它最终以错误告终。 如何编写正确的查询?我收到的错误是这样的。

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

要在 DynamoDB 中批量写入,数据必须以 dynamodb 方式格式化。 如果你想使用标准 json 格式,请使用 documentclient。 下面有一个示例,请记住 dynamobb batchwrite 仅接受请求的最大 25 个元素。

所以根据doc https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-table-read-write-batch.html你必须有 :

1. 属性

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

根据你的例子:

“角色”:{“S”:“候选人”}

2. Items

每个项目必须具有此格式

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

3. 要添加的项目数组

创建一个项目数组,不超过 25 个元素(这是 dynamodb 对批量写入的限制)

4.您的请求参数

把它放在一起

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

5. 请求

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

UPDATE

你的例子将是这样的:

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
            }
          }
        }
      }
    ]
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在dynamodb中使用batchWriteItem 的相关文章

随机推荐