2. Loading Sample DataΒΆ
awstin.dynamodb.Table.put_item()
takes instances of the data model
to insert into DynamoDB after serialization.
awstin
handles conversions between float
and Decimal
internally,
so float
can be used when instantiating data models.
import json
from models import Movie
from awstin.dynamodb import DynamoDB
def load_movies(movies):
dynamodb = DynamoDB()
table = dynamodb[Movie]
for movie_json in movies:
movie = Movie(
title=movie_json["title"],
year=movie_json["year"],
info=movie_json["info"],
)
table.put_item(movie)
if __name__ == "__main__":
with open("moviedata.json", "r") as json_file:
movie_list = json.load(json_file)
load_movies(movie_list)
awstin.dynamodb.DynamoModel
has
awstin.dynamodb.DynamoModel.serialize()
and
awstin.dynamodb.DynamoModel.deserialize()
methods to convert between
DynamoDB representations of data and the data models, and can be used to read
data from JSON.
import json
from models import Movie
from awstin.dynamodb import DynamoDB
def load_movies(movies):
dynamodb = DynamoDB()
table = dynamodb[Movie]
for movie in movies:
table.put_item(movie)
if __name__ == "__main__":
with open("moviedata.json", "r") as json_file:
movie_list = json.load(json_file)
load_movies([Movie.deserialize(movie) for movie in movie_list])