Skip to main content
 首页 » 编程设计

python之如何将 map 插入 DynamoDB 表

2024年08月20日34shanyou

我有以下代码行:

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

iocheckdict 如下所示:

{'A': 'One', 'C': 'Three', 'D': 'Four', 'B': 'Two', 'E': 'Five'} 

所以,当我运行代码时,出现了这个错误:

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:我的表中有 2 列 filename 和 status


表的属性定义:

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

我理解status的类型是S,但是建表的时候没找到 map 类型。我找到的只有 string、binary 和 number。

请您参考如下方法:

更容易插入到 dynamodb 中

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)