Amazon dynamodb 如何使用全局二级索引编写get方法DynamoDB?

Amazon dynamodb 如何使用全局二级索引编写get方法DynamoDB?,amazon-dynamodb,aws-amplify,amplifyjs,Amazon Dynamodb,Aws Amplify,Amplifyjs,一般来说,我对dynamoDB和NoSQL都是新手。我使用aws amplify创建此表: { "Table": { "AttributeDefinitions": [ { "AttributeName": "category", "AttributeType": "S" }, { "AttributeNa

一般来说,我对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"
    }
}

我希望只能通过标题获取项目,因此我将此添加到我的app.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获取项目时,会出现以下错误:

{“错误”:“无法加载项:提供的键元素与架构不匹配”}


有什么帮助吗?

在文档中,我没有看到IndexName是一个操作选项。试着改用。

没错。。GSI是原始表中记录的投影。在表中,PK+SK确保您可以检索单个条目,“get”就是这样做的。另一方面,GSI的分区键不能确保记录的唯一性,因为主表上的多个记录在用作GSI PK的属性中可能具有相同的值。因此,扫描和查询是GSI上唯一允许的操作