如何在无服务器框架中使用全局二级索引定义 DynamoDB table
How to define DynamoDB table with global secondary index in serverless framework
我的 DynamoDB table
- awsRequestID (S)
- ttl (N)
- 创建日期 (S) (ISO)
- user_id (S)
- 留言 (S)
我努力实现的目标
我想要一个全局二级索引,这样我就可以查询以过滤用户的所有消息,并按照 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
中的说明对它们进行排序(使用排序键)
serverless.yml
resources:
Resources:
EventsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.eventsTable}
AttributeDefinitions:
- AttributeName: awsRequestID
AttributeType: S
- AttributeName: ttl
AttributeType: N
- AttributeName: createdate
AttributeType: S
- AttributeName: user_id
AttributeType: S
- AttributeName: message
AttributeType: S
KeySchema:
- AttributeName: awsRequestID
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: UserIdIndex
KeySchema:
- AttributeName: user_id
KeyType: HASH
- AttributeName: createdate
KeyType: RANGE
Projection:
ProjectionType: 'ALL'
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
BillingMode: PAY_PER_REQUEST
部署时出错
An error occurred: EventsTable - Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes.
显然正确的语法是这样的。
这些是错误:
- 您不能将 ttl 列添加到 AttributeDefinitions(否则您会得到问题的错误)
- 您必须在属性定义中包含全局二级索引所需的列(否则您会得到完全相同的错误)
- 您不能在属性定义中有额外的列(消息)(都给出完全相同的错误消息)
resources:
Resources:
EventsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.eventsTable}
AttributeDefinitions:
- AttributeName: awsRequestID
AttributeType: S
- AttributeName: user_id
AttributeType: S
- AttributeName: createdate
AttributeType: S
KeySchema:
- AttributeName: awsRequestID
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: UserIdIndex
KeySchema:
- AttributeName: user_id
KeyType: HASH
- AttributeName: createdate
KeyType: RANGE
Projection:
ProjectionType: 'ALL'
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
BillingMode: PAY_PER_REQUEST
我的 DynamoDB table
- awsRequestID (S)
- ttl (N)
- 创建日期 (S) (ISO)
- user_id (S)
- 留言 (S)
我努力实现的目标
我想要一个全局二级索引,这样我就可以查询以过滤用户的所有消息,并按照 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
中的说明对它们进行排序(使用排序键)serverless.yml
resources:
Resources:
EventsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.eventsTable}
AttributeDefinitions:
- AttributeName: awsRequestID
AttributeType: S
- AttributeName: ttl
AttributeType: N
- AttributeName: createdate
AttributeType: S
- AttributeName: user_id
AttributeType: S
- AttributeName: message
AttributeType: S
KeySchema:
- AttributeName: awsRequestID
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: UserIdIndex
KeySchema:
- AttributeName: user_id
KeyType: HASH
- AttributeName: createdate
KeyType: RANGE
Projection:
ProjectionType: 'ALL'
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
BillingMode: PAY_PER_REQUEST
部署时出错
An error occurred: EventsTable - Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes.
显然正确的语法是这样的。
这些是错误:
- 您不能将 ttl 列添加到 AttributeDefinitions(否则您会得到问题的错误)
- 您必须在属性定义中包含全局二级索引所需的列(否则您会得到完全相同的错误)
- 您不能在属性定义中有额外的列(消息)(都给出完全相同的错误消息)
resources:
Resources:
EventsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.eventsTable}
AttributeDefinitions:
- AttributeName: awsRequestID
AttributeType: S
- AttributeName: user_id
AttributeType: S
- AttributeName: createdate
AttributeType: S
KeySchema:
- AttributeName: awsRequestID
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: UserIdIndex
KeySchema:
- AttributeName: user_id
KeyType: HASH
- AttributeName: createdate
KeyType: RANGE
Projection:
ProjectionType: 'ALL'
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
BillingMode: PAY_PER_REQUEST