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.
The Mikado Method by Ola Ellnestam Daniel Brolund(20726)
Hello! Python by Anthony Briggs(20012)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(18332)
Dependency Injection in .NET by Mark Seemann(18212)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(17689)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(17497)
Kotlin in Action by Dmitry Jemerov(17308)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(16937)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(16319)
Grails in Action by Glen Smith Peter Ledbrook(15467)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(13331)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(11436)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10582)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(10417)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(9443)
Hit Refresh by Satya Nadella(9087)
The Kubernetes Operator Framework Book by Michael Dame(8523)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8348)
Robo-Advisor with Python by Aki Ranin(8295)