【问题标题】:Pagination in DynamoDB with Global Secondary Indices在 DynamoDB 中使用全局二级索引进行分页
【发布时间】:2016-11-11 18:56:58
【问题描述】:

我的 DynamoDB 架构是:

TableName = 'Cities'
PrimaryIndex = '_id'
SecondaryIndex = 'areaId-timestamp-index'
  - areaId is primary key
  - timestamp is sort key

DynamoDB 中的数据结构如下所示:

{ _id: '....', name: 'New York', areaId: 22, timestamp: 1478882557071 },
{ _id: '....', name: 'Washington', areaId: 22, timestamp: 1478882557071 },
{ _id: '....', name: 'Copenhagen', areaId: 18, timestamp: 1478882557071 },
{ _id: '....', name: 'Berlin', areaId: 12, timestamp: 1478882557071 },

我希望能够在以下条件下对城市进行分页:

  1. 仅限城市,fx,id 为 22 的地区
  2. 必须按照时间戳顺序分页

我的查询如下所示:

const areaId = 22

const params = {
  TableName: 'Cities',
  Limit: 10,
  IndexName: 'areaId-timestamp-index',
  KeyConditionExpression: 'areaId = :x',
  ExpressionAttributeValues: {
    ':x': areaId
  }
}

dynamo.query(params)

当我运行它时,我得到了预期的响应,lastEvaluatedKey

LastEvaluatedKey = {
    areaId: 22,
    _id: '8c43-d917-f9ec',  // ID of last item in batch
    timestamp: 1478882561962  // Timestamp of last item in batch 
}

当我随后运行下一个后续查询(以获得更多结果)时,我添加了 ExclusiveStartKey 属性,其值与 LastEvaluatedKey 中返回的值完全相同。

但是,当我运行查询时,我得到:

The provided key element does not match the schema

【问题讨论】:

    标签: node.js amazon-web-services pagination amazon-dynamodb


    【解决方案1】:

    尝试将 LastEvaluatedKey 提供给 ExclusiveStartKey,您必须以任何格式提供您收到的全部值。

    const areaId = 22
    const params = {
      TableName: 'Cities',
      Limit: 10,
      LastEvaluatedKey:response.ExclusiveStartKey, // Get it from previous query respose and use in same format
      IndexName: 'areaId-timestamp-index',
      KeyConditionExpression: 'areaId = :x',
      ExpressionAttributeValues: {
        ':x': areaId
      }
    }
    

    希望这会有所帮助, 谢谢

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多