使用DynamoDB/Node.js使用辅助索引分页
我对Dynamodb中全局二级索引的分页有问题:/ 我的DynamoDB模式是:使用DynamoDB/Node.js使用辅助索引分页,node.js,amazon-web-services,pagination,amazon-dynamodb,secondary-indexes,Node.js,Amazon Web Services,Pagination,Amazon Dynamodb,Secondary Indexes,我对Dynamodb中全局二级索引的分页有问题:/ 我的DynamoDB模式是: Resources: ImportsTable: Type: AWS::DynamoDB::Table Properties: # Generate a name based on the stage TableName: ${self:service}-${self:custom.stage}-imports AttributeDefinitions:
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,
fixedKey: event.queryStringParameters.fixedKey,
timestamp: event.queryStringParameters.timestamp
}
因此,对于下一个调用,我在参数中添加了以下内容:
ExclusiveStartKey: {
id: event.queryStringParameters.id,
}
event.queryStringParameters.id看起来是个不错的键
88de14a0-2475-11e9-a0ee-d317558aa61b
但当我运行它时,我得到一个500错误,并显示以下消息:
The provided starting key is invalid
我发现一个潜在客户告诉我将整个LastEvaluatedKey添加到请求中,但当我使用以下内容运行查询时:
LastEvaluatedKey: {
id: 88de14a0-2475-11e9-a0ee-d317558aa61b
fixedKey: fixedKey
timestamp: 1548842283754
}
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
我最终找到了解决方案,时间戳是我模式中的一个数字,所以我需要在params:)中解析它。我最终找到了解决方案,时间戳是我模式中的一个数字,所以我需要在params:)中解析它。你的英语很好,不需要道歉。你的英语很好,不需要道歉。