Reputation: 23
In AWS DynamoDB you must specify a `partition key, which make it just work like GetItem ... because partition key is unique so it is supposed to return only one item, so if I know the ID of that item, it make no sense anymore to query! because query meant to be for constrains ...
so can someone give me example where querying one partition key can return multiple items ?
# 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"}}'
You also can only use the EQ
Operator for partition key
, so using for example BETWEEN
or OR
or IN
is not allowed on partition key
alternative to query
there is scan
but
so I realized I can use sort key, and then in this case partition key
can be my table name, so I need to change my vocabularies
Example : table my-api with partition key -> className
and sort key -> id
my-api
className | id | username | title
_User | 0 | "bingo" |
_User | 1 | "mimi" |
_Song | 0 | | "You with me"
it is weird design
Upvotes: 0
Views: 945
Reputation: 106
Partition key would always return unique result if there is no sort key defined. Configuring sort key to your table means that the combination between partition key and sort key must be unique. For best practise defining the structure of your table please refer to this
Upvotes: 0
Reputation: 1813
Querying is for finding Items based on Primary or Index Key attributes. Primary Key can have one Partition Key (hash key) and one optional Range Key. If Range key present, then you can have multiple records with same Partition Key. So to perform GetItem
operation on this kind of composite primary key you need to specify Range Key with Hash key.
Also, You can specify multiple Global Secondary Indexes (GSI) and Local Secondary Indexes (LSI), through which you can query non-key attributes.
So, Query operation provides you means to find items based on either Primary key, LSI or GSI's hash key and Range key's attributes.
Now, the example you used, is certainly not a good approach to design your schema.
Table != Database
Table == Table and DynamoDB houses all the tables.
User
, Song
etc needs to be stored in different tables. Then you can specify id
, username
to further uniquely identify items of your table. Each item can be thought of as record in RDBMS. Read more about choosing primary key and indexes and all information from AWS DynamoDB Developer Guide
Upvotes: 0
Reputation: 39166
If the table has both partition key and sort key and query by partition key alone will give you multiple items.
Get API:-
For a composite primary key, you must provide values for both the partition key and the sort key.
So, Get API will always return only one item. Also, there is no filter expression to filter by non-key attributes.
Upvotes: 3