DynamoDB API – Part Two
Hello Everyone
Welcome to CloudAffaire and this is Debjeet
In the last blog post, we have discussed some of the DynamoDB API’s available.
https://cloudaffaire.com/dynamodb-api-part-one/
In this blog post, we are going to continue our discussion on basic API’s available in DynamicDB.
DynamoDB API
UpdateItem:
Edits an existing item’s attributes, or adds a new item to the table if it does not already exist. You can put, delete, or add attribute values. You can also perform a conditional update on an existing item (insert a new attribute name-value pair if it doesn’t exist, or replace an existing name-value pair if it has certain expected attribute values).
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
update-item --table-name --key [--attribute-updates [--expected [--conditional-operator [--return-values [--return-consumed-capacity [--return-item-collection-metrics [--update-expression [--condition-expression [--expression-attribute-names [--expression-attribute-values [--cli-input-json [--generate-cli-skeleton |
Example: We will add a name (Bob) and salary (1200) where id = 100 in Employee table
1 2 3 4 5 6 7 8 |
aws dynamodb update-item ^ --table-name Employee ^ --key file://key1.json ^ --update-expression "SET #A = :y, #B = :t" ^ --expression-attribute-names file://key2.json ^ --expression-attribute-values file://key3.json ^ --return-values ALL_NEW ^ --endpoint-url http://localhost:8000 |
key1.json
1 2 3 |
{ "id": {"N": "100"} } |
key2.json
1 2 3 |
{ "#A":"name", "#B":"salary" } |
key3.json
1 2 3 4 |
{ ":y":{"S": "Bob"}, ":t":{"N": "1200"} } |
GetItem:
The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data and there will be no Item element in the response.
Syntax:
1 2 3 4 5 6 7 8 9 10 |
get-item --table-name --key [--attributes-to-get [--consistent-read | --no-consistent-read] [--return-consumed-capacity [--projection-expression [--expression-attribute-names [--cli-input-json [--generate-cli-skeleton |
Example: We will fetch all details where id = 100 from Employee table
1 2 3 4 |
aws dynamodb get-item ^ --table-name Employee ^ --key file://key4.json ^ --endpoint-url http://localhost:8000 |
key4.json
1 2 3 |
{ "id": {"N": "100"} } |
BatchGetItem:
The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key. A single operation can retrieve up to 16 MB of data, which can contain as many as 100 items. BatchGetItem will return a partial result if the response size limit is exceeded, the table’s provisioned throughput is exceeded, or an internal processing failure occurs. If a partial result is returned, the operation returns a value for UnprocessedKeys. You can use this value to retry the operation starting with the next item to get.
Syntax:
1 2 3 4 5 |
batch-get-item --request-items [--return-consumed-capacity [--cli-input-json [--generate-cli-skeleton |
Example: We will fetch all details where id = 101 and 104 from Employee table
1 2 3 4 |
aws dynamodb batch-get-item ^ --request-items file://key5.json ^ --endpoint-url http://localhost:8000 ^ --output table |
key5.json
1 2 3 4 5 6 7 8 9 10 11 12 |
{ "Employee": { "Keys": [ { "id": {"N": "101"} }, { "id": {"N": "104"} } ] } } |
Query:
The Query operation finds items based on primary key values. You can query any table or secondary index that has a composite primary key (a partition key and a sort key).
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
query --table-name [--index-name [--select [--attributes-to-get [--consistent-read | --no-consistent-read] [--key-conditions [--query-filter [--conditional-operator [--scan-index-forward | --no-scan-index-forward] [--return-consumed-capacity [--projection-expression [--filter-expression [--key-condition-expression [--expression-attribute-names [--expression-attribute-values [--cli-input-json [--starting-token [--page-size [--max-items [--generate-cli-skeleton |
Example: We will fetch salary details where id = 101 from Employee table
1 2 3 4 5 6 |
aws dynamodb query ^ --table-name Employee ^ --projection-expression "salary" ^ --key-condition-expression "id = :v1" ^ --expression-attribute-values file://key6.json ^ --endpoint-url http://localhost:8000 |
key6.json
1 2 3 |
{ ":v1": {"N": "101"} } |
DeleteItem:
Deletes a single item in a table by primary key. You can perform a conditional delete operation that deletes the item if it exists, or if it has an expected attribute value.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
delete-item --table-name --key [--expected [--conditional-operator [--return-values [--return-consumed-capacity [--return-item-collection-metrics [--condition-expression [--expression-attribute-names [--expression-attribute-values [--cli-input-json [--generate-cli-skeleton |
Example: We will data where id = 104 from Employee table
1 2 3 4 |
aws dynamodb delete-item ^ --table-name Employee ^ --key file://key7.json ^ --endpoint-url http://localhost:8000 |
key7.json
1 2 3 |
{ "id": {"N": "104"} } |
DeleteTable:
The DeleteTable operation deletes a table and all of its items. After a DeleteTable request, the specified table is in the DELETING state until DynamoDB completes the deletion. If the table is in the ACTIVE state, you can delete it. If a table is in CREATING or UPDATING states, then DynamoDB returns a ResourceInUseException. If the specified table does not exist, DynamoDB returns a ResourceNotFoundException. If the table is already in the DELETING state, no error is returned.
Syntax:
1 2 3 4 |
delete-table --table-name [--cli-input-json [--generate-cli-skeleton |
Example: We will the Employee table
1 2 3 4 |
aws dynamodb delete-table ^ --table-name Employee ^ --endpoint-url http://localhost:8000 --output text |
Hope you have enjoyed this article. Stream API is not covered in this blog and will be covered when we will discuss features of DynamoDB. In the next blog post, we will discuss Primary Key in DynamoDB.
To get more details on DynamoDB, please refer below AWS documentation
https://docs.aws.amazon.com/dynamodb/index.html
Your topic seems somewhat terse-you’re often incredibly eloquent in your reasoning.