如何使用 AWS DynamoDB 请求单个记录?代码示例不起作用



我已经将代码示例从最基本的部分拿下来,但我仍然无法按键检索单个记录。 这是我使用 nodejs 的基本示例(本片段中未包含的信条(:

var dynamodbClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: 'newsarticles',
Key: { id: '068d1110-cd0c-11e8-a4d4-57cb772554a4' }
};
//log full dynamodb request object
console.log("request params: ", JSON.stringify(params));
//make dynamodb request
dynamodbClient.get(params, function(err, data) {
if (err) {
console.log("error: " + err)
} else {
console.log(data.Item);
}
});

运行上面的代码示例会给我以下错误:

验证异常:提供的键元素与架构不匹配

我的数据集很容易插入,系统中的记录如以下示例所示:

{
"added": 1539231216801,
"description": "A number of ethics complaints filed against Supreme Court Justice Brett Kavanaugh have yet to be resolved and will be transferred to a new jurisdiction, Chief Justice John Roberts wrote in a letter Wednesday. ",
"edited": 1539231216402,
"id": "068d1110-cd0c-11e8-a4d4-57cb772554a4",
"largeImageUrl": "https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2018/10/09/105496148-1539094642099gettyimages-1047769784.1910x1000.jpeg",
"newstitle": "Ethics complaints against Brett Kavanaugh have not been resolved yet",
"newsurl": "https://www.cnbc.com/2018/10/10/ethics-complaints-against-brett-kavanaugh-have-not-been-resolved-yet.html",
"posted": 1539231216402,
"scrapeid": "05c3a690-cd0c-11e8-a4d4-57cb772554a4",
"scrapes": [
"05c3a690-cd0c-11e8-a4d4-57cb772554a4"
],
"source": "www.cnbc.com",
"type": "rate"
}

该表定义如下:

表名称:新闻文章

主分区键:id (字符串(

主排序键:已添加(数字(

错误消息并没有真正告诉我问题是什么。 任何人都可以看到我的请求中有什么问题吗? 有没有其他方法可以诊断可能缺失或不正确的内容? 提前感谢,很抱歉 NOOB 问题,但我只是第一次开始使用 AWS DynamoDB。

您不包括完整的主键,您还需要指定排序键,在这种情况下added

var params = {
TableName: 'newsarticles',
Key: {
id: '068d1110-cd0c-11e8-a4d4-57cb772554a4',
added: aNumberHere
}
};

或者,如果您尝试获取具有特定 ID 的所有记录,则需要使用查询。

相关内容

最新更新