Building Serverless Microservices in Python by Takashi Freeman Richard
Author:Takashi Freeman Richard
Language: eng
Format: epub, pdf
Tags: COM000000 - COMPUTERS / General, COM082000 - COMPUTERS / Bioinformatics, COM006000 - COMPUTERS / Buyer
Publisher: Packt Publishing
Published: 2019-03-29T12:02:38+00:00
Writing to DynamoDB using Python
The following code writes three records to DynamoDB. Create another file called dynamo_modify_items.py with the following Python code:
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): return self.table.update_item( Key={ 'EventId': event_name, 'EventDay': event_datetime }, ExpressionAttributeValues={":eventCount": event_count}, UpdateExpression="ADD EventCount :eventCount") def main(): table_name = 'user-visits' dynamo_repo = DynamoRepository(table_name) print(dynamo_repo.update_dynamo_event_counter('324', 20171001)) print(dynamo_repo.update_dynamo_event_counter('324', 20171001, 2)) print(dynamo_repo.update_dynamo_event_counter('324', 20171002, 5)) if __name__ == '__main__': main()
Here, we use Boto3's resource(), which is a higher-level service resource with the repository pattern. We abstract all the DynamoDB-related code in the DynamoRepository() class that instantiates as dynamo_repo with table_name. self.dynamodb.Table() creates a table resource based on table_name. That will be used later on when calling update_dynamo_event_counter() to update DynamoDB records.
In self.table.update_item(), I first declare a variable called eventCount using ExpressionAttributeValues. I'm using this in the DynamoDB advanced Update Expressions (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html), which is one of my favorite features in DynamoDB. Why? Because not all NoSQL databases can do something similar without having something like a semaphore lock and having the clients do a retry. It performs the following three actions in one atomic statement, while circumventing possible concurrency violations at the cost of eventual consistency:
Reads records matching the given EventId=event_name and EventDay=event_datetime
Creates a new item if it doesn't exist, setting EventCount=1
If it does already exist, then it increments EventCount by event_count
Download
Building Serverless Microservices in Python by Takashi Freeman Richard.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12583)
Hello! Python by Anthony Briggs(9919)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9798)
The Mikado Method by Ola Ellnestam Daniel Brolund(9781)
Dependency Injection in .NET by Mark Seemann(9342)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9297)
Hit Refresh by Satya Nadella(8825)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8304)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7785)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7767)
Grails in Action by Glen Smith Peter Ledbrook(7699)
The Kubernetes Operator Framework Book by Michael Dame(7663)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7561)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7451)
Practical Computer Architecture with Python and ARM by Alan Clements(7377)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7359)
Robo-Advisor with Python by Aki Ranin(7332)
Building Low Latency Applications with C++ by Sourav Ghosh(7240)
Svelte with Test-Driven Development by Daniel Irvine(7205)
