Amazon dynamodb DynamoDB查找日期后的所有项目
我有一个DynamoDB表,其中存储链接数据(Amazon dynamodb DynamoDB查找日期后的所有项目,amazon-dynamodb,Amazon Dynamodb,我有一个DynamoDB表,其中存储链接数据(url,date,category,标记等) 我需要能够- 按url查找项目(检查url不存在) 查找给定日期之后存储的所有项目 基于以上内容,我在date上以url作为主散列键和辅助索引设置了模式,如下所示- AWSTemplateFormatVersion: '2010-09-09' Parameters: TableName: Type: String Default: "my_links" HashAttr:
url
,date
,category
,标记
等)
我需要能够-
- 按
查找项目(检查url
不存在)url
- 查找给定日期之后存储的所有项目
date
上以url
作为主散列键和辅助索引设置了模式,如下所示-
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
TableName:
Type: String
Default: "my_links"
HashAttr:
Type: String
Default: url
IndexAttr:
Type: String
Default: date
ReadCapacity:
Type: Number
Default: 5
WriteCapacity:
Type: Number
Default: 5
Resources:
Table:
Properties:
KeySchema:
- AttributeName: !Ref HashAttr
KeyType: HASH
AttributeDefinitions:
- AttributeName: !Ref HashAttr
AttributeType: S
- AttributeName: !Ref IndexAttr
AttributeType: S
GlobalSecondaryIndexes:
- IndexName: !Ref IndexAttr
KeySchema:
- AttributeName: !Ref IndexAttr
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: !Ref ReadCapacity
WriteCapacityUnits: !Ref WriteCapacity
ProvisionedThroughput:
ReadCapacityUnits: !Ref ReadCapacity
WriteCapacityUnits: !Ref WriteCapacity
TableName: !Ref TableName
Type: AWS::DynamoDB::Table
我可以按date
查询表,如下所示,但只能使用eq
条件-
ddb=boto3.resource("dynamodb")
table=ddb.Table("my_links")
from boto3.dynamodb.conditions import Key
queryexp=Key('date').eq("2020-02-19")
for item in table.query(IndexName="date",
KeyConditionExpression=queryexp)["Items"]:
print (item)
filterexp=Key('date').gte("2020-02-19")
for item in table.scan(FilterExpression=filterexp)["Items"]:
print (item)
如果我使用gte
代替eq
条件,我会得到以下结果-
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query key condition not supported
但是,我可以使用扫描和gte
条件查询表-
ddb=boto3.resource("dynamodb")
table=ddb.Table("my_links")
from boto3.dynamodb.conditions import Key
queryexp=Key('date').eq("2020-02-19")
for item in table.query(IndexName="date",
KeyConditionExpression=queryexp)["Items"]:
print (item)
filterexp=Key('date').gte("2020-02-19")
for item in table.scan(FilterExpression=filterexp)["Items"]:
print (item)
但我猜我不再需要二级索引,而且随着表变大,这将变得非常昂贵:-/
因此,如果可能的话,我宁愿使用二级索引和查询(我想的对吗?),但是我需要对模式做些什么才能在一个日期之后获取所有项目?您不能使用GTE。
查询支持EQ | LE | LT | GE | GT |以| before |开头
查看这篇文章,这篇文章很有用-
此外—
最后,答案是对数据进行非标准化,将日期
字段(以及相关的二级索引)替换为周
字段,然后使用eq
查询条件搜索一个或多个特定周,并加入结果(我只需要几周的数据)
显然,可以将
周
替换为月
,以增加范围但降低粒度。queryexp=DDBKey('date')。介于(低值=args[“startdate”],高值=args[“enddate”])
仍然失败,查询键条件不受支持我错过了这个:创建全局二级索引而不是二级索引我不是已经创建了全局二级索引吗?索引定义在CF yaml中的globalSecondaryIndex
下指定