【问题标题】:dynamodb how to define none key schema in serverless.yml?dynamodb 如何在 serverless.yml 中定义无键模式?
【发布时间】:2019-02-24 05:42:26
【问题描述】:

我尝试在我的无服务器 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 元素都应该有这些属性。

但是有错误:

发生错误:StoreDynamoDbTable - 属性 AttributeDefinitions 与表的KeySchema和二级不一致 索引。

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

【问题讨论】:

    标签: amazon-dynamodb serverless-framework serverless


    【解决方案1】:

    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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-30
      • 2018-04-29
      • 2018-12-18
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 2017-08-01
      • 2018-12-07
      相关资源
      最近更新 更多