【问题标题】:How to express a Hash + Range in JSON for dynamodb getItem on Node.js如何在 JSON 中为 Node.js 上的 dynamodb getItem 表示 Hash + Range
【发布时间】:2015-10-06 08:25:03
【问题描述】:

我在 dynamodb 中有一个名为 Message 的表。键由一个 Hash(称为 entryStamp 作为一个数字)和一个 Range(作为一个字符串的 userId)组成。

我正在尝试使用 Node.js Lambda 对其进行 getItem。我知道如何使用 Hash only 键为不同的表获取项目,并且它可以工作。

但我找不到有关如何修改我的 json 以表达键的 Range 部分的文档。

var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();
exports.handler = function(event, context) {
    var messageIdParts = event.messageId.split("-");
    var desiredStampString = messageIdParts[0];
    var desiredSendStamp = Number(desiredStampString);
    var userId = messageIdParts[1];

    var params = {
        "ConsistentRead": false,
        "Key": {
            HashKeyElement: { "entryStamp": desiredSendStamp },
            RangeKeyElement: { "userId": userId }
           },
        "ProjectionExpression": "message",
        "ReturnConsumedCapacity": "NONE",
        "TableName": "Message"
    }
    try {
        dynamo.getItem(params, context.done);
    } catch (e) {
        console.log("Exception for getItem: " + e);
    }
};

以下是 DynamoDB“详细信息”选项卡中的表信息:

Table Name: Message
Primary Hash Key:   entryStamp (Number)
Primary Range Key:  userId (String)
Table Status:   Active

这是我传递给 event.messageId 的内容:

{
    "messageId": "1443768744451-testUser"
}

提前致谢! 当我将上述内容发送到 getItem 时,我得到了这个:

{
   "errorMessage": "The provided key element does not match the schema",
   "errorType": "ValidationException",
   "stackTrace": [
        "Request.extractError (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/protocol/json.js:40:27)",
        "Request.callListeners (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/sequential_executor.js:100:18)",
        "Request.emit (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/sequential_executor.js:77:10)",
        "Request.emit (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/request.js:604:14)",
        "Request.transition (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/request.js:21:12)",
        "AcceptorStateMachine.runTo (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/state_machine.js:14:12)",
        "/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/state_machine.js:26:10",
        "Request.<anonymous> (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/request.js:22:9)",
"Request.<anonymous> (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/request.js:606:12)",
        "Request.callListeners (/var/runtime/node_modules/dynamodb-doc/node_modules/aws-sdk/lib/sequential_executor.js:104:18)"
      ]
    }

【问题讨论】:

  • 本例中desiredSendStampuserId 的实际值是多少?您还可以分享Message 表的表属性定义吗?
  • 看起来不错。你确定哈希键是 N 并且 userId 是字符串 (S)?

标签: amazon-web-services amazon-dynamodb aws-lambda


【解决方案1】:

我认为params中的Key定义不太对,你不需要指定HashKeyElementRangeKeyElement。试试:

var params = {
        "ConsistentRead": false,
        "Key": {
            "entryStamp": desiredSendStamp,
            "userId": userId
           },
        "ProjectionExpression": "message",
        "ReturnConsumedCapacity": "NONE",
        "TableName": "Message"
};

此外,如果您升级,现在可以使用 aws-sdk 包中的 DocumentClient class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多