如何使用 serverless.yml 创建 dynamodb 表并使用 python boto3 删除其中的项目?

5 python amazon-dynamodb boto3 aws-lambda serverless

我使用 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
Run Code Online (Sandbox Code Playgroud)

但我有这个问题:

发生错误:myTable - 一个或多个参数值无效:KeySchema 中的属性数量与 AttributeDefinitions 中定义的属性数量不完全匹配(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;请求 ID:PEI9OT7E72HQN4N5MQUOIUQ18JVV4KQNSO5AEMVJF66Q9ASUAAJG;代理: 无效的)。

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

Mar*_*cin 4

如果你想保留你的,KeySchema必须从: lastnameAttributeDefinitions

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
Run Code Online (Sandbox Code Playgroud)

但如果你想保留lastname,你可以为你的表定义 本地二级索引

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
Run Code Online (Sandbox Code Playgroud)

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

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

不管有没有 boto3,你都不能直接执行此操作,除非你想执行scan,应该避免这样做,因为它可能很昂贵并且效率不高。一般的解决方案是定义一个全局二级索引,其中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
Run Code Online (Sandbox Code Playgroud)