amazon-dynamodb - DynamoDB 查找日期之后的所有项目
问题描述
我有一个 DynamoDB 表,其中存储了链接数据(url
、date
、category
等tags
)。
我需要能够——
- 查找项目
url
(检查url
不存在) - 查找在给定之后存储的所有项目
date
基于上述,我将架构设置url
为主哈希键和二级索引date
,如下所示 -
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)
如果我使用 agte
代替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)
但是我猜我不再需要二级索引,而且随着表变大,这将变得非常昂贵:-/
因此,如果可能的话,我宁愿坚持使用二级索引和查询(我在想这个吗?),但是我需要对架构做些什么才能在日期之后获取所有项目?
解决方案
您不能使用 GTE。查询支持 EQ | 乐 | LT | 通用电气 | 燃气轮机 | BEGINS_WITH | 之间
检查这篇文章https://medium.com/cloud-native-the-gathering/querying-dynamodb-by-date-range-899b751a6ef2
推荐阅读
- cocoa - 如何判断是否从 NSComboBox 中的 comboBoxWillDismiss 中选择了项目或取消了菜单?
- python - 哈希无法验证烧瓶管理员
- ios - SwiftUI ButtonStyle 在 iOS 14.4 上忽略 isEnabled 环境变量,适用于 iOS 14.5+
- java - 为什么 JFR 会跳过事件字段
- jquery - 无法控制 jQuery 动画持续时间
- typo3 - TYPO3 9.5:没有名为 TYPO3\CMS\Saltedpasswords\Utility\ExtensionManagerConfigurationUtility 的类
- angular - Java Spring 没有解析 Post 参数
- javascript - 在 HTTP GET 请求中使用 If 语句时函数内变量的值不会改变
- xml - 使用 Fetch API 包含使用 XSLT 转换的 XML 文件
- python - 如何让 python 在另一个应用程序中等待“等待”提示?