I am currently attempting to analyze data from my DynamoDB database, with a focus on querying for both current and past dates. However, I have encountered some issues while using the AWS web app. Specifically, when I set the date as the primary key, the data is visible, but when attempting to scan it, the data does not appear.
In addition, I have also tried using the Boto3 API, but the output has shown an incorrect last evaluated key of 20230318, with a scanned count of 12279 that is not accurate. Below is an portion of the code to scan the table:
import boto3
import csv
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Attr
# Initialize the DynamoDB client
dynamodb = boto3.resource('dynamodb', region_name='us-east-2')
# Replace 'your_table_name' with the name of your DynamoDB table
table = dynamodb.Table('table_name')
# Get the current day timestamp
current_day = "20230405"
previous_day = "20230404" # datetime.now(timezone.utc).date().strftime("%Y%m%d")
#today = datetime.now()
#current_day = today.strftime('%Y%m%d')
#previous_day = (today - timedelta(days=1)).strftime('%Y%m%d')
# Scan the table
response_current_day = table.scan(
ProjectionExpression='ip_address, pk, c2_name, #counter',
ExpressionAttributeNames={
'#counter': 'counter',
},
FilterExpression=Attr('pk').eq(current_day)
)
Output of the request:
{'Items': [],
'Count': 0,
'ScannedCount': 12279,
'LastEvaluatedKey': {'pk': '20230318', 'sk': 'redacted'},
'ResponseMetadata': {'RequestId': 'redacted',
'HTTPStatusCode': 200,
'HTTPHeaders': {'server': 'Server',
'date': 'Wed, 05 Apr 2023 21:14:11 GMT',
'content-type': 'application/x-amz-json-1.0',
'content-length': '126',
'connection': 'keep-alive',
'x-amzn-requestid': 'REDACTED',
'x-amz-crc32': '3416724477'},
'RetryAttempts': 0}}
This worked as intended. I didn't know about the difference between the scan vs query. Thank you for the help. If anyone else in the future is looking at this you would also need to add an import of "Key".
from boto3.dynamodb.conditions import Key