Ano*_*ias 1 python amazon-web-services amazon-dynamodb boto3
我正在通过 CDK 创建 dynamodb 表。
const table = new dynamodb.Table(this, "my-table", {
tableName: StackConfiguration.tableName,
partitionKey: { name: "file_id", type: dynamodb.AttributeType.STRING },
});
dynamoReplayTable.addGlobalSecondaryIndex({
indexName: "processed",
# ideally would like boolean here but doesn't seem to be an option
partitionKey: { name: "processed", type: dynamodb.AttributeType.STRING },
});
Run Code Online (Sandbox Code Playgroud)
然后使用 boto 3 我将数据插入表中,如下所示
failedRecord = {
"file_id": str(file_id),
"processed": "false",
"payload": str(payload),
"headers": str(headers),
}
table.put_item(Item=failedRecord)
Run Code Online (Sandbox Code Playgroud)
然后,我有另一个服务来读取项目,然后进行处理,我想将已处理的字段(全局二级索引)更新为 true。
我现在有这个代码
table.update_item(
Key={"file_id": file_id}, AttributeUpdates={"processed": "true"},
)
Run Code Online (Sandbox Code Playgroud)
但这会导致以下错误
Parameter validation failed:
Invalid type for parameter AttributeUpdates.processed, value: true, type: <class 'str'>, valid types: <class 'dict'>
Run Code Online (Sandbox Code Playgroud)
DynamoDB 以非常具体的方式处理数据类型,您可以在此处和此处找到更多信息。
就您而言,问题在于"true"更新命令的值。使用类型可能很棘手,boto3提供了 和TypeSerializer,TypeDeserializer您可以使用它来处理转换:
import boto3
from boto3.dynamodb.types import TypeSerializer
serializer = TypeSerializer()
my_single_value = "processed"
print(serializer.serialize(my_single_value))
# {'S': 'processed'}
my_dict_object = {
"processed": "true"
}
print({k: serializer.serialize(v) for k, v in my_dict_object.items()})
# {'processed': {'S': 'true'}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7541 次 |
| 最近记录: |