Core constructs/concepts of DynamoDB
- Table - A collection of items (data) in DynamoDB.
- Items - A collection/group of attributes within a table. (Items in DynamoDB are similar in many ways to rows, records, or tuples in other database systems.)
- Attributes- An attribute is a fundamental data element, something that does not need to be broken down any further.
- DynamoDB supports nested attributes up to 32 levels deep.
Let’s relate above constructs with Document Databases
- Collection - A collection where you can store documents, similar to Table in DynamoDB.
- Document - An individual record within a collection, similar to an Item in DynamoDB.
- Fields - Data fields within a document, equivalent to attributes in DynamoDB.
• MongoDB “collection” → DynamoDB “table”
• MongoDB “document” → DynamoDB “item”
• MongoDB “field” → DynamoDB “attribute”
Maximum size of Item in DynamoDB is
400KB
Two types of keys in an Item.
-
Partition key - also called a hash key
- DynamoDB uses the partition key’s value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored.
-
Sort key - also called a range key
- DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same partition key value are stored together, in sorted order by sort key value.
Primary Key
- When you create a table, in addition to the table name, you must specify the primary key of the table.
- The primary key uniquely identifies each item in the table, so that no two items can have the same key.
DynamoDB supports two different kinds of primary keys
- Partition key – A simple primary key, composed of one attribute known as the partition key.
- Partition key and sort key – Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.
Secondary indexes
- You can create one or more secondary indexes on a table. A secondary index lets you query the data in the table using an alternate key, in addition to queries against the primary key. DynamoDB doesn’t require that you use indexes, but they give your applications more flexibility when querying your data.
DynamoDB supports two kinds of indexes:
- Global secondary index – An index with a partition key and sort key that can be different from those on the table.
- Local secondary index – An index that has the same partition key as the table, but a different sort key.
Install NoSQL workbench & Dynamodb Local
brew install --cask nosql-workbench
# install sdkman for java
curl -s "https://get.sdkman.io" | bash
sdk list java
sdk install java 23.0.1-open
java --version
# download dynamodb local zip file from here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html
unzip dynamodb_local_latest.zip
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
export AWS_ACCESS_KEY_ID=dummy
export AWS_SECRET_ACCESS_KEY=dummy
export AWS_DEFAULT_REGION=us-east-1
aws dynamodb list-tables --endpoint-url http://localhost:8000References: