Javascript 如何使用DynamoDB batchGet命令
我从AWS DynamoDB教程中创建了一个“电影”DynamoDB表,发布在 具有以下属性:Javascript 如何使用DynamoDB batchGet命令,javascript,typescript,amazon-web-services,amazon-dynamodb,dynamodb-queries,Javascript,Typescript,Amazon Web Services,Amazon Dynamodb,Dynamodb Queries,我从AWS DynamoDB教程中创建了一个“电影”DynamoDB表,发布在 具有以下属性: var tableAttrs = { TableName : "Movies", KeySchema: [ { AttributeName: "year", KeyType: "HASH"}, { AttributeName: "title&q
var tableAttrs = {
TableName : "Movies",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"},
{ AttributeName: "title", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
现在我想使用batchGet
命令:
var params = {
"RequestItems" : {
"Movies": {
"Keys" : [
{year : { "N" : "1000" } },
{year : { "N" : "1001" } }
]
}
}
}
并使用以下工具运行它:
let db = new DynamoDB.DocumentClient({ apiVersion: '2012-08-10' })
let result = db.batchGet(params).promise();
但我得到了一个错误:
ValidationException: The provided key element does not match the schema
为什么作为关键元素提供的年份
与架构不匹配?如何避免此错误并使其正常工作
下面是显示表键的屏幕截图:
:从文档中
BatchGetItem操作返回一个或多个项的属性
来自一个或多个表。您可以通过主键标识请求的项目
我们必须指定整个主键,即分区键和排序键的组合。GetItem也一样
批量获取:
const AWS = require("aws-sdk");
AWS.config.update({ region: "us-east-1" });
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.batchGet(
{
RequestItems: {
Movies: {
Keys: [
{ year: 1000, title: "one" },
{ year: 1000, title: "two" },
],
},
},
},
function (err, data) {
console.log("err", err, "data", JSON.stringify(data));
}
);
为了仅通过分区键获取记录,我们可以使用查询
查询:
const AWS = require("aws-sdk");
AWS.config.update({ region: "us-east-1" });
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.query(
{
TableName: "Movies",
KeyConditionExpression: "#year = :yearValue ",
ExpressionAttributeValues: {
":yearValue": 1000,
},
ExpressionAttributeNames: {
"#year": "year",
},
},
function (err, data) {
if (err) console.error(err);
else
console.log("dynamodb query succeeded:", JSON.stringify(data, null, 2));
}
);