DynamoDB API – Part Two

DynamoDB API – Part Two

DynamoDB API – Part Two

Hello Everyone

Welcome to CloudAffaire and this is Debjeet

In the last blog post, we have discuss some of the DynamoDB API’s available.

In this blog post we are going to continue our discuss 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:

update-item
--table-name <value>
--key <value>
[--attribute-updates <value>]
[--expected <value>]
[--conditional-operator <value>]
[--return-values <value>]
[--return-consumed-capacity <value>]
[--return-item-collection-metrics <value>]
[--update-expression <value>]
[--condition-expression <value>]
[--expression-attribute-names <value>]
[--expression-attribute-values <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Example: We will add name (Bob) and salary (1200) where id = 100 in Employee table

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

{
"id": {"N": "100"}
}

key2.json

{
"#A":"name", "#B":"salary"
}

key3.json

{
":y":{"S": "Bob"},
":t":{"N": "1200"}
}

DynamoDB API

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:

get-item
--table-name <value>
--key <value>
[--attributes-to-get <value>]
[--consistent-read | --no-consistent-read]
[--return-consumed-capacity <value>]
[--projection-expression <value>]
[--expression-attribute-names <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Example: We will fetch all details where id = 100 from Employee table

aws dynamodb get-item ^
--table-name Employee ^
--key file://key4.json ^
--endpoint-url http://localhost:8000

key4.json

{
"id": {"N": "100"}
}

DynamoDB API

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:

batch-get-item
--request-items <value>
[--return-consumed-capacity <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Example: We will fetch all details where id = 101 and 104 from Employee table

aws dynamodb batch-get-item ^
--request-items file://key5.json ^
--endpoint-url http://localhost:8000 ^
--output table

key5.json

{
"Employee": {
"Keys": [
{
"id": {"N": "101"}
},
{
"id": {"N": "104"}
}
]
}
}

DynamoDB API

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:

query
--table-name <value>
[--index-name <value>]
[--select <value>]
[--attributes-to-get <value>]
[--consistent-read | --no-consistent-read]
[--key-conditions <value>]
[--query-filter <value>]
[--conditional-operator <value>]
[--scan-index-forward | --no-scan-index-forward]
[--return-consumed-capacity <value>]
[--projection-expression <value>]
[--filter-expression <value>]
[--key-condition-expression <value>]
[--expression-attribute-names <value>]
[--expression-attribute-values <value>]
[--cli-input-json <value>]
[--starting-token <value>]
[--page-size <value>]
[--max-items <value>]
[--generate-cli-skeleton <value>]

Example: We will fetch salary details where id = 101 from Employee table

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

{
":v1": {"N": "101"}
}

DynamoDB API

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:

delete-item
--table-name <value>
--key <value>
[--expected <value>]
[--conditional-operator <value>]
[--return-values <value>]
[--return-consumed-capacity <value>]
[--return-item-collection-metrics <value>]
[--condition-expression <value>]
[--expression-attribute-names <value>]
[--expression-attribute-values <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Example: We will data where id = 104 from Employee table

aws dynamodb delete-item ^
--table-name Employee ^
--key file://key7.json ^
--endpoint-url http://localhost:8000

key7.json

{
"id": {"N": "104"}
}

DynamoDB API

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 table is already in the DELETING state, no error is returned.

Syntax:

delete-table
--table-name <value>
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Example: We will the Employee table

aws dynamodb delete-table ^
--table-name Employee ^
--endpoint-url http://localhost:8000
--output text

DynamoDB API

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

 

This Post Has One Comment

  1. Your topic seems somewhat terse-you’re often incredibly eloquent in your reasoning.

Leave a Reply

Close Menu