Dynamo DB Api
Dynamo DB Api
pm jat @ daiict
Dynamo DB – Data Model
• Dynamo DB Table
• Meta information of a table
– Primary Key and “Secondary Indexes”
• Structure and meaning of “Primary Key” in Dynamo
• Primary Key of the dynamo table and “Key” in Key-Value stores
• The Primary Key is used for
– Uniquely identifying an item in a table
– data partitioning
– data ordering within a partition
– Performing search operation
• Define and alter a table “structure”
Example Table
Note following
• Table named “People”
• Primary Key here is “Person ID”
• Other than the Primary Key the Table is “schema less”
– This means PK is the only thing needs to be defined
as schema of the table, and nothing else!
Example Table
Note following
• Data objects (Items) are divided in two parts
– Key, and
– Value
• In this case Person ID is the key whereas rest of the
attribute values are data.
• Attributes in value part can be nested – for example
Address attribute has sub-attributes
Sort-Key
for ordering data within a
partition
• When specified both Primary
Key said to be “Composite”
Example Table
• Here is a Table with Composite Primary Key
• Table Name: Music
• Primary Key:
Partition Key: Artist
Clustering Key: Song Title
• Note a operation enhancement here in Dynamo DB
• We can perform GET based on part of Primary Key
– By specifying only “Artists Name”
– May also specify some filter on “Song Title”
Secondary Indexes
• “Index keys” are additional “search keys”
• “Index values” are keys of data records “Items”
• Indexes belong to a table
• DynamoDB maintains indexes automatically –
as we add, update, or delete data
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.API.html
item.json
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.API.html
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.API.html
27-Oct-23 Dynamo DB – Data Model and API 30
“update‐item” example
Parameters: (1) Key, (2) Update expressions, (3) new values
OPTIONAL
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.API.html
27-Oct-23 Dynamo DB – Data Model and API 31
“update‐item” example
OPTIONAL
• Specify Key and Values from a JSON file
key.json
expression‐attribute‐values.json
Specify projection
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html
27-Oct-23 Dynamo DB – Data Model and API 34
“batch-get‐item” example
OPTIONAL
//table‐name
request‐items.json
//key values
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html
27-Oct-23 Dynamo DB – Data Model and API 35
“query” example
• Query (also) allows querying based on Key only
• We specify “Key-Expression”
• In the example below:
– Table: Thread. Partition Key: ForumName
– Returns all of the items for given value for given partition key.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
27-Oct-23 Dynamo DB – Data Model and API 36
“query” example
OPTIONAL
• Table: Thread. Partition Key: Forum Name
• Returns all of the items with given value for given partition key value
values.json
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
27-Oct-23 Dynamo DB – Data Model and API 37
Process “query” Results
• After query has been retrieved based on Partition Key (and Sort Key), results of that
query can be further processed
– Filtering
– Counting
– Limiting the items in results set (LIMIT parameter)
– Paginating the results
• Note that a query operation can retrieve a maximum of
1 MB of data (before the filter)
Partition-key Sort-key
values.json
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
27-Oct-23 Dynamo DB – Data Model and API 39
Limiting the Number of Items in the Result Set
• The Query operation allows us limit the number of items that it reads.
• To do this, set the Limit parameter to the maximum number of items that you want.
• Note that the filter is applied after limit.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
27-Oct-23 Dynamo DB – Data Model and API 42
Scan Examples
OPTIONAL
• By Default Sequential
• Scan operation allows specifying parameter “TotalSegments” indicating a parallel
number of segments to be executed
key.json
GameTitle Index
https://awscli.amazonaws.com/v2/documentation/api/latest/reference/dynamodb/create-table.html#examples
27-Oct-23 Dynamo DB – Data Model and API 67
Add GSI through UPDATE TABLE OPTIONAL
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
27-Oct-23 Dynamo DB – Data Model and API 70
Example: Query through GSI OPTIONAL
• Returns User, TopScore for game “Meteor Blasters”. Searching is
done using GSI “GameTitleInex”!
aws dynamodb query \
‐‐table‐name GameScores \
‐‐index‐name GameTitleIndex \
‐‐key‐condition‐expression "GameTitle=:v_title" \
‐‐expression‐attribute‐values '{
":v_title": {"S": "Meteor Blasters"}
}' \
‐‐projection‐expression "UserId, TopScore"
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
27-Oct-23 Dynamo DB – Data Model and API 71
Tuning Consistency in Dynamo DB
• Recall Dynamo DB prefers Availability over consistency
• Dynamo API does not allow users to control R and W but allows specifying the
desired consistency level while doing READ operations.
• Provides the following two options
– “eventually consistent read”; which is basically weaker consistency’
– “strongly consistent reads”
• Dynamo DB uses eventually consistent reads unless we specify otherwise.
• Read operations (such as GetItem, Query, and Scan) provide a
ConsistentRead parameter. If you set this parameter to true,
DynamoDB uses strongly consistent reads during the operation.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html
27-Oct-23 Dynamo DB – Data Model and API 72
Consistency in Dynamo DB
• Eventually Consistent Reads:
– When we read data from a Dynamo DB table, the response might not reflect the
results of a recently completed write operation.
– The response might include some stale data.
– If you repeat your read request after a short time, the response should return
the latest data.
• Strongly Consistent Reads:
– When you request a strongly consistent read, Dynamo DB returns a response
with the most up-to-date data, reflecting the updates from all prior write
operations that were successful.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html