简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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