Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boto3 Dynamodb table batch insert missing something

I expected something like this to work. I am adding 26 items to a dynamo db using boto3 interface.

But I am missing something because the code reports AttributeError: 'str' object has no attribute 'batch_write_item' right at the 25th insert (which should have auto-flushed the buffer)

from boto3.dynamodb import table
items = [
{'key': 1, u'timestamp': '1493269200000'},
{'key': 2, u'timestamp': '1493269200000'},
{'key': 3, u'timestamp': '1493269200000'},
{'key': 4, u'timestamp': '1493269200000'},
{'key': 5, u'timestamp': '1493269200000'},
{'key': 6, u'timestamp': '1493269200000'},
{'key': 7, u'timestamp': '1493269200000'},
{'key': 8, u'timestamp': '1493269200000'},
{'key': 9, u'timestamp': '1493269200000'},
{'key': 10, u'timestamp': '1493269200000'},
{'key': 11, u'timestamp': '1493269200000'},
{'key': 12, u'timestamp': '1493269200000'},
{'key': 13, u'timestamp': '1493269200000'},
{'key': 14, u'timestamp': '1493269200000'},
{'key': 15, u'timestamp': '1493269200000'},
{'key': 16, u'timestamp': '1493269200000'},
{'key': 17, u'timestamp': '1493269200000'},
{'key': 18, u'timestamp': '1493269200000'},
{'key': 19, u'timestamp': '1493269200000'},
{'key': 20, u'timestamp': '1493269200000'},
{'key': 21, u'timestamp': '1493269200000'},
{'key': 22, u'timestamp': '1493269200000'},
{'key': 23, u'timestamp': '1493269200000'},
{'key': 24, u'timestamp': '1493269200000'},
{'key': 25, u'timestamp': '1493269200000'},
{'key': 26, u'timestamp': '1493269200000'}
]

with table.BatchWriter('my_tbl_name',"us-east-1") as tbl:
    for r in items:
        tbl.put_item(r)

I have also tried with tbl.put_item(str(r)) and a few other things like JSON encode... but no luck. Does anyone know of a simple working example? Yes I looked but there is lots of mixing between Boto and Boto3.

like image 628
user2315423 Avatar asked Apr 28 '17 21:04

user2315423


1 Answers

All I needed was an instance of a Table The camel BatchWriter() was giving me the wrong kind of object for the context. I needed batch_writer()

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('staging.tstable3')
print(table.creation_date_time)

with table.batch_writer() as batch:
    for r in items:
        batch.put_item(Item=r)
like image 179
user2315423 Avatar answered Sep 22 '22 20:09

user2315423