无法通过 boto3 查询 DynamoDB 中的分区键

Unable to query on Partition key in DyanmoDB by boto3

我有一个 table TestTable 和分区键 TestColumn

输入日期:

from_date= "2017-04-20T16:31:54.451071+00:00"
to_date = "2018-04-20T16:31:54.451071+00:00"

当我使用相等查询日期时,它就可以工作了。

key_expr = Key('TestColumn').eq(to_date)
query_resp = table.query(KeyConditionExpression=key_expr)

但是当我使用 between 查询时,则无法正常工作。

key_expr = Key('TestColumn').between(from_date, to_date)
query_resp = table.query(KeyConditionExpression=key_expr)

错误: Unknown err_msg while querying dynamodb: An error occurred (ValidationException) when calling the Query operation: Query key condition not supported

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

DynamoDB 查询将 return 来自一个分区的数据,这意味着您必须在请求中提供单个分区键。

KeyConditionExpression

The condition that specifies the key value(s) for items to be retrieved by the Query action.

The condition must perform an equality test on a single partition key value.

您可以选择对排序键使用 BETWEEN 运算符(但您仍然必须提供单个分区键)。

如果您使用 Scan,则可以使用 ExpressionFilter 并在 TestColumn

上使用 BETWEEN 运算符