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

How do I request a single record using AWS DynamoDB? Code samples are not working

我已经从最基础的部分获取了代码示例,但我仍然无法通过密钥检索单个记录。这是我使用 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);
    }
});

运行 上述示例代码给出了以下错误:

ValidationException:提供的关键元素与模式不匹配

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

{
  "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"
}

table的定义是这样的:

Table 名称:newsarticles

主分区键:id(字符串)

主排序键:已添加(数量)

错误消息并没有真正告诉我问题出在哪里。任何人都可以看到我的请求有什么问题吗?是否有另一种方法来诊断可能丢失或不正确的内容?提前致谢,对于 NOOB 问题,我深表歉意,但我是第一次使用 AWS DynamoDB。

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

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

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