Last active
May 29, 2018 23:57
-
-
Save reisjr/5ee3b820c933a986d3a422476ac523f5 to your computer and use it in GitHub Desktop.
DynamoDB - Collection of not "trivial" operations
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
| #Append time to a list using face_id as key. Create list if it doesn't exist. | |
| def log_event(face_id, similarity): | |
| now = datetime.datetime.now() | |
| create_dynamodb = dynamodb_events_table.update_item( | |
| Key={ | |
| 'FaceId': face_id, | |
| 'Timestamp': str(now.date()) | |
| }, | |
| UpdateExpression="SET #time_name = list_append(if_not_exists(#time_name, :empty_list), :t), Similarity = :s", | |
| ExpressionAttributeNames = { | |
| "#time_name": "Time", | |
| }, | |
| ExpressionAttributeValues={ | |
| ":t": [ str(now.time()) ], | |
| ":s": similarity, | |
| ":empty_list": [] | |
| }, | |
| ReturnValues="UPDATED_NEW" | |
| ) |
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
| DynamoDBLambdaFunctionRole: | |
| Type: AWS::IAM::Role | |
| Properties: | |
| AssumeRolePolicyDocument: | |
| Version: '2012-10-17' | |
| Statement: | |
| - Effect: Allow | |
| Principal: | |
| Service: lambda.amazonaws.com | |
| Action: | |
| - sts:AssumeRole | |
| Path: '/' | |
| ManagedPolicyArns: | |
| - arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole | |
| Policies: | |
| - PolicyName: LambdaCwlPolicy | |
| PolicyDocument: | |
| Statement: | |
| - Effect: Allow | |
| Action: | |
| - logs:CreateLogGroup | |
| - logs:CreateLogStream | |
| - logs:PutLogEvents | |
| Resource: arn:aws:logs:*:*:* | |
| - PolicyName: DynamoDBPolicy | |
| PolicyDocument: | |
| Statement: | |
| - Effect: Allow | |
| Action: | |
| - dynamodb:UpdateItem | |
| - dynamodb:Scan | |
| - dynamodb:Query | |
| - dynamodb:PutItem | |
| - dynamodb:GetItem | |
| - dynamodb:DeleteItem | |
| - dynamodb:BatchGetItem | |
| - dynamodb:BatchWriteItem | |
| Resource: | |
| - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${Table1} | |
| - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${Table2} | |
| - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${Table3} |
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
| # Not a beautiful code for the problem, but it handles 3 cases | |
| # No map created | |
| # Map created, but key doesn't exist | |
| # Map created, key exists | |
| # INPUT: size - the value to be added | |
| # MAP Structure keys (dates / ), values (sizes accumulated) | |
| # Used to count the amount of bytes consumed by day | |
| dynamodb = boto3.resource('dynamodb', config=Config(max_pool_connections=30)) | |
| dynamodb_counter_table = dynamodb.Table(DDB_AUDIENCE_COUNTERS_TABLE) | |
| day = datetime.datetime.today().strftime('%Y-%m-%d') | |
| try: | |
| dynamodb_counter_table.update_item( | |
| Key={ | |
| 'CounterName': COUNTER_BYTES | |
| }, | |
| UpdateExpression="SET #val.#age = #val.#day + :incr", | |
| ExpressionAttributeNames = { | |
| "#day": str(day), | |
| "#val": "Value" | |
| }, | |
| ExpressionAttributeValues={ | |
| ":incr": size, | |
| }, | |
| ReturnValues="UPDATED_NEW" | |
| ) | |
| except Exception: # Map created but key is not there | |
| print("WARN: update_bytes key not found. Trying to create key...") | |
| try: | |
| dynamodb_counter_table.update_item( | |
| Key={ | |
| 'CounterName': COUNTER_BYTES | |
| }, | |
| UpdateExpression="SET #val.#day = :incr", | |
| ExpressionAttributeNames = { | |
| "#age": str(day), | |
| "#val": "Value" | |
| }, | |
| ExpressionAttributeValues={ | |
| ":incr": size, | |
| }, | |
| ReturnValues="UPDATED_NEW" | |
| ) | |
| except Exception: # No map created | |
| print("WARN: update_bytes map not found. Creating map...") | |
| dynamodb_counter_table.update_item( | |
| Key={ | |
| 'CounterName': COUNTER_BYTES | |
| }, | |
| UpdateExpression="SET #val = :incr", | |
| ConditionExpression = "attribute_not_exists(#val)", | |
| ExpressionAttributeNames = { | |
| "#val": "Value" | |
| }, | |
| ExpressionAttributeValues={ | |
| ":incr": { str(day): size } | |
| } | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment