I am coming from a relational database background and currently trying to familiarise with NoSQL methodology (DynamoDB particularly). I am using Python boto3 SDK as my development environment.
I have a table called "availabilities" that has two fields called "fromTime" and "toTime". They hold time data in 24-hours format such as "16:00", or "08:30". I need to write a function that fetches all the records that the current time falls in the range of from and to times. For example current time is 16:00. The function should fetch all the rows that 16:00 falls into the range of from an to times such as 08:00 - 18:00. I am not sure if I should go with scan or query but here is how I tried it in a Lambda function:
import json
import logging
import boto3
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key, Attr
from datetime import datetime
def scan(self, table_name, region, filter_expression, projection_expression):
dynamodb_resource = boto3.resource("dynamodb", region_name=region)
table = dynamodb_resource.Table(table_name)
response = table.scan(
FilterExpression=filter_expression,
ProjectionExpression=projection_expression
)
return response
def lambda_handler(event, context):
now = datetime.now()
current_time = now.strftime("%H:%M")
fe = current_time.between(Key('fromTime'), Key('toTime'))
pe = 'user_id'
availableUserIDs = scan('availabilities', 'us-east-1', fe, pe)['Items']
return {
'statusCode': 200,
'body': json.dumps(availableUserIDs)
}
I'm getting an error as follows:
"errorMessage": "'str' object has no attribute 'between'"
I am thinking about converting everything into minutes since midnight by multiplying hours by 60 and adding minutes on to it, and then comparing like that. However, I'm not sure how I can access the fromTime and toTime fields before actually running the scan function.