Batch-loading data into DynamoDB

We will first discuss how to batch-load data into DynamoDB from a comma-separated values (CSV) file called sample_data/dynamodb-sample-data.txt. Rather than insert an individual statement for each item, this is a much more efficient process, as the data file is decoupled from the Python code:

EventId,EventDay,EventCount
324,20171010,2
324,20171012,10
324,20171013,10
324,20171014,6
324,20171016,6
324,20171017,2
300,20171011,1
300,20171013,3
300,20171014,30

Add another method, called update_dynamo_event_counter(), that updates DynamoDB records using the DynamoRepository class.

Here are the contents of the serverless-microservice-data-api/aws_dynamo/dynamo_insert_items_from_file.py Python script:

from boto3 import resource

class DynamoRepository:
def __init__(self, target_dynamo_table, region='eu-west-1'):
self.dynamodb = resource(service_name='dynamodb', region_name=region)
self.target_dynamo_table = target_dynamo_table
self.table = self.dynamodb.Table(self.target_dynamo_table)

def update_dynamo_event_counter(self, event_name,
event_datetime, event_count=1):
response = self.table.update_item(
Key={
'EventId': str(event_name),
'EventDay': int(event_datetime)
},
ExpressionAttributeValues={":eventCount":
int(event_count)},
UpdateExpression="ADD EventCount :eventCount")
return response

Here, we have a DynamoRepository class that instantiates a connection to DynamoDB in __init__() and an update_dynamo_event_counter() method that updates the DynamoDB records if they exist, or adds a new one if they don't using the passed-in parameters. This is done in one atomic action.

Here's the second half of the serverless-microservice-data-api/aws_dynamo/dynamo_insert_items_from_file.py Python script:

 import csv
table_name = 'user-visits-sam'
input_data_path = '../sample_data/dynamodb-sample-data.txt'
dynamo_repo = DynamoRepository(table_name)
with open(input_data_path, 'r') as sample_file:
csv_reader = csv.DictReader(sample_file)
for row in csv_reader:
response = dynamo_repo.update_dynamo_event_counter(row['EventId'], row['EventDay'], row['EventCount'])
print(response)

This Python code opens the CSV, extracts the header row, and parses each row while writing it to the DynamoDB table called user-visits-sam.

Now that we have loaded some data rows into the DynamoDB table, we will query the table by debugging a local Lambda function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset