简体   繁体   English

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

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

I can't find an answer to this but I have this code:我找不到这个问题的答案,但我有这个代码:

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

I'm basically just storing tweets I pull using Tweepy and putting them up on my AWS instance.我基本上只是存储我使用 Tweepy 提取的推文并将它们放在我的 AWS 实例上。 I wrote this to be "easy" for now, but I know that as the number of tweets I get increases, this will just be highly inefficient.我现在写这篇文章是为了“简单”,但我知道随着我收到的推文数量的增加,这将是非常低效的。 Anybody know how I can rewrite this to get it close to linear time?有人知道我如何重写它以使其接近线性时间吗?

You could use the boto3 batch_write_item() method.您可以使用 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'
                        }
                    }
                }
            }
        ]
    }
)

Keep in mind the limits for this operation请记住此操作的限制

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

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