2017-04-21 29 views
1

我有下面的代碼行:如何將地圖插入到DynamoDB表中?

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

+0

根據http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item你必須通過一個M,例如'「M」:{「Name」:{「S」:「Joe」},「Age」:{「N」:「35」}}',換句話說'dict'部分應該是例如'{「Name」:{「S」:「Joe」},「Age」:{「N」:「35」}}'。 –

+0

@EmreSevinç我把字典變成了'Iocheckdict = {「S1」:{「N」:「0」},「S2」:{「N」:「0」},「S3」:{「N」: 「0」},「S4」:{「N」:「0」},「S5」:{「N」:「0」}}'。但是,這個聲明仍然是:'table.put_item(Item = {'filename':key,'status':{'M':iocheckdict}})'給了我同樣的錯誤。可以寫一個答案? – Dawny33

+0

如果您編輯了問題並添加了有關表格的詳細信息,例如表格模式的細節。因爲我認爲你的字段'filename'和'status'的類型可能不符合你的變量'key'和'iocheckdict'的類型。 –

回答

1

更簡單的方式插入到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) 
相關問題