7

我正在使用基础架构即代码在 CloudFormation 中实施 GSI。我想要做的就是使用这个表来记录主 DynamoTable 中的条目数。这是主要故事的样子:

Resources:
  CaseRecords:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: ${self:custom.tableName}
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: userId
          AttributeType: S
        - AttributeName: caseRecordId
          AttributeType: S
      KeySchema:
        - AttributeName: userId
          KeyType: HASH
        - AttributeName: caseRecordId
          KeyType: RANGE

我不需要原始表中的键,我想要的只是为新 GSI 创建一个新的 HASH 键,它会告诉我我正在跟踪的计数来自哪个表,即上面的表。

以下是我迄今为止尝试实施 GSI 的方式:

# Implement a GSI to handle item count totals
      GlobalSecondaryIndexes:
          - IndexName: gsiCaseCountTable
            KeySchema: 
              - AttributeName: table-name
                KeyType: HASH
            ProvisionedThroughput:
              ReadCapacityUnits: 5
              WriteCapacityUnits: 5

但是,我得到的错误如下:

An error occurred: CaseRecords - Property Projection cannot be empty..

当我包含我曾经的 PROJECTION 时,仅userId来自原始表的只是为了跟踪每个用户在原始表中的条目计数,我尝试以下操作:

 Implement a GSI to handle item count totals
      GlobalSecondaryIndexes:
      - IndexName: gsiCaseCountTable
        KeySchema:
        - AttributeName: table-name
          KeyType: HASH
        Projection:
          NonKeyAttributes:
          - userId
          ProjectionType: INCLUDE
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5

但是,这也会返回错误:

An error occurred: CaseRecords - Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes.

如何使用 CloudFormation 模板在 Dynamo 中正确实现全局二级索引,以便在原始表中记录条目数????

谢谢。

更新

如果有人想知道这就是我能够部署它的方式。这不是一个完美的解决方案,但它让我可以跟踪并计算项目表中的条目:

# NOTE: DynamoDB Serverless Configuration
# NoSQL Table for CaseRecord DB

Resources:
  CaseRecords:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: ${self:custom.tableName}
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: userId
          AttributeType: S
        - AttributeName: caseRecordId
          AttributeType: S
      KeySchema:
        - AttributeName: userId
          KeyType: HASH
        - AttributeName: caseRecordId
          KeyType: RANGE
      # Set the capacity based on the stage
      # ProvisionedThroughput:
        # ReadCapacityUnits: ${self:custom.tableThroughput}
        # WriteCapacityUnits: ${self:custom.tableThroughput}
      # Implement a GSI to handle item count totals
      GlobalSecondaryIndexes:
      - IndexName: gsiCaseCountTable
        KeySchema:
        - AttributeName: userId
          KeyType: HASH
        Projection:
          ProjectionType: KEYS_ONLY

更新 #2 - 失败

根据下面@Pedro Arantes 提供的信息,我正在尝试使用我想要使用的属性定义来实现 GSI。然而,这也失败了。下面是实施,这是我使用的 AWS Doc 的链接:AWS GSI Doc,这是失败的实施:

Resources:
  CaseRecords:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: ${self:custom.tableName}
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: userId
          AttributeType: S
        - AttributeName: caseRecordId
          AttributeType: S
        - AttributeName: table-name
          AttributeType: S
        - AttributeName: count
          AttributeType: N
      KeySchema:
        - AttributeName: userId
          KeyType: HASH
        - AttributeName: caseRecordId
          KeyType: RANGE
      # Set the capacity based on the stage
      # ProvisionedThroughput:
        # ReadCapacityUnits: ${self:custom.tableThroughput}
        # WriteCapacityUnits: ${self:custom.tableThroughput}
      # Implement a GSI to handle item count totals
      GlobalSecondaryIndexes:
      - IndexName: gsiCaseCountTable
        KeySchema:
        - AttributeName: table-name
          KeyType: HASH
        Projection: 
          NonKeyAttributes: 
            - userId
            - count
          ProjectionType: INCLUDE

我怎样才能让它只与NonKeyAttributes我在AttributeDefinitions???

4

1 回答 1

11

您需要添加table-nameatAttributeDefinitions属性。来自文档

属性定义

描述表和索引的键模式的属性列表。允许重复。

因此,即使您不使用原始表中的某些属性,您也必须声明能够在您的 GSI 中使用。

更新 #2 - 失败

您正在使用键属性userIdcount并且您在 处定义AttributeDefinitionsNonKeyAttributes(但它们键属性)Projection。您不需要添加它们,因为它们是自动投影的。来自文档

AWS::DynamoDB::表投影

表示从表复制(投影)到索引中的属性。这些是自动投影的主键属性和索引键属性的补充。

最终模板

Resources:
  CaseRecords:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: ${self:custom.tableName}
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: userId
          AttributeType: S
        - AttributeName: caseRecordId
          AttributeType: S
        - AttributeName: table-name
          AttributeType: S
      KeySchema:
        - AttributeName: userId
          KeyType: HASH
        - AttributeName: caseRecordId
          KeyType: RANGE
      # Set the capacity based on the stage
      # ProvisionedThroughput:
      # ReadCapacityUnits: ${self:custom.tableThroughput}
      # WriteCapacityUnits: ${self:custom.tableThroughput}
      # Implement a GSI to handle item count totals
      GlobalSecondaryIndexes:
        - IndexName: gsiCaseCountTable
          KeySchema:
            - AttributeName: table-name
              KeyType: HASH
          Projection:
            NonKeyAttributes:
              - count
            ProjectionType: INCLUDE

注意事项:

  1. count不应该打开,AttributeDefinitions因为您没有将它用作键。

  2. 你不需要添加userIdatProjection因为它会自动成为项目,因为它是在AttributeDefinitions.

于 2019-06-14T17:23:21.307 回答