2015-10-02 42 views
1

我在dynamodb中有一個名爲Message的表。密鑰由一個哈希(稱爲entryStamp作爲一個數字)和一個範圍(userId作爲一個字符串)組成。如何在Node.js上爲dynamodb getItem在JSON中表示哈希+範圍

我想使用Node.js Lambda對它做一個getItem。我知道如何使用哈希唯一鍵爲不同的表獲取項目,並且工作正常。

但我找不到有關如何修改我的JSON來表示鍵的範圍部分的文檔。

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)" 
     ] 
    } 
+0

這個例子中'desiredSendStamp'和'userId'的實際值是什麼?你還可以共享「Message」表的表屬性定義嗎? – mkobit

+0

看起來沒問題。你確定散列鍵是N並且userId是一個字符串(S)? – Mircea

回答

0

我覺得在params的按鍵定義是不完全正確,你並不需要指定HashKeyElementRangeKeyElement。嘗試:

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

此外,您還可以在AWS-SDK包中使用該DocumentClient class現在,如果升級。