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