查询小于条件的 DynamoDB ISO-8601 日期索引
Query DynamoDB ISO-8601 Date index with less than condition
我有一个 DynamoDB 索引以 iso-8601 格式存储日期,[即'2017-03-17']。我想在索引中查询小于特定日期的日期。 [即 datetime.now() - 30 天]。
我相信这是来自亚马逊 API 文档的支持。我似乎遗漏了 boto3 语法的某些内容。
我正在使用资源而不是 boto3 中的客户端。
我知道我可以使用 epoch,但为了可读性我想使用 ISO 格式。
这是我的测试代码。
import boto3
from boto3.dynamodb.conditions import Key, Attr
ddb = boto3.resource('dynamodb')
_table = ddb.Table('TableWithDateIndex')
response2 = _table.query(
IndexName='DeleteDate-index',
KeyConditionExpression=Key('IndexDate').lt('2017-04-17')
)
print response2['Items']
失败:
botocore.exceptions.ClientError:调用Query操作时发生错误(ValidationException):不支持查询键条件
因此 IndexDate 是 'DeleteDate-index' 索引的哈希键。使用Query时,必须为hash key提供相等条件,即.eq()
。换句话说,您不能在 Query 中将 lt
、lte
、gt
、gte
等比较运算符与散列键一起使用。在 KeyConditionExpression 中,这些运算符只能应用于范围键。
以下是 documentation 的摘录:
In a Query operation, you use the KeyConditionExpression parameter to
determine the items to be read from the table or index. You must
specify the partition key name and value as an equality condition. You
can optionally provide a second condition for the sort key (if
present).
您可以使用扫描操作代替查询。当 运行 Scan 时,您可以在 FilterExpression 中的任何字段上使用任何运算符(或者根本不使用任何条件)。尽管您应该谨慎使用 Scan,因为它可能是一个非常重量级的操作:它意味着读取 table(或二级索引)中的每个项目。
我有一个 DynamoDB 索引以 iso-8601 格式存储日期,[即'2017-03-17']。我想在索引中查询小于特定日期的日期。 [即 datetime.now() - 30 天]。
我相信这是来自亚马逊 API 文档的支持。我似乎遗漏了 boto3 语法的某些内容。
我正在使用资源而不是 boto3 中的客户端。
我知道我可以使用 epoch,但为了可读性我想使用 ISO 格式。
这是我的测试代码。
import boto3
from boto3.dynamodb.conditions import Key, Attr
ddb = boto3.resource('dynamodb')
_table = ddb.Table('TableWithDateIndex')
response2 = _table.query(
IndexName='DeleteDate-index',
KeyConditionExpression=Key('IndexDate').lt('2017-04-17')
)
print response2['Items']
失败:
botocore.exceptions.ClientError:调用Query操作时发生错误(ValidationException):不支持查询键条件
因此 IndexDate 是 'DeleteDate-index' 索引的哈希键。使用Query时,必须为hash key提供相等条件,即.eq()
。换句话说,您不能在 Query 中将 lt
、lte
、gt
、gte
等比较运算符与散列键一起使用。在 KeyConditionExpression 中,这些运算符只能应用于范围键。
以下是 documentation 的摘录:
In a Query operation, you use the KeyConditionExpression parameter to determine the items to be read from the table or index. You must specify the partition key name and value as an equality condition. You can optionally provide a second condition for the sort key (if present).
您可以使用扫描操作代替查询。当 运行 Scan 时,您可以在 FilterExpression 中的任何字段上使用任何运算符(或者根本不使用任何条件)。尽管您应该谨慎使用 Scan,因为它可能是一个非常重量级的操作:它意味着读取 table(或二级索引)中的每个项目。