dynamo-db
  1. dynamo-db-key-value-document-database

Key-Value & Document Database - DynamoDB Tutorial

DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It's a fully managed cloud database and supports both document and key-value data models. It's highly scalable and flexible, so it can handle a wide range of workloads, including web, gaming, ad tech, IoT, and others.

Syntax

Creating a table in DynamoDB:

aws dynamodb create-table \
    --table-name <table-name> \
    --attribute-definitions \
        AttributeName=<primary-key>,AttributeType=<type> \
    --key-schema \
        AttributeName=<primary-key>,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=<value>,WriteCapacityUnits=<value>

Inserting data into the table:

aws dynamodb put-item \
    --table-name <table-name> \
    --item '{"<attribute-name>": {"<type>": "<value>"}, ...}'

Querying data from the table:

aws dynamodb query \
    --table-name <table-name> \
    --key-condition-expression <key-condition-expression> \
    --expression-attribute-values '{":<value>": {"<type>": "<value>"}}'

Example

Creating a table in DynamoDB:

aws dynamodb create-table \
    --table-name users \
    --attribute-definitions \
        AttributeName=id,AttributeType=S \
        AttributeName=name,AttributeType=S \
    --key-schema \
        AttributeName=id,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=1,WriteCapacityUnits=1

Inserting data into the table:

aws dynamodb put-item \
    --table-name users \
    --item '{"id": {"S": "1"}, "name": {"S": "John Doe"}}'

Querying data from the table:

aws dynamodb query \
    --table-name users \
    --key-condition-expression "id = :val" \
    --expression-attribute-values '{":val":{"S":"1"}}'

Output

Creating a table in DynamoDB returns a JSON object with information about the table:

{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "id",
                "AttributeType": "S"
            },
            {
                "AttributeName": "name",
                "AttributeType": "S"
            }
        ],
        "TableName": "users",
        "KeySchema": [
            {
                "AttributeName": "id",
                "KeyType": "HASH"
            }
        ],
        "TableStatus": "CREATING",
        "CreationDateTime": "2021-09-28T19:43:57.368000+00:00",
        "ProvisionedThroughput": {
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:<region>:<account-id>:table/users"
    }
}

Inserting data into the table doesn't return any output unless there's an error.

Querying data from the table returns a JSON object with the query results:

{
    "Items": [
        {
            "id": {
                "S": "1"
            },
            "name": {
                "S": "John Doe"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

Explanation

DynamoDB is a NoSQL database, which means that it doesn't use tables with predefined schemas like relational databases. Instead, it stores data as key-value pairs or documents, which can have different structures. Each item in a DynamoDB table has a primary key, which is used to uniquely identify the item and to partition the data.

In the example above, we created a table named "users" with two attributes: "id" and "name". "id" is the primary key of the table, and its type is string ("S"). We also specified the provisioned throughput of the table, which determines the number of reads and writes per second that the table can handle.

To insert data into the table, we used the "put-item" command and provided the attributes of the item as a JSON object. Each attribute has a name and a value, and the value is a JSON object with a type and a value that matches the type.

To query data from the table, we used the "query" command and provided the key condition expression and the expression attribute values that match the primary key of the item we're looking for.

Use

DynamoDB is a good choice for applications that require low-latency data access and high scalability. It's suitable for a wide range of workloads, including:

  • Web applications
  • Gaming applications
  • Ad tech applications
  • IoT applications
  • Real-time analytics applications
  • Mobile applications

DynamoDB is also integrated with other AWS services, such as Lambda, API Gateway, and Elasticsearch, which makes it easy to build serverless applications and to analyze and visualize the data stored in the database.

Important Points

  • DynamoDB is a NoSQL database that supports the key-value and document data models.
  • It's a fully managed cloud database and highly scalable and flexible.
  • To create a table, we specify the attribute definitions, the key schema, and the provisioned throughput.
  • To insert data into the table, we use the "put-item" command and provide the attributes of the item as a JSON object.
  • To query data from the table, we use the "query" command and provide the key condition expression and the expression attribute values that match the primary key of the item we're looking for.

Summary

DynamoDB is a key-value and document database that provides single-digit millisecond performance at any scale. It's a fully managed cloud database and supports the key-value and document data models. To use DynamoDB, we create a table with the attribute definitions, the key schema, and the provisioned throughput. We insert data into the table using the "put-item" command and query data from the table using the "query" command. DynamoDB is suitable for a wide range of applications, including web, gaming, ad tech, IoT, and others, and is integrated with other AWS services to build serverless applications and to analyze and visualize the data stored in the database.

Published on: