DynamoDB API – Part One
Hello Everyone
Welcome to CloudAffaire and this is Debjeet
In the last blog post, we have installed DynamoDB in our local system.
https://cloudaffaire.com/install-dynamodb-in-local-system/
In this blog post, we are going to discuss basic API calls to DynamicDB.
DynamoDB API :
AWS provides sets of API to interact with DynamoDB. In this demo, we will go through different sets of API that can be used with DynamoDB. For length constraint, we will divide this blog into two blogs.
CreateTable:
The CreateTable operation adds a new table to your account. In an AWS account, table names must be unique within each region. That is, you can have two tables with the same name if you create the tables in different regions.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 |
create-table --attribute-definitions --table-name --key-schema [--local-secondary-indexes [--global-secondary-indexes [--billing-mode [--provisioned-throughput [--stream-specification [--sse-specification [--cli-input-json [--generate-cli-skeleton |
Example: We will create a table Employee with a single primary key on id (number field)
1 2 3 4 5 6 7 |
aws dynamodb create-table ^ --table-name Employee ^ --attribute-definitions AttributeName=id,AttributeType=N ^ --key-schema AttributeName=id,KeyType=HASH ^ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 ^ --endpoint-url http://localhost:8000 ^ --output table |
DescribeTable:
Returns information about the table, including the current status of the table, when it was created, the primary key schema, and any indexes on the table.
Syntax:
1 2 3 4 |
describe-table --table-name [--cli-input-json [--generate-cli-skeleton |
Example: We will describe the Employee table that we just created.
1 2 3 4 |
aws dynamodb describe-table ^ --table-name Employee ^ --endpoint-url http://localhost:8000 ^ --output table |
ListTables:
Returns an array of table names associated with the current account and endpoint. The output from ListTables is paginated, with each page returning a maximum of 100 table names.
Syntax:
1 2 3 4 5 6 |
list-tables [--cli-input-json [--starting-token [--page-size [--max-items [--generate-cli-skeleton |
Example: List all tables present in DynamoDB.
1 |
aws dynamodb list-tables --endpoint-url http://localhost:8000 --output table |
PutItem:
Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item. You can perform a conditional put operation (add a new item if one with the specified primary key doesn’t exist), or replace an existing item if it has certain attribute values. You can return the item’s attribute values in the same operation, using the ReturnValues parameter.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
put-item --table-name --item [--expected [--return-values [--return-consumed-capacity [--return-item-collection-metrics [--conditional-operator [--condition-expression [--expression-attribute-names [--expression-attribute-values [--cli-input-json [--generate-cli-skeleton |
Example: We will insert a single record id = 100 in our Employee table from a JSON file (empl1.json)
1 2 3 4 |
aws dynamodb put-item ^ --table-name Employee ^ --item file://empl1.json ^ --endpoint-url http://localhost:8000 |
Empl1.json:
1 2 3 |
{ "id": {"N": "100"} } |
BatchWriteItem:
The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB.
Syntax:
1 2 3 4 5 6 |
batch-write-item --request-items [--return-consumed-capacity [--return-item-collection-metrics [--cli-input-json [--generate-cli-skeleton |
Example: We will insert 2 items (id = 101 and id = 104) in our existing employee table from Jason file (empl2.josn)
1 2 3 4 5 |
aws dynamodb batch-write-item ^ --request-items file://empl2.json ^ --endpoint-url http://localhost:8000 ^ --return-consumed-capacity TOTAL ^ --output text |
empl2.json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "Employee": [ { "PutRequest": { "Item": { "id": {"N": "101"}, "name": {"S": "Debjeet"}, "salary": {"N": "1000"} } } }, { "PutRequest": { "Item": { "id": {"N": "104"}, "name": {"S": "Zor"}, "salary": {"N": "500"} } } } ] } |
Scan:
The Scan operation returns one or more items and item attributes by accessing every item in a table or a secondary index. To have DynamoDB return fewer items, you can provide a FilterExpression operation.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
scan --table-name [--index-name [--attributes-to-get [--select [--scan-filter [--conditional-operator [--return-consumed-capacity [--total-segments [--segment [--projection-expression [--filter-expression [--expression-attribute-names [--expression-attribute-values [--consistent-read | --no-consistent-read] [--cli-input-json [--starting-token [--page-size [--max-items [--generate-cli-skeleton |
Example: We will get all the id from the Employee table.
1 2 3 4 |
aws dynamodb scan ^ --table-name Employee ^ --attributes-to-get id ^ --endpoint-url http://localhost:8000 |
Hope you have enjoyed this article. In the next blog post, we will continue from here.
To get more details on DynamoDB, please refer below AWS documentation
https://docs.aws.amazon.com/dynamodb/index.html