Pra*_*mar 4 amazon-web-services amazon-dynamodb
我有2个属性id(字符串,主键),值(字符串).当我尝试使用KeyConditionExpression时,它会抛出不支持的查询键条件.
KeyConditionExpression: "begins_with(ID, :tagIDValue)"
or
KeyConditionExpression: "contains(ID, :tagIDValue)"
Run Code Online (Sandbox Code Playgroud)
从这个链接我知道我们只能在主键上使用EQ操作.我怎样才能做到这一点
解决方案:================================================ ======
我需要使用starts_with或contains来过滤所以我采用了以下方法.
表属性:PK(partion_key,string),ID(排序键,字符串),值(字符串).
现在我的主键是基于PK,ID构建的
PK将具有所有行的常量值.所以KeyConditionExpression就像.
KeyConditionExpression: "PL = :pk and begins_with(ID, :tagIDValue)"
Run Code Online (Sandbox Code Playgroud)
注意:但仍然包含不使用KeyConditionExpression.我认为它已从KeyConditionExpression中删除
小智 11
您可以使用begins_with
,并contains
只与指定的主键EQ条件之后的一系列关键.
要使用主键,您可以使用EQ
KeyConditionExpression: "ID = :tagIDValue"
Run Code Online (Sandbox Code Playgroud)
我不同意 Tolbahady 关于begins_with
并且contains
仅适用于范围键的声明。您可以使用 method 在任何键中使用任何比较运算符scan
。扫描是一种进行 dynamodb 查询的灵活但昂贵且低效的方式。
无论如何,有一个名为AWS NoSQL Workbench的工具。它有点像 MySQL Workbench。它的好处是,您可以在检查访问模式的同时构建您的表(您的应用程序针对您的表的可能和最常用的查询)。
小智 6
begins_with
您不能在哈希(主)键上使用条件表达式。它仅适用于scan
method 和 Range(sort) 键。
您可以做的另一件事是创建一个GlobalSecondaryIndexes
ID 作为哈希键。
例如:
IntentTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: timestamp
KeyType: RANGE
AttributeDefinitions:
- AttributeName: client
AttributeType: S
- AttributeName: timestamp
GlobalSecondaryIndexes:
- IndexName: byID
KeySchema:
- AttributeName: ID
KeyType: HASH
- AttributeName: timestamp
KeyType: RANGE
Projection:
ProjectionType: ALL
Run Code Online (Sandbox Code Playgroud)
然后现在您可以使用query
该索引而不是scan.
不惜一切代价避免扫描,否则您会对每月的账单感到震惊。
归档时间: |
|
查看次数: |
7411 次 |
最近记录: |