In this step, you bulk-load some data into the DynamoDB you created in the preceding step. This means that in succeeding steps, you will have sample data to use.
In the scripts/directory, you will find a file called items.json. This file contains 835 example items that were randomly generated for this lab. These items include User
, Game
, and UserGameMapping
entities. Open the file if you want to see some of the example items.
The scripts/ directory also has a file called bulk_load_table.py that reads the items in the items.json file and bulk-writes them to the DynamoDB table. Below is the content of the file:
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('battle-royale')
items = []
with open('scripts/items.json', 'r') as f:
for row in f:
items.append(json.loads(row))
with table.batch_writer() as batch:
for item in items:
batch.put_item(Item=item)
In this script, rather than using the low-level client in Boto 3, you use a higher-level Resource object . Resource objects provide an easier interface for using the AWS APIs. The Resource object is useful in this situation because it batches the requests. The BatchWriteItem operation accepts as many as 25 items in a single request. The Resource object handles that batching for you rather than making you divide the data into requests of 25 or fewer items.
Run the bulk_load_table.py script and load your table with data by running the following command in the terminal:
python scripts/bulk_load_table.py
You can ensure that all your data was loaded into the table by running a Scan
operation and getting the count.
aws dynamodb scan --table-name battle-royale --select COUNT --return-consumed-capacity TOTAL
This should display the following results:
{
"Count": 835,
"ScannedCount": 835,
"ConsumedCapacity": {
"CapacityUnits": 14.5,
"TableName": "battle-royale",
"Table": {
"CapacityUnits": 14.5
}
}
}
You should see a Count
of 835, indicating that all of your items were loaded successfully.
You can also browse the table by navigating to Services -> Database -> DynamoDB in the AWS console.
In the next step, you see how to retrieve multiple entity types in a single request, which can reduce the total network requests you make in your application and enhance application performance.