【问题标题】:How to query table by non primary key attribute in Amazons DynamoDB with DocumentClient.get如何使用 DocumentClient.get 在 Amazons DynamoDB 中按非主键属性查询表
【发布时间】:2018-08-18 06:08:42
【问题描述】:

我似乎在使用 AWS.DynamoDB.DocumentClient().get() 查询数据时遇到了很多困难。

我正在使用无服务器并使用此架构设置我的 serverless.yml

resources:
  Resources:
    ShortUrlsTable:
      Type: "AWS::DynamoDB::Table"
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: longUrl
            AttributeType: S
          - AttributeName: shortPath
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: longUrlIndex
            KeySchema:
              - AttributeName: longUrl
                KeyType: HASH
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
            Projection:
              ProjectionType: ALL
          - IndexName: shortPathIndex
            KeySchema:
              - AttributeName: shortPath
                KeyType: HASH
            Projection:
              ProjectionType: ALL
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:custom.tableName}

我希望能够使用longUrlshortPath 在数据库中搜索shortUrlItem

到目前为止,我已经设置好了:

dynamoDb = new AWS.DynamoDB.DocumentClient()

app.get("/:longUrl", (req, res) => {
  const {longUrl} = req.params

  const getParams = {
    TableName: SHORT_URLS_TABLE,
    Key: {longUrl},
  }

  dynamoDb.get(getParams, (error, result) => {
    res.send({...error, ...result})
  })
})

我似乎得到的只是这条错误消息返回给我:

"message":"The provided key element does not match the schema","code":"ValidationException","time":"2018-08-17T20:39:27.765Z","requestId":"4RKNVG7ET1ORVF10H71M7AUABRVV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":21.513795782119505,"TableName":"short-urls-table-dev"

我似乎无法确定我是否正确查询或正确设置架构以使二级索引成为表中的可搜索键。

【问题讨论】:

    标签: amazon-dynamodb serverless-framework


    【解决方案1】:

    我可以看到两个错误

    1:您的 getParams 错误。您在 PK 上提出 get 请求,但您在参数部分提供了 GSI 密钥。应该是这样的

    const getParams = {
      TableName: SHORT_URLS_TABLE,
      Key: {
        id: id, // Because id is the attribute of your HASH key. 
      }
    }
    

    这是错误的原因。您的哈希键不在属性 longUrl 上。

    2:无论如何,你不能在 GSI 上发出get 请求。它没有 GSI 的设计。 GSI 不强制唯一性,因此同一个 GSI 哈希键中可以有多个项目,因此您只能使用 query 而不是 get

    你正在尝试做的事情是这样的

    const queryParams = {
        TableName: SHORT_URLS_TABLE,
        IndexName: 'longUrlIndex',
        KeyConditionExpression: 'longUrl = :longUrlValue',
        ExpressionAttributeValues: {
          'longUrlValue': longUrl
        }
    };
    
    dynamoDb.query(queryParams, (error, result) => {
      res.send({...error, ...result})
    })
    

    【讨论】:

    • 嘿,谢谢,我会试一试看看它的作用,我找到了一个类似于你提供的解决方案,但它仍然建议我可以使用get。那我还需要 GSI 吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多