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?
1 Answer
Reset to default 5When 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
评论列表(0条)