如何将地图插入DynamoDB表?

时间:2017-04-21 08:49:21

标签: python amazon-dynamodb boto3

我有以下代码行:

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列filenamestatus

表的属性定义:

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

我知道status的类型是S,但在创建表时我没有找到地图类型。我找到的只有stringbinarynumber

2 个答案:

答案 0 :(得分:2)

更容易插入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)

答案 1 :(得分:0)

我也对文档关注数据类型感到困惑。 感谢@omuthu的回答,我意识到一个人只需要传递dict对象即可。我认为该问题的答案更为简洁,是通过去除附加的“ M”(地图)级别来简化原来的put_item()调用,即:

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

或作为一个工作示例:

import boto3

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

key="test1235.txt"
iocheckdict = {'A': 'One', 'C': 'Three', 'D': 'Four', 'B': 'Two', 'E': 'Five'}

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