如何在DynamoDB表中插入映射?

2024-10-01 15:46:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码行:

table.put_item( Item={'filename' : key, 'status' : {'M' : iocheckdict }})

iocheckdict如下所示:

^{pr2}$

所以,当我运行代码时,我得到了一个错误:

An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Type mismatch for key status expected: S actual: M

尽管我提到M作为数据类型,但我为什么要得到这个呢?

PS:我的表中有两列filenamestatus


表的属性定义:

"AttributeDefinitions": [
    {
        "AttributeName": "filename",
        "AttributeType": "S"
    },
    {
        "AttributeName": "status",
        "AttributeType": "S"
    }
],

我知道status的类型是S,但是我在创建表时没有找到映射类型。我找到的只有stringbinary和{}。在


Tags: key代码an类型putstatus错误table
2条回答

插入发电机B的简便方法

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("table_name")

item={}
item['filename'] = key
item['status'] = {'A': 'One', 'C': 'Three', 'D': 'Four', 'B': 'Two', 'E': 'Five'}

table.put_item(Item=item)

我也被关注数据类型的doc弄糊涂了。 多亏了@omuthu的回答,我意识到只需要传入dict对象。我认为对这个问题的一个稍微简洁的回答是通过去掉“M”(map)附加级别来简化原始的put_item()调用,即:

table.put_item( Item={'filename':key, 'status':iocheckdict} )

或者作为一个有效的例子:

^{pr2}$

相关问题 更多 >

    热门问题