Last active
March 20, 2024 22:44
-
-
Save pictolearn/3a13312e7525cc766be42fb43d3d9a94 to your computer and use it in GitHub Desktop.
increment of atomic counter in DynamoDB using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# increment of an atomic counter | |
# boto3, an AWS SDK package | |
# JSON, a text format package that is language independent | |
# decimal, a precision Handling package | |
import boto3 | |
import json | |
import decimal | |
# Helper class to convert a DynamoDB item to JSON. | |
class DecimalEncoder(json.JSONEncoder): | |
def default(self, o): | |
if isinstance(o, decimal.Decimal): | |
if o % 1 > 0: | |
return float(o) | |
else: | |
return int(o) | |
return super(DecimalEncoder, self).default(o) | |
# resource request service and region are set | |
dynamodb = boto3.resource('dynamodb', region_name='us-east-1') | |
table = dynamodb.Table('<table name>') | |
title = "The Big New Movie" | |
year = 2015 | |
# updation of items into the table | |
response = table.update_item( | |
Key={ | |
'year': year, | |
'title': title | |
}, | |
UpdateExpression="set info.rating = info.rating + :val", | |
ExpressionAttributeValues={ | |
':val': decimal.Decimal(1) | |
}, | |
ReturnValues="UPDATED_NEW" | |
) | |
print("UpdateItem succeeded:") | |
print(json.dumps(response, indent=4, cls=DecimalEncoder)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this atomic?