Javascript 在dynamodb中使用batchWriteItem
我的dynamo db中有两个表,一个是候选表,另一个是用户表,我想在dynamo db中使用batchWriteItem,以便在表中添加数据 我格式化的查询如下Javascript 在dynamodb中使用batchWriteItem,javascript,node.js,amazon-dynamodb,aws-sdk-nodejs,Javascript,Node.js,Amazon Dynamodb,Aws Sdk Nodejs,我的dynamo db中有两个表,一个是候选表,另一个是用户表,我想在dynamo db中使用batchWriteItem,以便在表中添加数据 我格式化的查询如下 var user = { userid: usrid, role: 'candidate', password: vucrypt.encryptpass(pass) }; var canduser = { fname: req.body.fnam
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(写入,函数(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个元素的mawimum请求 因此,根据以下条件,您必须: 1.属性 “属性_1”:{“S”:“属性_1_值”} 根据你的例子: “角色”:{“S”:“候选人”} 2.项目 每个项目必须具有此格式
PutRequest: {
Item: {
...,
"ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" },
...
}
}
3.要添加的项目数组
创建一个不超过25个元素的项目数组(这是batchwrite的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);
}
});
更新
您的示例如下:
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
}
}
}
}
]
}
}
这是正确的答案,有一些类型的问题
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 +""
}
}
}
}]
}
}
上面的查询是正确的(我写的)有一个表名和所有缺少的表名,我不知道如何将它们放入上面的查询中。你能帮我吗?这是我正在寻找的东西。你能给我一个基于我编写的查询的例子吗?@ArunVM我添加了一个更新,但根据你的需要检查类型。例如有两个表
“users”:[{put:user}],'candidate':[{put:canduser}],
答案是只有一个表,这就是我被卡住的地方。