繁体   English   中英

使用 Boto3 以有效的方式将项目行添加到 DynamoDB

[英]Adding Rows of items to DynamoDB using Boto3 in an efficient way

我找不到这个问题的答案,但我有这个代码:

def putItem(table_name):
items = [['4/20/20','4/21/20'],['12345','01100111']]
sz = len(items[0])
i =0
while i < sz:
    add = dynamodb.put_item(
        TableName = table_name,
        Item ={
            'dates' : {
            'S': items[0][i]
            },
            'tweet_id':{
            'S': items[1][i]
            }
        }
    )
    i+=1

我基本上只是存储我使用 Tweepy 提取的推文并将它们放在我的 AWS 实例上。 我现在写这篇文章是为了“简单”,但我知道随着我收到的推文数量的增加,这将是非常低效的。 有人知道我如何重写它以使其接近线性时间吗?

您可以使用 boto3 batch_write_item()方法。

import boto3

dynamodb = boto3.client('dynamodb')

response = dynamodb.batch_write_item(
    RequestItems={
        'my-table-name': [
            {
                'PutRequest': {
                    'Item': {
                        'dates' : {
                            'S': '4/20/20'
                        },
                        'tweet_id':{
                            'S': '12345'
                        }
                    }
                }
            },
            {
                'PutRequest': {
                    'Item': {
                        'dates' : {
                            'S': '4/20/20'
                        },
                        'tweet_id':{
                            'S': '01100111'
                        }
                    }
                }
            }
        ]
    }
)

请记住此操作的限制

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM