在AWS DynamoDB中,您必須指定一個`分區鍵,它可以像GetItem一樣工作......因爲分區鍵是唯一的,所以它應該只返回一個項目,所以如果我知道該項目的ID,它再也沒有意義了!因爲查詢的目的是爲了約束...AWS中的查詢有什麼意義DynamoDB
所以有人可以給我例子查詢一個分區鍵可以返回多個項目?
# Create single-attribute primary key table
aws dynamodb create-table --table-name testdb6 --attribute-definitions '[{"AttributeName": "Id", "AttributeType": "S"}]' --key-schema '[{"AttributeName": "Id", "KeyType": "HASH"}]' --provisioned-throughput '{"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}'
# Populate table
aws dynamodb put-item --table-name testdb6 --item '{ "Id": {"S": "1"}, "LastName": {"S": "Lopez"}, "FirstName": {"S": "Maria"}}'
aws dynamodb put-item --table-name testdb6 --item '{ "Id": {"S": "2"}, "LastName": {"S": "Fernandez"}, "FirstName": {"S": "Augusto"}}'
# Query table using only partition attribute
aws dynamodb query --table-name testdb6 --select ALL_ATTRIBUTES --key-conditions '{"Id": {"AttributeValueList": [{"S": "1"}], "ComparisonOperator": "EQ"}}'
您也可以只使用EQ
運營商爲partition key
,所以例如使用BETWEEN
或OR
或IN
是不允許的partition key
替代query
有scan
但
- 掃描是昂貴的(慢)
- 你無法在掃描上排序
更新
所以我意識到,我可以使用排序鍵,然後在這種情況下partition key
可以成爲我的表名,所以我需要改變我的詞彙
- 表 - >數據庫
- 分區鍵 - >表/收藏
- 排序關鍵字 - >主鍵/的ObjectId
例子:表我-API與partition key -> className
和sort key -> id
my-api
className | id | username | title
_User | 0 | "bingo" |
_User | 1 | "mimi" |
_Song | 0 | | "You with me"
很是怪異設計
查詢使用排序鍵 –