使用 DynamoDB/Node.js 进行二级索引的分页



>我对 Dynamodb 中全局二级索引的分页有问题:/

我的 DynamoDB 架构为:

Resources:
  ImportsTable:
    Type: AWS::DynamoDB::Table
    Properties:
      # Generate a name based on the stage
      TableName: ${self:service}-${self:custom.stage}-imports
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
        - AttributeName: fixedKey
          AttributeType: S
        - AttributeName: timestamp
          AttributeType: N
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      StreamSpecification:
        StreamViewType: NEW_IMAGE
      ProvisionedThroughput:
        ReadCapacityUnits: ${self:custom.app.tableThroughput.imports}
        WriteCapacityUnits: ${self:custom.app.tableThroughput.imports}
      TimeToLiveSpecification:
        AttributeName: expirationTime
        Enabled: true
      GlobalSecondaryIndexes:
        - IndexName: time-index
          KeySchema:
            - AttributeName: fixedKey
              KeyType: HASH
            - AttributeName: timestamp
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
          ProvisionedThroughput:
            ReadCapacityUnits: ${self:custom.app.tableThroughput.imports}
            WriteCapacityUnits: ${self:custom.app.tableThroughput.imports}
      SSESpecification:
        SSEEnabled: true

我的查询参数:

let params = {
            TableName: process.env.importsTableName,
            IndexName: 'time-index',
            KeyConditionExpression: 'fixedKey = :fk',
            Limit: 5,
            ProjectionExpression: "timeBasedId, importFileS3Key, meta, #st, #ch, success, errors, #ty, #id, email",
            ScanIndexForward: true,
            ExpressionAttributeNames: {
                "#ch": "Attributes",
                "#st": "status",
                "#ty": "type",
                "#id": "identity",
            },
            ExpressionAttributeValues: {
                ":fk" : "fixedKey",
            },
 };

当我运行这个时,我得到这样的响应:

   LastEvaluatedKey: {
    id: 88de14a0-2475-11e9-a0ee-d317558aa61b
    fixedKey: fixedKey
    timestamp: 1548842283754
}

所以对于下一次调用,我在我的参数中添加了这个:

ExclusiveStartKey: {
                    id: event.queryStringParameters.id,
                }

event.queryStringParameters.id 看起来像一把好钥匙

88de14a0-2475-11e9-a0ee-d317558aa61b

但是当我运行它时,我收到 500 错误和以下消息:

The provided starting key is invalid

我找到了一个线索,告诉我将整个 LastEvaluatedKey 添加到请求中,但是当我用这个运行查询时:

ExclusiveStartKey: {
                    id: event.queryStringParameters.id,
                    fixedKey: event.queryStringParameters.fixedKey,
                    timestamp: event.queryStringParameters.timestamp
                }

当 event.queryStringParameters 看起来像这样时:

{ fixedKey: 'fixedKey',
id: '88de14a0-2475-11e9-a0ee-d317558aa61b',
timestamp: '1548842283754' }

我收到此错误:

The provided key element element does not math the schema

我最终找到了解决方案,时间戳是我的架构中的一个数字,所以我需要在参数中解析 Int() :)

最新更新