javascript - DynamoDB can't get item by range key? - Stack Overflow

I'm trying to get a single record from a DynamoDB table using a column defined as a RANGE key, but

I'm trying to get a single record from a DynamoDB table using a column defined as a RANGE key, but when I do I get this error:

The provided key element does not match the schema

Here is how I'm creating and seeding the table:

// Create words table
if(!tableExists('words')) {
  console.log('Creating words table');
  await createTable({
    TableName: 'words',
    KeySchema: [
      { AttributeName: 'id', KeyType: 'HASH' },
      { AttributeName: 'index', KeyType: 'RANGE' },
    ],
    AttributeDefinitions: [
      { AttributeName: 'id', AttributeType: 'S' },
      { AttributeName: 'index', AttributeType: 'N' },
    ],
    ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
  });
  await wait(5000);
  console.log('done');
} else {
  console.log('words table found. Skipping.')
}

// Seed words
let index = 0;
for(let word of words) {
  console.log(`Adding word ${word}`);
  const params = {
    TableName: tableName('words'),
    Item: {
      id: word,
      index: index,
    },
  };
  await db.put(params).promise();
  console.log('added');
  index++;
}

And here's how I'm trying to fetch the record:

const db = require('../db');
const getResponseItem = response => response.Item;

module.exports = function loadWordByIndex(index) {
  return db.get({
    TableName: 'talk_stem.words',
    Key: {
      index,
    },
  })
  .promise()
  .then(getResponseItem);
};

What's the point of defining a RANGE key if I can't even reference it in queries?

I'm trying to get a single record from a DynamoDB table using a column defined as a RANGE key, but when I do I get this error:

The provided key element does not match the schema

Here is how I'm creating and seeding the table:

// Create words table
if(!tableExists('words')) {
  console.log('Creating words table');
  await createTable({
    TableName: 'words',
    KeySchema: [
      { AttributeName: 'id', KeyType: 'HASH' },
      { AttributeName: 'index', KeyType: 'RANGE' },
    ],
    AttributeDefinitions: [
      { AttributeName: 'id', AttributeType: 'S' },
      { AttributeName: 'index', AttributeType: 'N' },
    ],
    ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
  });
  await wait(5000);
  console.log('done');
} else {
  console.log('words table found. Skipping.')
}

// Seed words
let index = 0;
for(let word of words) {
  console.log(`Adding word ${word}`);
  const params = {
    TableName: tableName('words'),
    Item: {
      id: word,
      index: index,
    },
  };
  await db.put(params).promise();
  console.log('added');
  index++;
}

And here's how I'm trying to fetch the record:

const db = require('../db');
const getResponseItem = response => response.Item;

module.exports = function loadWordByIndex(index) {
  return db.get({
    TableName: 'talk_stem.words',
    Key: {
      index,
    },
  })
  .promise()
  .then(getResponseItem);
};

What's the point of defining a RANGE key if I can't even reference it in queries?

Share Improve this question edited Nov 29, 2018 at 18:23 SimpleJ asked Nov 29, 2018 at 18:03 SimpleJSimpleJ 14.8k13 gold badges61 silver badges96 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

When you make a get you can only ask for an item. Get returns (or not) one single item which corresponds to the full key, otherwise "The provided key element does not match the schema". Example:

const id = 'marmelade';
const index = 5;

db.get({
  TableName: 'talk_stem.words',
  Key: {
    id,
    index,
  },
}).promise()

With get you look for one item!

What you need is a query (see doc here). You can imagine something like this:

db.query({
  TableName: 'talk_stem.words',
  KeyConditionExpression: '#id = :id AND #index BETWEEN :indexLow AND :indexHigh',
  ExpressionAttributeNames: {
    '#id': 'id',
    '#index': 'index',
  },
  ExpressionAttributeValues: {
    ':id': id, # a fixed id
    ':indexLow': 3,
    ':indexHigh': 9,
  },
}).promise()

Keep in mind that with DynamoDB get and query need to mention the partition key. Always. When you want to get items for partition keys you do not know, you can only do an "expensive" scan.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342940a4623411.html

相关推荐

  • javascript - DynamoDB can't get item by range key? - Stack Overflow

    I'm trying to get a single record from a DynamoDB table using a column defined as a RANGE key, but

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信