问题描述
我已经使用 serverless.yml 创建了 dynamodb 表,如下所示:
I've created the dynamodb table using serverless.yml as below:
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
但是我遇到了这个问题:
But I've got this issue:
发生错误:myTable - 一个或多个参数值是无效:KeySchema 中的属性数量不完全匹配AttributeDefinitions 中定义的属性数量(服务:AmazonDynamoDBv2;状态码:400;错误代码:验证异常;请求 ID:PEI9OT7E72HQN4N5MQUOIUQ18JVV4KQNSO5AEMVJF66Q9ASUAAJG;代理:空).
你能帮我使用 serverless.yml 创建 dynamodb 表吗?以及如何删除名字为First"的项目?在这个表中使用 python boto3?
Could you help me creating the dynamodb table using serverless.yml?And how can I delete the items that first name is "First" in this table using python boto3?
推荐答案
如果你想保留你的 KeySchema
你必须drop lastname
来自 AttributeDefinitions
:
If you want to keep your KeySchema
you have to drop 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
,您可以定义 您的表的本地二级索引:
But if you want to keep lastname
, you could define local secondary index for your table:
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
将用于主表.
With the above, you can sort for lastname
using the LSI, while firstname
would be used in the primary table.
如何删除名字为First"的项目?在这个表中使用 python boto3?
您不能直接使用或不使用 boto3,除非您想执行 scan,应该避免这样做,因为它既昂贵又效率低下.一般的解决方案是定义一个全球二级索引,其中firstname
将是新的主键.然后,您将查询感兴趣的 firstname
的 GSI,以获取要删除的记录的 id
.如果您有多个具有相同 firstname
的记录(可能是这种情况),您会返回许多记录(GSI 主键不需要唯一,不像用于主表).
You can't do it directly with or without boto3, unless you want to perform scan, which should be avoided as it can be expensive and is not efficient. To general solution is to define a Global Secondary Indexes where the firstname
would be the new primary key. Then you would query the GSI for the firstname
of interest to obtain the id
of the record you want to delete. If you have several records with same firstname
, which probably will be the case, you get a number of records back (GSI primary keys don't need to be unique, unlike for the primary table).
带有 LSI 和 GSI 的示例表:
Example table with LSI and 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
这篇关于如何使用 serverless.yml 创建 dynamodb 表并使用 python boto3 删除其中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!