dynamodb 如何在 serverless.yml 中定义 none 键模式?

dynamodb how to define none key schema in serverless.yml?

我尝试在我的无服务器 aws lambda 中应用 dynamodb。 我的文件是这样的:

resources:
  Resources:
    StoreDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: lat
            AttributeType: N 
          - AttributeName: lng
            AttributeType: N
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.TableStore}

我尝试将 lat 和 lng 应用为 storeTable 的属性,只是属性而不是 key Schema,但每个 store 元素都应该具有这些属性。

但是有错误:

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

如何使lat和lng只是桅杆属性,而不是索引的关键元素?

DynamoDB 要求您仅声明构成密钥架构的属性。 (See AWS docs)

如果 id 是用于构成密钥架构的唯一属性,则您的资源应如下所示:

resources:
  Resources:
    StoreDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.TableStore}

DynamoDB 不关心其他属性。插入数据后,DynamoDB 将检测新属性,而无需在模式中声明它们。这就是非关系数据库的全部意义所在。


另外,如果你想在你的键模式中有一个日期作为排序键,你可以有这样的东西:

resources:
  Resources:
    StoreDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: date
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: date
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.TableStore}

键架构始终至少有一个分区 (HASH) 键,并且可以选择有一个排序 (RANGE) 键。 Check this to learn more about DynamoDB's key schema.