2개 답변
- 최신
- 최다 투표
- 가장 많은 댓글
2
Your issue is that you use timestamp as a partition key, which will not allow you to retrieve the latest item without knowing the exact time it was written, it's not suggested to ever use a timestamp for a partition key. Below I suggest an alternative data model to suit your needs:
pk | timeStamp | temperature | payload |
---|---|---|---|
1 | 1707211111 | 0 | {some data} |
1 | 1707216222 | 0 | {some data} |
1 | 1707217333 | 0 | {some data} |
1 | 1707214485 | 0 | {some data} |
And your Lambda function will look like this:
import boto3 session = boto3.session.Session() dynamodb = session.resource('dynamodb') table = dynamodb.Table('HomeDB') def lambda_handler(event, context): response = table.query( KeyConditionExpression="#pk= :pk", ScanIndexForward=False, Limit=1, ExpressionAttributeValues={ ":pk": 1 }, ExpressionAttributeNames={ "#pk":"pk" } ) # Other business logic here
More details in this blogpost: https://aws.amazon.com/blogs/database/effective-data-sorting-with-amazon-dynamodb/
0
Hi,
This article is probably interesting for what you want to achieve: https://blog.jankosmala.eu/2018/10/20/aws-dynamodb-get-the-latest-item/
Best,
Didier
관련 콘텐츠
- AWS 공식업데이트됨 일 년 전
Good advice from Lee, as always. Thinking outside the box a bit... depending on your use case, there might be an alternate approach you could explore, using DynamoDB Streams.