簡體   English   中英

將字典列表上傳到 DynamoDB

[英]Upload list of dictionaries to DynamoDB

我有以下字典列表:

datalist = [
    {'Business': 'Business A', 'Category': 'IT', 'Title': 'IT Manager'},
    {'Business': 'Business A', 'Category': 'Sourcing', 'Title': 'Sourcing Manager'}
]

我想以以下格式上傳到 DynamoDB:

table.put_item(Item={
                    'Business':'Business A would go here',
                    'Category':'IT would go here',
                    'Title':'IT Manager would go here'
                    },
                    {
                    'Business':'Business B would go here',
                    'Category':'Sourcing would go here',
                    'Title':'Sourcing Manager would go here'
                    }
               )

我嘗試先將 dicts 列表轉換為 dict 並以這種方式訪問元素或嘗試遍歷 Items 參數但沒有運氣。 任何幫助,將不勝感激。

這是我的 DynamoDB 結構,3 列(業務、類別、標題):

{
  "Business": {
    "S": "Business goes here"
  },
  "Category": {
    "S": "Category goes here"
  },
  "Title": {
    "S": "Title goes here"
  }
}

put_item API 調用允許您僅上傳一項,但您嘗試同時上傳兩項,這不起作用。

您可以使用 boto3 批量處理多個 put items 請求,以減少 API 調用的數量。 這是改編自文檔的示例:

with table.batch_writer() as batch:

    for item in datalist:

        batch.put_item(
            Item=item
        )

這將在后台自動創建批量寫入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM