如何编写具有全局二级索引的 Get方法 DynamoDB?



我是dynamoDB和NoSQL的新手。我使用 aws amplify 创建了这个表:

{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "category",
"AttributeType": "S"
},
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "title",
"AttributeType": "S"
}
],
"TableName": "products2-prod",
"KeySchema": [
{
"AttributeName": "category",
"KeyType": "HASH"
},
{
"AttributeName": "title",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": 1574728653.881,
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:xxxxx:table/products2-prod",
"TableId": "7c3ae2a1-cef1-4e52-85e5-b8ef543b0d30",
"GlobalSecondaryIndexes": [
{
"IndexName": "byId",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"IndexSizeBytes": 0,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:xxxxx:table/products2-prod/index/byId"
},
{
"IndexName": "byTitle",
"KeySchema": [
{
"AttributeName": "title",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"IndexSizeBytes": 0,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:xxxxx:table/products2-prod/index/byTitle"
}
],
"StreamSpecification": {
"StreamEnabled": true,
"StreamViewType": "NEW_IMAGE"
},
"LatestStreamLabel": "2019-11-26T00:37:33.881",
"LatestStreamArn": "arn:aws:dynamodb:xxxxx:table/products2-prod/stream/2019-11-26T00:37:33.881"
}
}

我希望只能通过标题获取项目,因此我将其添加到我的应用程序.js文件中:

app.get(path + '/name'  + '/:title', function(req, res) {
let params = {}
try {
params["title"] = convertUrlType(req.params["title"], "S");
} catch (err) {
res.json({ error: 'Wrong column type ' + err });
}
let getItemParams = {
TableName: tableName,
IndexName: "byTitle",
Key: params
}
dynamodb.get(getItemParams,(err, data) => {
if(err) {
res.statusCode = 500;
res.json({error: 'Could not load items: ' + err.message});
} else {
if (data.Item) {
res.json(data.Item);
} else {
res.json(data) ;
}
}
});
});

但是,当我尝试使用 byTitle GSI 获取项目时,我收到此错误:

{"error":"无法加载项目:提供的键元素与架构不匹配"}

有什么帮助吗?

在文档中,我没有看到 IndexName 是获取操作的选项。尝试改用查询。

相关内容

  • 没有找到相关文章

最新更新