【发布时间】:2020-02-19 10:29:22
【问题描述】:
我有一个存储链接数据的 DynamoDB 表(url、date、category、tags 等)。
我需要能够 -
- 通过
url查找项目(检查url不存在) - 查找在给定
date之后存储的所有项目
基于以上,我在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)
如果我使用 gte 代替 eq 条件,我会得到以下 -
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query key condition not supported
但是,我可以使用扫描和gte 条件查询表 -
filterexp=Key('date').gte("2020-02-19")
for item in table.scan(FilterExpression=filterexp)["Items"]:
print (item)
但是我猜我不再需要二级索引,而且随着表变大,这将变得非常昂贵:-/
所以如果可能的话,我宁愿坚持使用二级索引和查询(我在想这个吗?),但是我需要对架构做些什么才能在 之后获取所有项目 em> 约会?
【问题讨论】:
标签: amazon-dynamodb