【问题标题】:How to create the dynamodb table using serverless.yml and delete the items of it using python boto3?如何使用 serverless.yml 创建 dynamodb 表并使用 python boto3 删除它的项目?
【发布时间】:2020-10-14 07:55:44
【问题描述】:

我使用 serverless.yml 创建了 dynamodb 表,如下所示:

resources:
  Resources:
    myTable:
      Type: AWS::DynamoDB::Table
      DeletionPolicy: Retain
      Properties:
        TableName: myTable
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: firstname
            AttributeType: S
          - AttributeName: lastname
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: firstname
            KeyType: RANGE
        BillingMode: PAY_PER_REQUEST
        SSESpecification:
          SSEEnabled: true

但我遇到了这个问题:

发生错误:myTable - 一个或多个参数值是 无效:KeySchema 中的属性数不完全匹配 AttributeDefinitions 中定义的属性数(服务: 亚马逊DynamoDBv2;状态码:400;错误代码:验证异常; 请求编号:PEI9OT7E72HQN4N5MQUOIUQ18JVV4KQNSO5AEMVJF66Q9ASUAAJG; 代理:空)。

您能帮我使用 serverless.yml 创建 dynamodb 表吗? 以及如何使用python boto3删除此表中名字为“First”的项目?

【问题讨论】:

    标签: python aws-lambda amazon-dynamodb boto3 serverless


    【解决方案1】:

    如果你想保留你的KeySchema,你必须删除 lastname from AttributeDefinitions:

    Resources:
      myTable:
        Type: AWS::DynamoDB::Table
        DeletionPolicy: Retain
        Properties:
          TableName: myTable
          AttributeDefinitions:
            - AttributeName: id
              AttributeType: S
            - AttributeName: firstname
              AttributeType: S
          KeySchema:
            - AttributeName: id
              KeyType: HASH
            - AttributeName: firstname
              KeyType: RANGE
          BillingMode: PAY_PER_REQUEST
          SSESpecification:
            SSEEnabled: true
    

    但如果你想保留lastname,你可以为你的桌子定义 local secondary index

    Resources:
      myTable:
        Type: AWS::DynamoDB::Table
        DeletionPolicy: Retain
        Properties:
          TableName: myTable
          AttributeDefinitions:
            - AttributeName: id
              AttributeType: S
            - AttributeName: firstname
              AttributeType: S
            - AttributeName: lastname
              AttributeType: S
          KeySchema:
            - AttributeName: id
              KeyType: HASH
            - AttributeName: firstname
              KeyType: RANGE
          LocalSecondaryIndexes:
            - IndexName: by-lastname
              KeySchema: 
              - AttributeName: id
                KeyType: HASH
              - AttributeName: lastname
                KeyType: RANGE
              Projection: 
                ProjectionType: ALL
          BillingMode: PAY_PER_REQUEST
          SSESpecification:
            SSEEnabled: true
    

    通过上述方法,您可以使用 LSI 对 lastname 进行排序,而在主表中将使用 firstname

    如何使用python boto3删除此表中名字为“First”的项目?

    不能直接使用或不使用boto3,除非你想执行scan,应该避免这样做,因为它可能很昂贵而且效率不高。一般的解决方案是定义一个Global Secondary Indexes,其中firstname 将是新的主键。然后,您将在 GSI 中查询感兴趣的 firstname,以获取要删除的记录的 id。如果您有多个具有相同 firstname 的记录(可能会出现这种情况),您会返回许多记录(GSI 主键不需要唯一,与主表不同) .

    LSI 和 GSI 示例表:

    Resources:
      myTable:
        Type: AWS::DynamoDB::Table
        DeletionPolicy: Retain
        Properties:
          TableName: myTable
          AttributeDefinitions:
            - AttributeName: id
              AttributeType: S
            - AttributeName: firstname
              AttributeType: S
            - AttributeName: lastname
              AttributeType: S
          KeySchema:
            - AttributeName: id
              KeyType: HASH
            - AttributeName: firstname
              KeyType: RANGE
          LocalSecondaryIndexes:
            - IndexName: by-lastname
              KeySchema: 
              - AttributeName: id
                KeyType: HASH
              - AttributeName: lastname
                KeyType: RANGE
              Projection: 
                ProjectionType: ALL
          GlobalSecondaryIndexes:
            - IndexName: firstname-gsi
              KeySchema: 
                - AttributeName: firstname
                  KeyType: HASH
              Projection: 
                ProjectionType: ALL
              #ProvisionedThroughput: 
              #  ProvisionedThroughput        
          BillingMode: PAY_PER_REQUEST
          SSESpecification:
            SSEEnabled: true
    

    【讨论】:

      【解决方案2】:

      原因是AttributeDefinitions 中的所有属性名称也必须包含在KeySchema 中。我可以看到那里缺少lastname 属性。

      【讨论】:

      • 能否附上样品?
      猜你喜欢
      • 2016-04-13
      • 2021-09-14
      • 2019-08-05
      • 2018-01-05
      • 1970-01-01
      • 2021-07-24
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多