如果你想保留你的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