浏览器中的dynamodb入门

时间:2016-03-09 19:57:56

标签: amazon-web-services aws-sdk

我对从dynamodb检索数据感到困惑...连接不是问题因为我得到The provided key element does not match the schema

the example provided from AWS

var table = new AWS.DynamoDB({params: {TableName: 'MY_TABLE'}});
var key = 'UNIQUE_KEY_ID';
var itemParams = {Item: {id: {S: key}, data: {S: 'data'}}};

  table.getItem({Key: {id: {S: key}}}, function(err, data) {
    console.log(data.Item); // print the item data
  });

enter image description here

在我的情况下,唯一的关键是"时间"而我想做的是按键检索(不是唯一的)

1 个答案:

答案 0 :(得分:0)

getItem仅适用于主键。从文档: GetItem操作返回具有给定键的项的一组属性。请参阅docs

要解决此问题,请创建一个具有" key"的全局二级索引。作为HASH和"时间"作为范围。然后使用该索引执行查询操作IndexName

var params = {
  IndexName: 'your-new-GSI-index',
  KeyConditionExpression: '#key = :key',
  ExpressionAttributeNames: { '#key': 'key },
  ExpressionAttributeValues: { ':key': { S: yourKeyVar } }
}
table.query(params, callback);

(没有测试此代码,但应该可以工作)