英文:
Retrieve latest record added to AWS Dynamodb table and display on flask webpage
问题
我正在开发一个Flask网站,需要从AWS DynamoDB表中检索最新添加的记录并在网页上显示检索到的数据。
我的DynamoDB表的主键是时间戳(timestamp)。但是,为了查询记录,我仅使用当前日期。'currentdate'列不是主键。
以下是我实施的查询:
timestamp1 = datetime.datetime.now()
timestamp = timestamp1.strftime("%m/%d/%Y")
table = DynamoDB.Table('smart Irrigation')
response = table.Scan(
FilterExpression=Attr('currentdate').eq(timestamp),
Limit=1
)
然而,在执行上述查询时,我收到以下错误消息:
ParamValidationError botocore.exceptions.ParamValidationError: 参数验证失败:输入中的未知参数:"ScanIndexForward",必须是以下之一:TableName、IndexName、AttributesToGet、Limit、Select、ScanFilter、ConditionalOperator、ExclusiveStartKey、ReturnConsumedCapacity、TotalSegments、Segment、ProjectionExpression、FilterExpression、ExpressionAttributeNames、ExpressionAttributeValues、ConsistentRead
英文:
I am developing a flask website where the I need to retrieve the latest record added to AWS dynamodb table and display the retrieved data on a webpage.
The primary key in my DynamoDB table is timestamp. However to query the record I am using the current date only. 'currentdate' column is not primary key.
The following is the query I have implemented:
timestamp1 = datetime.datetime.now()
timestamp = timestamp1.strftime("%m/%d/%Y")
table = DynamoDB. Table('smart Irrigation')
response = table. Scan(
FilterExpression=Attr('currentdate').eq(timestamp),
ScanIndexForward=False, Limit=1
)
However when executing the above query, I am getting the following error message :
> ParamValidationError botocore.exceptions.ParamValidationError:
> Parameter validation failed: Unknown parameter in input:
> "ScanIndexForward", must be one of: TableName, IndexName,
> AttributesToGet, Limit, Select, ScanFilter, ConditionalOperator,
> ExclusiveStartKey, ReturnConsumedCapacity, TotalSegments, Segment,
> ProjectionExpression, FilterExpression, ExpressionAttributeNames,
> ExpressionAttributeValues, ConsistentRead
答案1
得分: 0
尽管其名称为ScanIndexForward
,但不适用于Scan
API。
数据建模方式使得按所需方式读取数据非常困难。您应考虑重新进行数据建模。
根据吞吐量需求,我建议使用具有静态值作为分区键和currentdate
作为排序键的GSI。这将允许您对索引进行ScanIndexForward=False
和Limit=1
的Query
,从而返回最新的项目。
英文:
Despite its name ScanIndexForward
is not applicable for Scan
API.
They way your data is modeled leaves it very difficult to read your data in the desired way. You should consider remodelling your data.
Depending on your throughput needs I would use a GSI with a static value as partition key and currentdate
as the sort key. That would allow you do so a Query
on the index with ScanIndexForward=False
and Limit=1
which would return you the latest item.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论