在 dynamodb 中使用 batchWriteItem
Using batchWriteItem in dynamodb
我的 dynamo 数据库中有两个 table,一个是候选 table,另一个是用户 table 我想在 dynamo 数据库中使用 batchWriteItem 以添加table.
中的数据
我格式化后的查询如下
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 个元素的 mawimum。
所以根据 doc 你必须有 :
1.属性
"ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }
根据你的例子:
"role": {"S":"candidate"}
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 +""
}
}
}
}]
}
}
我的 dynamo 数据库中有两个 table,一个是候选 table,另一个是用户 table 我想在 dynamo 数据库中使用 batchWriteItem 以添加table.
中的数据我格式化后的查询如下
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 个元素的 mawimum。
所以根据 doc 你必须有 :
1.属性
"ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }
根据你的例子:
"role": {"S":"candidate"}
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 +""
}
}
}
}]
}
}